Django Blog Project #16: Adding URL redirects using the Blogger API
I wanted to insert URL redirects on my old Blogger posts pointing to my new blog articles. A comment on my Migrating Blogger Posts post suggested that I use the (Python) Blogger API. This was a great suggestion. The Blogger API was well documented and easy to use. Here is the script I used to insert the URL redirects on each of my old Blogger posts.
from gdata import service
import re
import gdata
import atom
NEW_HTML = """
<script language="javascript">
setTimeout('location.href="%s"', 2000);
</script>
<br /><br />
<b>
</b><p>This is my OLD blog. I've copied this post over to my NEW blog at:</p>
<p><a href="%s">%s</a></p>
<p>You should be redirected in 2 seconds.</p>
<br /><br />
"""
# authenticate
blogger_service = service.GDataService('myusername@gmail.com', 'mypassword')
blogger_service.service = 'blogger'
blogger_service.account_type = 'GOOGLE'
blogger_service.server = 'www.blogger.com'
blogger_service.ProgrammaticLogin()
# get list of blogs
query = service.Query()
query.feed = '/feeds/default/blogs'
feed = blogger_service.Get(query.ToUri())
# get blog id
blog_id = feed.entry[0].GetSelfLink().href.split("/")[-1]
# get all posts
query = service.Query()
query.feed = '/feeds/%s/posts/default' % blog_id
query.published_min = '2000-01-01'
query.published_max = '2009-01-01'
query.max_results = 1000
feed = blogger_service.Get(query.ToUri())
print feed.title.text
for entry in feed.entry:
# create link to article on new blog
new_link = re.sub(r'http://iwiwdsmi\.blogspot\.com/(.*)\.html',
r'http://www.saltycrane.com/blog/\1/',
entry.link[0].href)
print new_link
# update post
to_add = NEW_HTML % (new_link, new_link, new_link)
entry.content.text = to_add + entry.content.text
blogger_service.Put(entry, entry.GetEditLink().href)
4
Comments
—
Comments feed for this post
#2 Crivawrernima commented on 2009-02-06:
Thanks a lot for the tips................! i will keep visit your blog later again........!
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(4)
-
aws
(8)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(9)
-
conkeror
(2)
-
cygwin
(18)
-
datastructures
(15)
-
datetime
(3)
-
dell
(3)
-
django
(39)
-
emacs
(20)
-
files_directories
(10)
-
install_setup
(7)
-
javascript
(3)
-
keyboard
(6)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
preferences
(8)
-
processes
(3)
-
pyqt
(18)
-
python
(122)
-
ratpoison
(3)
-
regexes
(5)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(13)
-
ssh
(7)
-
subversion
(6)
-
twisted
(6)
-
ubuntu
(60)
-
urxvt
(5)
-
vxworks
(25)
-
webservices
(4)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan
#1 niseVeids commented on 2008-10-01:
Hello Nice site!
G'night