Skip to content

@gauteh /.gitignore
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
python script for starring and liking google reader items from exported json (used to migrate starred and liked items), check http://gaute.vetsj.com/greader for web app version.
*.json
*.xml
token*
oauth*
#! python2
#
# Gaute Hope <eg@gaute.vetsj.com> 2011
#
# Note: This script was only made to serve its purpose one time, and is not
# necessarily very user friendly. But have been used with success migrating
# Google Reader data to a Google Apps account.
#
# Stars and likes items in Google Reader, used to migrate starred
# and liked items from old account to new. Subscriptions already
# transferred using OPML, json acquired using the export functionality
# on original account.
#
# First: Create a secret API key for this application. Since this is not
# provided as a service, but run stand alone, you are the provider
# and need a secret key!
# See: https://accounts.google.com/ManageDomains
#
# This does work on Google Apps, but enable the Webmaster Tools
# Service and add service (domain) and generate key through there.
#
# Store key in file oauth_key and secret in file oauth_secret (or edit
# script below).
#
# Place files:
# - starred-items.json
# - liked-items.json
#
# in same folder as script and run with python2. You will have to grant access
# to this program for your account. Click on printed url, and when done
# press enter in this program.
#
# Resources:
# - http://asktherelic.com/2010/04/26/accessing-google-reader-via-oauth-and-python/
# - http://blog.martindoms.com/2010/01/20/using-the-google-reader-api-part-3/#item-editing
# - http://code.google.com/p/google-api-python-client/wiki/OAuth2
import os
import os.path
import json
import sys
import urlparse
import urllib
import oauth2 as oauth
scope = 'http://www.google.com/reader/api'
sub_url = '%s/0/subscription/list?output=json' % scope
starred_url = '%s/0/stream/contents/user/-/state/com.google/starred?output=json' % scope
reading_url = '%s/0/stream/contents/user/-/state/com.google/reading-list?output=json' % scope
# google auth
request_token_url = "https://www.google.com/accounts/OAuthGetRequestToken?scope=%s" % scope
authorize_url = 'https://www.google.com/accounts/OAuthAuthorizeToken'
access_token_url = 'https://www.google.com/accounts/OAuthGetAccessToken'
f = open ('oauth_key', 'r')
oauth_key = f.read ().strip ()
f.close ()
f = open ('oauth_secret', 'r')
oauth_secret = f.read ().strip ()
f.close ()
consumer = oauth.Consumer (oauth_key, oauth_secret)
print "Authorzing with Google Reader.."
if not (os.path.exists('token') and os.path.exists('token_secret')):
client = oauth.Client (consumer)
# Requesting token
print "Request token.."
resp, content = client.request (request_token_url, 'GET')
request_token = dict(urlparse.parse_qsl (content))
print "Oauth Token: ", request_token['oauth_token']
print "Oauth Token secret: ", request_token['oauth_token_secret']
# Authorize in browser
print "=> Open this link in a browser..:"
print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token'])
print
print "Press ENTER when ready.."
raw_input ()
# Get access token
token = oauth.Token (request_token['oauth_token'], request_token['oauth_token_secret'])
client = oauth.Client (consumer, token)
resp, content = client.request(access_token_url, 'POST')
access_token = dict(urlparse.parse_qsl (content))
print "Access token:"
print "Oauth token: ", access_token['oauth_token']
print "Oauth secret token: ", access_token['oauth_token_secret']
# Writing token
print "Writing token to file.."
f = open ('token', 'w')
f.write (access_token['oauth_token'])
f.close ()
oauth_token = access_token['oauth_token']
f = open ('token_secret', 'w')
f.write (access_token['oauth_token_secret'])
f.close ()
oauth_token_secret = access_token['oauth_token_secret']
else:
# Reading token from file
print "Reading token from file.."
f = open ('token', 'r')
oauth_token = f.read ()
f.close ()
f = open ('token_secret', 'r')
oauth_token_secret = f.read ()
f.close ()
print "Token: ", oauth_token
print "Token secret: ", oauth_token_secret
token = oauth.Token (oauth_token, oauth_token_secret)
client = oauth.Client (consumer, token)
# Trying to get subscriptions..
print "Testing token..",
resp, content = client.request (sub_url, 'GET')
subs = json.loads (content)
if 'subscriptions' in subs:
print "Token alright."
else:
print "Token wrong, could not get subscriptions."
sys.exit (1)
# Getting starred items..
print "Remotely starred items:"
resp, content = client.request (starred_url, 'GET')
stars = json.loads (content)
for i in stars['items']:
print "[*]", i['title']
### Starred items
starred = open ('starred-items.json', 'r')
items = json.load (starred)
starred.close ()
## Getting edit token
print "Getting edit token.."
token_url = '%s/0/token?client=scroll' % scope
resp, content = client.request (token_url, 'GET')
oauth_token_edit = content
for i in items['items']:
print "=> Starring: ", i['title'], "..",
url = '%s/0/edit-tag?client=scroll' % scope
data = 'a=user/-/state/com.google/starred&async=true&s=%(sid)s&i=%(id)s&T=%(token)s' % { 'sid' : i['origin']['streamId'], 'id' : i['id'], 'token' : oauth_token_edit }
resp, content = client.request (url, 'POST', body = data)
if resp['status'] == '200':
print 'done.'
else:
print 'failed.'
print "Liking items.."
liked = open ('liked-items.json', 'r')
items = json.load (liked)
for i in items['items']:
print "=> Liking: ", i['title'], "..",
url = '%s/0/edit-tag?client=scroll' % scope
data = 'a=user/-/state/com.google/like&async=true&s=%(sid)s&i=%(id)s&T=%(token)s' % { 'sid' : i['origin']['streamId'], 'id' : i['id'], 'token' : oauth_token_edit }
resp, content = client.request (url, 'POST', body = data)
if resp['status'] == '200':
print 'done.'
else:
print 'failed.'
@DavidJRobertson

Very useful for moving from a Gmail account to a Google Apps account. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.