Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from lxml.html import tostring, html5parser
  4.  
  5. namespaces = {'html': 'http://www.w3.org/1999/xhtml'}
  6.  
  7. def parsetable(url, path='//html:table'):
  8.     html = html5parser.parse(url)
  9.     table = html.find(path, namespaces=namespaces)
  10.    
  11.     headers = [header.text for header in table.findall('html:thead/html:tr/html:th', namespaces=namespaces)]
  12.     rows = table.findall('html:tbody/html:tr', namespaces=namespaces)
  13.                                                                                                                  
  14.     return [dict(zip(headers, [tostring(col, encoding=str, method='text') for col in row])) for row in iter(rows)]
  15.    
  16.  
  17. def tagfilter(tags, gender, role, blacklist = []):
  18.     tags = iter(tags)
  19.     tag, thresh = next(tags)
  20.  
  21.     table = parsetable('http://rp.aryion.com/characters.php?term={}'.format(tag))
  22.  
  23.     for row in table:
  24.         if row['Character'] in blacklist:
  25.             continue
  26.         if gender != row['Gender'] or int(row[role]) < thresh:
  27.             blacklist.append(row['Character'])
  28.  
  29.     try:
  30.         return tagfilter(tags, gender, role, blacklist)
  31.     except StopIteration:
  32.         return [row['Character'] for row in table if row['Character'] not in blacklist]
  33.  
  34. from sys import argv
  35.  
  36. args = argv
  37.  
  38. argv.pop(0)
  39.  
  40. gender = { 'female': 'F',
  41.             'f': 'F',
  42.             'fem': 'F',
  43.             'male': 'M',
  44.             'm': 'M',
  45.             'herm': 'H',
  46.             'h': 'H',
  47.             'undefined': 'U',
  48.             'u': 'U' }[argv.pop(0)]
  49.  
  50. role = {    'prey': 'For prey',
  51.             'pred': 'For pred' }[argv.pop(0)]
  52.  
  53. tags = []
  54.  
  55. while len(argv) > 1:
  56.     tag = argv.pop(0)
  57.     thresh = int(argv.pop(0))
  58.     tags.append((tag, thresh))
  59.  
  60.  
  61. print(tags)
  62.  
  63. for i in tagfilter(tags, gender, role):
  64.     print('https://profile.aryion.com/profile/{}'.format(i))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement