Skip to content

Instantly share code, notes, and snippets.

@myles
Created November 27, 2009 15:36
Show Gist options
  • Save myles/244070 to your computer and use it in GitHub Desktop.
Save myles/244070 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
"""
pygtatweet2identica.py
PyGTATweet2Identica converts PyGTA's Twitter timeline to PyGTA's
Identi.ca timeline.
Created by Myles Braithwaite on 2009-11-02.
Copyright (c) 2009 PyGTA (Python GTA User Group).
Insipired by:
darling.py
Created by Rui Carmo on 2009-07-21.
Copyright (c) 2009 __MyCompanyName__. All rights reserved.
http://the.taoofmac.com/space/blog/2009/11/01/1010
"""
import re
import twitter
import simplejson
import sqlite3
import httplib2
from urllib import urlencode
TWITTER_USERNAME = ''
TWITTER_PASSWORD = ''
IDENTICA_USERNAME = ''
IDENTICA_PASSWORD = ''
def main():
db = sqlite3.connect("/home/myles/Applications/Twitter2Identica/pygta_tweets.db")
c = db.cursor()
# Create the database Table and Index
c.execute("""CREATE TABLE IF NOT EXISTS status (
id text, status text, seen boolean)""")
c.execute("""CREATE UNIQUE INDEX IF NOT EXISTS
id on status (id ASC)""")
db.commit()
# Connect to Twitter and download the latest tweets.
api = twitter.Api(username=TWITTER_USERNAME, password=TWITTER_PASSWORD,
input_encoding=None)
tweets = api.GetUserTimeline(TWITTER_USERNAME)
# Insert tweets into the database.
for i in tweets:
c.execute("""INSERT OR IGNORE INTO status (id, status)
VALUES (?, ?)""", (i.id, i.text))
db.commit()
# Get all the tweets that are not seen.
c.execute("""SELECT * FROM status WHERE seen IS NULL
ORDER BY id ASC""")
queue = []
for row in c:
(key, status, state) = row
queue.append((key, status))
for i in queue:
(key, status) = i
# Converting the Hashtag to Group
p = re.compile('#pygta', re.IGNORECASE)
new_status = p.sub('!pygta', status)
# Connecting to Identi.ca
h = httplib2.Http()
h.add_credentials(IDENTICA_USERNAME, IDENTICA_PASSWORD, 'identi.ca')
hrs = {'Content-Type': 'application/x-www-form-urlencoded'}
# Posting the status.
resp, content = h.request(
'http://identi.ca/api/statuses/update.json',
'POST', urlencode({'status': new_status}),
headers=hrs)
if resp['status'] == '200':
# If posting the status worked mark update as seen
c.execute("""UPDATE OR REPLACE status
SET seen=? WHERE id=?""", ('1', key))
db.commit()
return
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment