This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/disabled_stuff/steam.py

76 lines
2.7 KiB
Python
Raw Permalink Normal View History

import re
2014-02-14 04:36:57 +01:00
from bs4 import BeautifulSoup, NavigableString, Tag
2014-02-12 23:48:33 +01:00
from util import hook, http, web
from util.text import truncate_str
steam_re = (r'(.*:)//(store.steampowered.com)(:[0-9]+)?(.*)', re.I)
2013-08-01 12:07:20 +02:00
def get_steam_info(url):
page = http.get(url)
soup = BeautifulSoup(page, 'lxml', from_encoding="utf-8")
2014-02-12 23:48:33 +01:00
data = {}
2014-02-12 23:48:33 +01:00
data["name"] = soup.find('div', {'class': 'apphub_AppName'}).text
data["desc"] = truncate_str(soup.find('meta', {'name': 'description'})['content'].strip(), 80)
2014-02-12 23:48:33 +01:00
# get the element details_block
details = soup.find('div', {'class': 'details_block'})
2014-02-12 23:57:10 +01:00
# loop over every <b></b> tag in details_block
2014-02-12 23:48:33 +01:00
for b in details.findAll('b'):
2014-02-12 23:57:10 +01:00
# get the contents of the <b></b> tag, which is our title
2014-02-12 23:48:33 +01:00
title = b.text.lower().replace(":", "")
if title == "languages":
# we have all we need!
break
2014-02-12 23:57:10 +01:00
# find the next element directly after the <b></b> tag
2014-02-14 05:03:08 +01:00
next_element = b.nextSibling
if next_element:
2014-02-12 23:57:10 +01:00
# if the element is some text
2014-02-14 05:03:08 +01:00
if isinstance(next_element, NavigableString):
text = next_element.string.strip()
2014-02-12 23:48:33 +01:00
if text:
2014-02-12 23:57:10 +01:00
# we found valid text, save it and continue the loop
2014-02-12 23:48:33 +01:00
data[title] = text
continue
2014-02-12 23:57:10 +01: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 05:03:08 +01:00
next_element = next_element.find_next('a', href=True)
2014-02-12 23:48:33 +01:00
2014-02-12 23:57:10 +01:00
# if the element is an <a></a> tag
2014-02-14 05:03:08 +01:00
if isinstance(next_element, Tag) and next_element.name == 'a':
text = next_element.string.strip()
2014-02-12 23:48:33 +01:00
if text:
2014-02-12 23:57:10 +01:00
# we found valid text (in the <a></a> tag),
# save it and continue the loop
2014-02-12 23:48:33 +01:00
data[title] = text
continue
data["price"] = soup.find('div', {'class': 'game_purchase_price price'}).text.strip()
return u"\x02{name}\x02: {desc}, \x02Genre\x02: {genre}, \x02Release Date\x02: {release date}," \
u" \x02Price\x02: {price}".format(**data)
2013-08-01 12:07:20 +02: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 12:17:41 +02:00
def steam(inp):
"""steam [search] - Search for specified game/trailer/DLC"""
2013-08-01 12:07:20 +02: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'])