Guest User

khohols.py

a guest
Mar 15th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. import httplib
  2. import json
  3. import re
  4. import sys
  5.  
  6.  
  7. def main():
  8.     if len(sys.argv) < 2:
  9.         print 'Please pass thread url in commandline'
  10.         return
  11.  
  12.     url = sys.argv[1]
  13.     if url[len(url) - 4:] == 'html':
  14.         url = url.replace('html', 'json')
  15.     else:
  16.         url += '.json'      # this could fail hard
  17.  
  18.     header = {'User-Agent': 'Opera 9.20'}
  19.     conn = httplib.HTTPConnection('www.2ch.hk', 80, header)
  20.     conn.request('GET', url)
  21.     response = conn.getresponse()
  22.     if 300 <= response.status < 200:
  23.         print response.status + '\n' + response.reason
  24.         return
  25.  
  26.     thread = response.read()
  27.     conn.close()
  28.     decoded = json.loads(thread)
  29.  
  30.     trips = {}
  31.     hohols = 0
  32.     regexp = re.compile('.+postertripid\">([\w\d\+/-]{8}).+src=\"/flags/([a-zA-Z]+)\.png')
  33.     for i in decoded['thread']:
  34.         field = i[0][u'name']
  35.         t, c = regexp.match(field).groups()
  36.         if not t in trips:
  37.             if c == u'ua':
  38.                 hohols += 1
  39.                 trips[t] = 1
  40.  
  41.     print hohols
  42.     return
  43.  
  44.  
  45. if __name__ == '__main__':
  46.     main()
Advertisement
Add Comment
Please, Sign In to add comment