Advertisement
tiin57

count.py

Jul 16th, 2014
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 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 style="font-size: 36px;">There are %s staff on the Cube.</span>
  13. </center>\n
  14. '''
  15.  
  16. def ternary(cond, iftrue, iffalse):
  17.     if (cond):
  18.         return iftrue
  19.     else:
  20.         return iffalse
  21. def check_text(ele):
  22.     if ele.text != None:
  23.         scheck = ele.getparent().get('style')
  24.         return [ele.text, ternary(scheck, scheck, ele.get('style')), ele.getparent().getparent().tag == 'strong']
  25.     if len(ele.getchildren()) != 0:
  26.         return check_text(ele.getchildren()[0])
  27. def get_numpty(numpties):
  28.     n = numpties[randint(0, len(numpties) - 1)]
  29.     if n[0] in whitelist:
  30.         return get_numpty(numpties)
  31.     return n
  32. def index(req):
  33.     findnumpty(req)
  34. def findnumpty(req):
  35.     req.content_type = 'text/html'
  36.     req.write('''
  37.         <style>
  38.         body {
  39.             font-family: Tahoma, Geneva, Sans-Serif;
  40.             font-size: 13px;
  41.         }
  42.         .bold {
  43.             font-weight: bold
  44.         }
  45.         </style>
  46.         ''')
  47.     tree = html.fromstring(requests.get('http://forums.thecubeserver.org/showteam.php').text)
  48.     elements = []
  49.     numpties = []
  50.     for i in tree.cssselect('.trow1'):
  51.         elements.append(i)
  52.     for i in tree.cssselect('.trow2'):
  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.     req.write(template % str(len(numpties)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement