Advertisement
Guest User

BenO

a guest
Feb 18th, 2009
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. import simplejson, httplib2, BeautifulSoup, urllib
  2. h = httplib2.Http()
  3. h.add_credentials('randomdev8d','felpo4242')
  4. search = httplib2.Http()
  5. from time import sleep
  6.  
  7. import pickle, os
  8.  
  9. if os.path.exists('people'):
  10.   p = open('people', 'rb')
  11.   people = pickle.load(p)
  12.   p.close()
  13. else:
  14.   people = {}
  15.  
  16. """ e.g. "text":"#dev8D @dev8d survey: Win7\/Laptop","to_user_id":null,"from_user":"alexwade","id":1202175487,"from_user_id":4737001,"iso_language_code":"fr","profile_image_url":"http:\/\/static.twitter.com\/images\/default_profile_normal.png","created_at":"Thu, 12 Feb 2009 10:04:25 +0000"}
  17. """
  18.  
  19. def get_results(text):
  20.   if text and isinstance(text, basestring):
  21.     tokens = text.split(' ')
  22.     results = []
  23.     for token in tokens:
  24.       token = token.lower()
  25.       spl = token.split('/')
  26.       if len(spl) == 2:
  27.         if spl[1].startswith('laptop'):
  28.           results.append((spl[0], 'laptop'))
  29.         elif spl[1].startswith('netbook'):
  30.           results.append((spl[0], 'netbook'))
  31.         elif spl[1].startswith('ec2') or spl[1].startswith('amazon'):
  32.           results.append((spl[0], 'amazonEC2'))
  33.         elif spl[1].startswith('iphone'):
  34.           results.append((spl[0], 'iphone'))
  35.         elif spl[1].startswith('mac'):
  36.           results.append((spl[0], spl[1]))
  37.   return results
  38.  
  39. def stats():
  40.   os = {}
  41.   hw = {}
  42.   for person in people:
  43.     for item in people[person]:
  44.       if os.has_key(item[0]):
  45.         os[item[0]] = os[item[0]] + 1
  46.       else:
  47.         os[item[0]] = 1
  48.       if hw.has_key(item[1]):
  49.         hw[item[1]] = hw[item[1]] + 1
  50.       else:
  51.         hw[item[1]] = 1
  52.   return (os, hw)
  53.  
  54. while True:
  55.   print "Checking surveys"
  56.   (resp, content) = search.request('http://search.twitter.com/search.json?rpp=99&q=dev8D+OR+dev8dsurvey')
  57.   doc = simplejson.loads(content.decode('utf-8'))
  58.   for item in doc['results']:
  59.     user = item['from_user']
  60.     if not (user == 'randomdev8d'):
  61.       text = item['text']
  62.       results = get_results(text)
  63.       if people.has_key(user):
  64.         for res in results:
  65.           people[user].add(res)
  66.       else:
  67.         people[user] = set(results)
  68.  
  69.   packet = stats()
  70.  
  71.   p = open('people', 'wb')
  72.   pickle.dump(people, p)
  73.   p.close()
  74.  
  75.   st = ["#dev8d tallys: OSs"]
  76.   for k in packet[0]:
  77.     st.append("%s:%s" % (k,packet[0][k]))
  78.   hwst = ["#dev8d tallys: HW"]
  79.   for k in packet[1]:
  80.     hwst.append("%s:%s" % (k,packet[1][k]))
  81.   body = urllib.urlencode({'status':", ".join(st)})
  82.   (rs,cont) = h.request('http://www.twitter.com/statuses/update.json', "POST", body=body)
  83.   sleep(20)
  84.   body = urllib.urlencode({'status':", ".join(hwst)})
  85.   (rs,cont) = h.request('http://www.twitter.com/statuses/update.json', "POST", body=body)
  86.   sleep(1440)
  87.  
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement