User:Jonas Lund/MrRetweet

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


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


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)''')

#initate twitter api
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,))
  if len(data)==0:
    print('There is no follower %s'%follower)
    cursor.execute("INSERT INTO follower VALUES (NULL, ?)", (follower,))

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

#create follower rss-list
cursor.execute("SELECT nick FROM followers")
for row in rows:
    follower = row[0]
    rssFeedUrl = "{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,))
      if len(data)==0:
        print('There is no record of %s'%tweetid)
        cursor.execute("INSERT INTO tweets VALUES (NULL, ?)", (tweetid,))
        print('Tweet %s found with rowids %s'%(tweetid,','.join(map(str,zip(*data)[0]))))


@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.