Skip to content

Commit

Permalink
Get price via Steam BigPicture API
Browse files Browse the repository at this point in the history
It is more reliable than web scraping (in most cases)
  • Loading branch information
HeroCC committed Sep 5, 2018
1 parent ced5b50 commit 258c8a5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
34 changes: 14 additions & 20 deletions SteamGame.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import json

import requests
from bs4 import BeautifulSoup
Expand All @@ -8,15 +9,17 @@ class SteamGame:

def __init__(self, appid):
self.appID = appid
self.url = 'http://store.steampowered.com/app/' + appid
self.url = 'https://store.steampowered.com/app/' + appid
self.gamePage = BeautifulSoup(
requests.get(self.url, cookies={'birthtime': '640584001', 'lastagecheckage': '20-April-1990',
'mature_content': '1'}).text,
"html.parser")
self.json = json.loads(requests.get("https://store.steampowered.com/api/appdetails/?appids=" + appid + "&cc=us")
.text)[appid]["data"]

self.title = self.gamePage.title.string.replace(" on Steam", "")
self.discountamount = self.discountamount()
self.price = self.getprice(False)
self.price = self.getprice()
self.achievements = self.getachev()
self.cards = self.getcards()
self.unreleased = self.isunreleased()
Expand All @@ -30,27 +33,18 @@ def discountamount(self):
else:
return False

def getprice(self, withoutdiscount: bool):
price = self.gamePage.find("div", class_="game_purchase_price")
ogPrice = self.gamePage.find("div", class_="discount_original_price")
discountprice = self.gamePage.find("div", class_="discount_final_price")

if self.isfree(): return "Free"

if discountprice is not None:
return discountprice.string.strip()
elif withoutdiscount and ogPrice is not None:
return ogPrice.string.strip()
elif price is not None:
return price.string.strip()
else:
def getprice(self):
if len(self.json["package_groups"]) is 0:
return "Free"

return "$" + str(self.json["package_groups"]
[0]
["subs"]
[0]
["price_in_cents_with_discount"] / 100)

def isfree(self):
cartButton = self.gamePage.find("div", class_="btn_addtocart")

if cartButton is not None:
return cartButton.text.strip() in ("Play Game", "Install now")
return self.json["is_free"]

def getachev(self):
achblock = self.gamePage.find("div", id="achievement_block")
Expand Down
1 change: 0 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def buildcommenttext(g):
else:
commenttext += ' * Currently is ' + g.price
if not g.isfree(): commenttext += ' USD'
if g.price != g.getprice(True): commenttext += ' (from ' + g.getprice(True) + ')'
commenttext += '\n'
if g.isfree(): commenttext += ' * Can be added to ASF clients with `!addlicense asf ' + g.appID + '`\n'
if g.discountamount is not False: commenttext += ' * Is currently discounted ' + g.discountamount + '\n'
Expand Down

0 comments on commit 258c8a5

Please sign in to comment.