Simple autoreload for wsgi python apps running in uWSGI

While developing, it’s usefull to have uwsgi autoreload your application. In my case I rsync changed files to the server running uWSGI. But uWSGI only monitors the app configuration file, which generally doesn’t change. In order to have your app auto-reload (like django does) you can switch to your app’s base directory and execute:

while true ; do
  find . -name \*.py                  \ # for python
         -newer /path/app.ini         \ # if changed
         -exec touch /path/app.ini \; \ # reload
         -print                         # print
  sleep 2
done

Everytime you rsync or scp some python file to the server the -newer predicate will turn true for this file and find will touch the configuration file (thereby making it newer than all the source files).

Advertisements

Mass/Bulk Follow with py-twitter

While trying to maintain the Greek Liberals twitter account, specifically trying to follow a bunch of people that I also follow, I didn’t manage to find a satisfactory solution to automatically follow a specific list of screen names.

Zac Bowling‘s bulk following tool wasn’t working for me (I get an authentication error). The only other promising solution is Flashtweet but they insist on loading the list of new people to follow from another twitter account’s follow list. At first that might not seem like a problem, since I could simply load my personal twitter account friends in Flashtweet and check the people I wanted to follow. But I already had a hand-picked list of people that I wanted the GreekLiberals twitter account to follow. I created this list by going through the CSV file that Tweeple exports where I can browse all the details of a user instead of just the twitter name like in Flashtweet’s interface. I don’t want to start mindlessly following people who for starters might not even speak greek or be otherwise interested in dialogue with the Greek Liberals.

So I decided to write a py-twitter script to do the importing. Here it is:

import twitter
import simplejson
from urllib2 import HTTPError, URLError

class MassFollower(twitter.Api):
   ''' Follow a list of people '''

   def AllFriends(self):
      friends = []
      while (not len(friends) % 100) :
         # see http://code.google.com/p/python-twitter/issues/detail?id=20
         friends.extend(self.GetFriends(page=((len(friends)/100)+1)))
      return friends

   def MassFollow(self,names):
      for name in set(names)-set([f.screen_name for f in self.AllFriends()]):
         if not self.GetUser(name).protected:
            try:
               self.CreateFriendship(name)
            except HTTPError, e:
               print "got error with code",e.code
               # see http://code.google.com/p/python-twitter/issues/detail?id=33 
               if e.code == 401 or e.code == 403:
                  data=simplejson.loads(e.read())
                  print "got data", data
                  if (hasattr(data,'error')):
                     print "can't follow ", name, " because ", err.error
                     continue
               raise

if __name__ == "__main__":
   import sys
   import getpass
   u=sys.argv[1]
   file=open(sys.argv[2],'r')
   p=getpass.getpass("Twitter password: ")
   try:
      api = MassFollower(username=u,password=p)
      api.MassFollow([ line.strip() for line in file ])
   except IOError, e:
      if hasattr(e, 'reason'):
         print "Can't contact twitter:", e.reason
      elif hasattr(e, 'code'):
         if e.code==400:
            print "Probably struck rate limit, try again later."
         else:
            print 'The server couldn\'t fulfill the request: code',e.code

Kudos to Niklas Saers whose own py-twitter utility, lasttweet.py, tipped me off to the page parameter argument to GetFriends method. At the time of this writing, an observant fellow will notice that, according to the aforelinked docs, there is no parameter named page. This is why this script is currently dependent on a version of py-twitter greater than 0.5, which is currently the stable version. So basically you need to get a svn checkout and install it manually on your system.

Apart from being my first use of the twitter API it is also the first semi-usefull thing I’ve written in python, so I would be gratefull for criticism or comments regarding the code above.