Created
March 19, 2017 06:45
-
-
Save timkofu/52d1c3e613bf516b8339c9e64c0e83ab to your computer and use it in GitHub Desktop.
Copy your Google+ posts to Twitter (2013)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
import json | |
import twitter | |
import sqlite3 | |
import os | |
from HTMLParser import HTMLParser | |
import nltk | |
class Worker(object): | |
def __init__(self, profile_id, oauth, cursor): | |
self.google_api_key = 'xxxxxxxxxxxxxxxxxxxx' | |
self.profile_id = profile_id | |
self.cursor = cursor | |
self.oauth = oauth | |
def _get_from_gplus(self): | |
if not self.profile_id: return | |
data = urllib.urlopen( | |
'https://www.googleapis.com/plus/v1/people/{0}/activities/public?key={1}'.format( | |
self.profile_id, self.google_api_key | |
) | |
).read() | |
items = json.loads(data).get('items') | |
if not items: return 'exists' | |
the_thing = items[0] | |
try: | |
self.cursor.execute("insert into posts values(?)", (the_thing['url'],)) | |
except sqlite3.IntegrityError: | |
return 'exists' | |
return HTMLParser().unescape(u'G+: '+nltk.clean_html(the_thing['object']['content'])[:64]).replace('\n', ' ')+u' ... '+the_thing['url'] | |
def _post_to_twitter(self, tweet, user): | |
if tweet == 'exists': return | |
twitter.Api(**self.oauth).PostUpdate(tweet) | |
def work(self): | |
self._post_to_twitter(self._get_from_gplus(), 'tim') | |
if __name__ == "__main__": | |
with sqlite3.connect(os.path.join(os.path.dirname(__file__), 'gplustotwitter.db')) as conn: | |
cursor = conn.cursor() | |
# Users | |
users = {'tim':{'oauth':{'consumer_key':'xxxxxxxxxxxxxxxxxxxx', | |
'consumer_secret':'xxxxxxxxxxxxxxxxxxxx', | |
'access_token_key':'xxxxxxxxxxxxxxxxxxxx', | |
'access_token_secret':'xxxxxxxxxxxxxxxxxxxx'}, | |
'gplus_id':'xxxxxxxxxxxxxxxxxxxx' | |
}, | |
} | |
for user in users.keys(): | |
Worker(users[user]['gplus_id'], users[user]['oauth'], cursor).work() | |
conn.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment