Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2012
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. import gevent.monkey
  3. gevent.monkey.patch_all()
  4.  
  5. import urllib, os, json, re, sys,csv
  6. import csv
  7. import urllib2
  8.  
  9. def get_data(uid):
  10.     url = 'http://webvybory2012.ru/json/voting_stations/%d/%d.json'% (uid / 100, uid)
  11.     raw_data = urllib.urlopen(url).read().decode('utf8')
  12.     try:
  13.         data = json.loads(raw_data)
  14.         return data['name'], data['address']
  15.     except:
  16.         pass
  17.  
  18. def get_channels(uid):
  19.     req = urllib2.Request('http://webvybory2012.ru/account/channels?station_id=%d' % uid)
  20.     req.add_header("Cookie", "<Paste your webvybory2012 cookie here>" )
  21.     f = urllib2.urlopen(req)
  22.     return f.read()
  23.  
  24.  
  25. def get_row(uid):
  26.     result =  get_data(uid)
  27.     if result:
  28.         cameras =  re.findall(r'\x24([^\x00]*?)(?:\x92|\x93)(?:\xae|\xad)(.*?)(?:\xae|\xad)', get_channels(uid))
  29.         row = [uid, result[1], result[0]] +  ["%s %s" % (camera[1], camera[0]) for camera in cameras]
  30.         #~ print "Writing ", result[1], result[0]
  31.  
  32.         return [unicode(item).encode('utf8') for item in row]
  33.  
  34. from gevent.pool import Pool
  35. map_func = Pool(10).map
  36.  
  37. writer = csv.writer(open(sys.argv[1], "wt"))
  38.  
  39. max_uid = 145000
  40. step = 500
  41.  
  42. for i in xrange(max_uid / step):
  43.     start, end = i * step, (i + 1) * step
  44.     rows = map_func(get_row, xrange(start, end))
  45.  
  46.  
  47.     for row in rows:
  48.         if row:
  49.             writer.writerow(row)
  50.     print "Completed %d " % end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement