public
Last active

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.

  • Download Gist
.gitignore
1 2 3 4
*.json
*.xml
token*
oauth*
staritems.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
#! 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.'

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.