Advertisement
tiin57

findnumpty.py

Jun 22nd, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. # Probably not the best quality code
  2. # but it's fun, so who cares :)
  3. # Written by Tiin57
  4. # Don't remove that, okay thanks
  5.  
  6. import requests, re
  7. from lxml import html
  8. from random import randint
  9.  
  10. template = '''
  11. <center>
  12.     <span id="%s" class="%s" style="%s font-size: 96px;">%s<br></span><br>
  13.     <span style="font-size: 36px;">is a numpty</span>
  14. </center>\n
  15. '''
  16.  
  17. whitelist = ('Tiin57', 'clolin01', 'Trex')
  18. def ternary(cond, iftrue, iffalse):
  19.     if (cond):
  20.         return iftrue
  21.     else:
  22.         return iffalse
  23. def check_text(ele):
  24.     if ele.text != None:
  25.         scheck = ele.getparent().get('style')
  26.         return [ele.text, ternary(scheck, scheck, ele.get('style')), ele.getparent().getparent().tag == 'strong']
  27.     if len(ele.getchildren()) != 0:
  28.         return check_text(ele.getchildren()[0])
  29. def get_numpty(numpties):
  30.     n = numpties[randint(0, len(numpties) - 1)]
  31.     if n[0] in whitelist:
  32.         return get_numpty(numpties)
  33.     return n
  34. def index(req):
  35.     findnumpty(req)
  36. def findnumpty(req):
  37.     req.content_type = 'text/html'
  38.     req.write('''
  39.         <style>
  40.         body {
  41.             font-family: Tahoma, Geneva, Sans-Serif;
  42.             font-size: 13px;
  43.         }
  44.         .bold {
  45.             font-weight: bold
  46.         }
  47.         </style>
  48.         ''')
  49.     tree = html.fromstring(requests.get('http://forums.hexteria.com/showteam.php').text)
  50.     elements = []
  51.     numpties = []
  52.     for i in tree.cssselect('td[width="60%"]'):
  53.         elements.append(i)
  54.     for i in elements:
  55.         x = check_text(i)
  56.         if x == None:
  57.             continue
  58.         text = x[0]
  59.         style = x[1]
  60.         isstrong = x[2]
  61.         if text == '' or text.startswith(' start: ') or re.match(r'/[\s]+/', text):
  62.             continue
  63.         numpties.append(x)
  64.     winner = get_numpty(numpties)
  65.     req.write('<title>%s is a numpty</title>' % winner[0])
  66.     req.write(template % (winner[0], ternary(winner[2], 'bold', ''), winner[1], winner[0]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement