Advertisement
PsHegger

Stack Overflow reputation checker

Nov 18th, 2013
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import urllib2
  5. from HTMLParser import HTMLParser
  6. from htmlentitydefs import name2codepoint
  7.  
  8. user_id = 1293167                                  # tuss laszlo
  9. #user_id = 751482                                    # pshegger
  10. so_base_url = "http://stackoverflow.com/users/"
  11.  
  12. class SOParser(HTMLParser):
  13.     def __init__(self):
  14.         HTMLParser.__init__(self)
  15.         self.span_started = False
  16.         self.repu_started = False
  17.         self.username_started = False
  18.         self.username = ""
  19.         self.repu = 0
  20.  
  21.     def handle_starttag(self, tag, attrs):
  22.         if tag == "span":
  23.             self.span_started = True
  24.  
  25.         if tag == "a" and self.span_started:
  26.             if len(attrs) > 0 and attrs[0][0] == "href" and attrs[0][1].endswith("?tab=reputation"):
  27.                 self.repu_started = True
  28.  
  29.         if tag == "h1":
  30.             if len(attrs) > 0 and attrs[0][0] == "id" and attrs[0][1] == "user-displayname":
  31.                 self.username_started = True
  32.  
  33.     def handle_endtag(self, tag):
  34.         if tag == "span":
  35.             self.span_started = False
  36.  
  37.         if tag == "a":
  38.             self.repu_started = False
  39.  
  40.         if tag == "h1":
  41.             self.username_started = False
  42.  
  43.     def handle_data(self, data):
  44.         if self.username_started:
  45.             self.username += data
  46.  
  47.         if self.repu_started:
  48.             self.repu = int(data)
  49.  
  50.     def handle_charref(self, name):
  51.         if name.startswith('x'):
  52.             c = unichr(int(name[1:], 16))
  53.         else:
  54.             c = unichr(int(name))
  55.         if self.username_started:
  56.             self.username += c
  57.  
  58. def main():
  59.     global user_id
  60.  
  61.     if len(sys.argv) > 1:
  62.         if sys.argv[1] == "-h":
  63.             print "%s [userid=%d]" % (sys.argv[0], user_id)
  64.             return
  65.         user_id = int(sys.argv[1])
  66.    
  67.     so_user_url = so_base_url + str(user_id)
  68.     parser = SOParser()
  69.     parser.feed(urllib2.urlopen(so_user_url).read())
  70.     print "Username:", parser.username
  71.     print "Reputation:", parser.repu
  72.  
  73. if __name__ == "__main__":
  74.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement