Skip to content

Instantly share code, notes, and snippets.

@mindsocket
Created July 11, 2012 01:56
Show Gist options
  • Save mindsocket/3087448 to your computer and use it in GitHub Desktop.
Save mindsocket/3087448 to your computer and use it in GitHub Desktop.
WUGC_AUscorebot
'''
Created on 08/07/2012
@author: roger
'''
import requests
from lxml import etree
import lxml. html
import memcache
from twitter.oauth import OAuth
from twitter.api import Twitter
import re
mc = memcache.Client(['127.0.0.1:11211'], debug=0)
url="http://wugc2012.org/fdsys/pscore/scv_search_tn/search"
t = Twitter(auth=OAuth('secret', 'secret', 'secret', 'secret'))
all_text = lambda element: lxml.html.tostring(element, method="text", encoding=unicode)
team_mapping = {
"Women's": "@Firetails2012",
"Women Masters": "@FlyingFoxes2012",
"Open": "@Dingoes2012",
"Masters": "Wombats",
"Mixed": "@Barramundis2012",
"Guts": "@Ironbarks2012",
}
def wugcbot(url, parms):
key = 'wugcsearch'
page = mc.get(key)
if not page:
searchpage = requests.post(url, parms, headers={ 'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.26 Safari/535.11' })
page = searchpage.content
mc.set(key, page, time=5*60)
html = lxml.html.fromstring(page)
'''<a href="#" class="Select_Link" onclick="document.location = 'http://wugc2012.org/fdsys/pscore/scv_match/open/556?lang=en'; return false;">[NowPlaying]</a>'''
current_games = html.xpath("//a[contains(text(),'NowPlaying')]")
game_set = set()
for game in current_games:
game_url = game.get('onclick').split("'")[1]
game_id = game_url.split('/')[-2] + '/' + game_url.split('/')[-1].split('?')[0]
game_set.add(game_id)
for game in game_set:
gamepage = requests.get("http://wugc2012.org/fdsys/pscore/scv_match/%s?lang=en" % game, headers={ 'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.26 Safari/535.11' })
page = gamepage.content
html = lxml.html.fromstring(page)
try:
titlecell = html.cssselect('td.CM.TG_VSTITLE')[0]
except IndexError:
print page
raise
titlerowtextlist = titlecell.getparent().text_content().split()
division = html.cssselect('#breadcrumb a')[1].text_content().split(':')[1][:-1]
gutsscore = [el.text for el in html.cssselect('.TG_GUTS_SET_SEL')]
if len(gutsscore):
game_text = ' '.join(titlerowtextlist + ["[Guts]"]).replace('0 VS 0', gutsscore[0][1:-1].split()[0])
division = "Guts"
else:
game_text = ' '.join(titlerowtextlist + ["[%s]" % division])
event_rows = html.cssselect('#events tr')
for row in reversed(event_rows[1:6]):
cells = [cell.text_content() for cell in row.cssselect('td')]
if cells[4] == "Goal":
event_text = ' '.join((cells[4], cells[0] or '', cells[1] or cells[7] or '', "to", cells[2] or cells[6] or ''))
elif cells[4] in ["GoodThrow", "ThrowMiss"]:
event_text = ' '.join((cells[4], cells[1] or cells[7] or ''))
elif cells[4] in ["FollowCatch", "CleanCatch"]:
continue
c1 = cells[2] if not '---' in cells[2] else ''
c2 = cells[6] if not '---' in cells[6] else ''
event_text = ' '.join((cells[4], cells[1] or cells[7] or '', "caught by", c1 or c2 or ''))
else:
event_text = ' '.join((cells[4], cells[0] or ''))
tweet = ' '.join((event_text, '-', game_text, '#wugc2012'))
if division in team_mapping and cells[4] in ["Goal", "GoodThrow"]:
if ((titlerowtextlist[0] == "Australia" and cells[1]) or
(titlerowtextlist[4] == "Australia" and cells[7])):
tweet = tweet + ' Go ' + team_mapping[division] + '!'
key = ''.join(event_text.split() + re.sub('[0-9]+', 'x', game_text).split())
doneit = mc.get(key)
if not doneit:
t.statuses.update(status=tweet)
mc.set(key, True, 12*60*60)
if __name__ == '__main__':
parms={
'tid':2,
'lang':'en',
'team':'Australia',
'ymd':'',
'field':'',
}
wugcbot(url, parms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment