2013-09-05 18:17:05 +08:00
import re
2014-02-14 16:36:57 +13:00
from bs4 import BeautifulSoup , NavigableString , Tag
2014-02-13 11:48:33 +13:00
from util import hook , http , web
from util . text import truncate_str
2013-09-05 18:17:05 +08:00
2012-11-08 21:56:56 +13:00
2013-08-01 01:10:48 +12:00
steam_re = ( r ' (.*:)//(store.steampowered.com)(:[0-9]+)?(.*) ' , re . I )
2013-08-01 18:07:20 +08:00
def get_steam_info ( url ) :
page = http . get ( url )
2013-08-01 01:10:48 +12:00
soup = BeautifulSoup ( page , ' lxml ' , from_encoding = " utf-8 " )
2014-02-13 11:48:33 +13:00
data = { }
2013-08-01 01:10:48 +12:00
2014-02-13 11:48:33 +13:00
data [ " name " ] = soup . find ( ' div ' , { ' class ' : ' apphub_AppName ' } ) . text
2014-02-13 12:00:33 +13:00
data [ " desc " ] = truncate_str ( soup . find ( ' meta ' , { ' name ' : ' description ' } ) [ ' content ' ] . strip ( ) , 80 )
2013-08-01 01:10:48 +12:00
2014-02-13 11:48:33 +13:00
# get the element details_block
details = soup . find ( ' div ' , { ' class ' : ' details_block ' } )
2014-02-13 11:57:10 +13:00
# loop over every <b></b> tag in details_block
2014-02-13 11:48:33 +13:00
for b in details . findAll ( ' b ' ) :
2014-02-13 11:57:10 +13:00
# get the contents of the <b></b> tag, which is our title
2014-02-13 11:48:33 +13:00
title = b . text . lower ( ) . replace ( " : " , " " )
if title == " languages " :
# we have all we need!
break
2014-02-13 11:57:10 +13:00
# find the next element directly after the <b></b> tag
2014-02-14 17:03:08 +13:00
next_element = b . nextSibling
if next_element :
2014-02-13 11:57:10 +13:00
# if the element is some text
2014-02-14 17:03:08 +13:00
if isinstance ( next_element , NavigableString ) :
text = next_element . string . strip ( )
2014-02-13 11:48:33 +13:00
if text :
2014-02-13 11:57:10 +13:00
# we found valid text, save it and continue the loop
2014-02-13 11:48:33 +13:00
data [ title ] = text
continue
2014-02-13 11:57:10 +13:00
else :
# the text is blank - sometimes this means there are
# useless spaces or tabs between the <b> and <a> tags.
# so we find the next <a> tag and carry on to the next
# bit of code below
2014-02-14 17:03:08 +13:00
next_element = next_element . find_next ( ' a ' , href = True )
2014-02-13 11:48:33 +13:00
2014-02-13 11:57:10 +13:00
# if the element is an <a></a> tag
2014-02-14 17:03:08 +13:00
if isinstance ( next_element , Tag ) and next_element . name == ' a ' :
text = next_element . string . strip ( )
2014-02-13 11:48:33 +13:00
if text :
2014-02-13 11:57:10 +13:00
# we found valid text (in the <a></a> tag),
# save it and continue the loop
2014-02-13 11:48:33 +13:00
data [ title ] = text
continue
data [ " price " ] = soup . find ( ' div ' , { ' class ' : ' game_purchase_price price ' } ) . text . strip ( )
2014-02-13 15:02:44 +13:00
return u " \x02 {name} \x02 : {desc} , \x02 Genre \x02 : {genre} , \x02 Release Date \x02 : { release date}, " \
u " \x02 Price \x02 : {price} " . format ( * * data )
2013-08-01 18:07:20 +08:00
@hook.regex ( * steam_re )
def steam_url ( match ) :
return get_steam_info ( " http://store.steampowered.com " + match . group ( 4 ) )
@hook.command
2013-08-01 18:17:41 +08:00
def steam ( inp ) :
""" steam [search] - Search for specified game/trailer/DLC """
2013-08-01 18:07:20 +08:00
page = http . get ( " http://store.steampowered.com/search/?term= " + inp )
soup = BeautifulSoup ( page , ' lxml ' , from_encoding = " utf-8 " )
result = soup . find ( ' a ' , { ' class ' : ' search_result_row ' } )
return get_steam_info ( result [ ' href ' ] ) + " - " + web . isgd ( result [ ' href ' ] )