User:Jonas Lund/MrRetweet: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "<source lang='python'> #!/usr/bin/env python # coding: utf-8 import twittermod, urlparse, urllib2 import lxml.etree from sqlite3 import * #initate mysqlite database connection =...")
 
No edit summary
Line 16: Line 16:


#initate twitter api
#initate twitter api
consumer_key='DCWaou5hDHfHSj9rvM39Q'
consumer_key=''
consumer_secret='OVLaXLlP1CXFgYsRzHMyDtjBp7eYHylJMrIMhasig'
consumer_secret=''
oauth_token_secret = 'MijOBSqA3hkm75nwWmz0i2fByhypMPknAQppYifA'
oauth_token_secret = ''
user_id = '403512764'
user_id = ''
oauth_token = '403512764-lZsUPFnTYvKUY00DAZgKJo3SYw4EJVEPAxRpVmx9'
oauth_token = ''
screen_name = 'mrrt2000'
screen_name = ''


api = twittermod.Api(consumer_key=consumer_key,
api = twittermod.Api(consumer_key=consumer_key,

Revision as of 18:53, 2 November 2011

#!/usr/bin/env python
# coding: utf-8
import twittermod, urlparse, urllib2
import lxml.etree
from sqlite3 import *

#initate mysqlite database
connection = connect('mrretweet.db')
cursor = connection.cursor()

#create tables – run once
#cursor.execute('''CREATE TABLE followers (id integer primary key, nick text)''')
#cursor.execute('''CREATE TABLE tweets (id integer primary key, pid integer, uid integer, tweet text)''')
#connection.commit()

#initate twitter api
consumer_key=''
consumer_secret=''
oauth_token_secret = ''
user_id = ''
oauth_token = ''
screen_name = ''

api = twittermod.Api(consumer_key=consumer_key,
consumer_secret=consumer_secret, access_token_key=oauth_token, access_token_secret=oauth_token_secret)

#Populate Most Recent Follower DB
followers = api.GetFollowers()
for follower in followers:
  follower = follower.screen_name

  cursor.execute("SELECT id FROM followers WHERE nick = ?", (follower,))
  data=cursor.fetchall()
  if len(data)==0:
    print('There is no follower %s'%follower)
    cursor.execute("INSERT INTO follower VALUES (NULL, ?)", (follower,))
    connection.commit()              

  else:
     print('Follower %s found with rowids %s'%(follower,','.join(map(str,zip(*data)[0]))))


#create follower rss-list
rows = cursor.execute("SELECT nick FROM followers")
for row in rows:
    
    #rss-feed
    follower = row[0]
    rssFeedUrl = "https://twitter.com/statuses/user_timeline/{0}.rss".format(follower)

    #open rss feed
    f = urllib2.urlopen(rssFeedUrl)
    doc = lxml.etree.parse(f)    
    
    for item in doc.xpath("//item"):
      tweetid = item.xpath(".//guid/text()")[0]
      Segments = tweetid.rpartition('/')
      tweetid = int(Segments[2])
      
      #if is record
      cursor.execute("SELECT id FROM tweets WHERE tweet = ?", (tweetid,))
      data=cursor.fetchall()
      if len(data)==0:
        print('There is no record of %s'%tweetid)
        cursor.execute("INSERT INTO tweets VALUES (NULL, NULL, NULL, ?)", (tweetid,))
        connection.commit()
        
        api.PostRetweet(tweetid)
      else:
        print('Tweet %s found with rowids %s'%(tweetid,','.join(map(str,zip(*data)[0]))))