User:Jonas Lund/MrRetweet

From Media Design: Networked & Lens-Based wiki
Jump to navigation Jump to search

What

MrRetweet is twitter account that retweets everything his followers are tweeting.

How

Using a modification of the python-twitter module, I first retrieve a list of mrrt2000's followers, compare this list with a sqlite database table followers and insert the new followers into the table (whilst ignoring the followers who already exist in the database). The next step is to retrieve each followers tweets as an RSS feed with urllib2 and then process the feed with lxml. The GUID of each tweet from the list generated by lxml is compared with another table in the database, tweets, ignored if they already exist and inserted and retweet if they are new to the table.

#!/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, tweet integer)''')
#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
cursor.execute("SELECT nick FROM followers")
rows=cursor.fetchall()
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, ?)", (tweetid,))
        connection.commit()
        
        api.PostRetweet(tweetid)
      else:
        print('Tweet %s found with rowids %s'%(tweetid,','.join(map(str,zip(*data)[0]))))

Why

@mrrt2000 can efficiently increase your network reach and influence without you ever having to lift a finger. Every tweet from each follower will be distributed evenly and without regards to its content, the only filter by which it acts is the users own decision to follow mrrt2000. Ultimately, mrrt2000 can become an collective (self-)perpetuating feedback loop, as you will see every retweet he tweets, even your own.