from datetime import datetime import urllib from dateutil.tz import tzlocal import requests EPIC_SEARCH_URL = "https://store-site-backend-static-ipv4.ak.epicgames.com/freeGamesPromotions?locale=en-US&country=SE&allowCountries=SE" STEAM_SEARCH_URL = "https://store.steampowered.com/search/suggest?term=%s&f=games&cc=SE&realm=1&l=english&use_search_spellcheck=1" STEAM_CHECK_GAME = 'https://api.steampowered.com/IStoreBrowseService/GetItems/v1?input_json={"ids":[{"appid":%s}],"context":{"language":"english","country_code":"SE","steam_realm":1},"data_request":{"include_reviews":true,"include_basic_info":true}}' def get_from_epic(): """Get free games from epic""" freegames = [] now = datetime.now(tzlocal()) resp = requests.get(EPIC_SEARCH_URL) # TODO: error handling in case request fails json_data = resp.json() for game in json_data.get("data", {}).get("Catalog", {}).get("searchStore", {}).get("elements", []): if game.get("promotions"): for offer in game.get("promotions", {}).get("promotionalOffers", []): for promo in offer.get("promotionalOffers"): # Check if promo is valid enddate = datetime.strptime(promo.get("endDate"), "%Y-%m-%dT%H:%M:%S.%f%z") startdate = datetime.strptime(promo.get("startDate"), "%Y-%m-%dT%H:%M:%S.%f%z") is_valid = enddate >= now >= startdate is_free = promo.get("discountSetting", {}).get("discountPercentage", -1) == 0 if is_valid and is_free: fg = {"title": game.get("title", "")} fg["image"] = game.get("keyImages", [{}])[0].get("url", "") freegames.append(fg) return freegames def get_from_steam(freegames): """Get info for freegames from Steam.""" # Info från Martin # Man kan använda denna och parsa HTMLen: # https://store.steampowered.com/search/suggest?term=impostor+factory&f=games&cc=SE&realm=1&l=english&use_search_spellcheck=1 # Kolla upp betyg och beskrivning för detta ID: # https://api.steampowered.com/IStoreBrowseService/GetItems/v1?input_json={"ids":[{"appid":1182620}],"context":{"language":"english","country_code":"SE","steam_realm":1},"data_request":{"include_reviews":true,"include_basic_info":true}} for gamedict in freegames: searchstring = urllib.parse.quote(gamedict["title"]) resp = requests.get(STEAM_SEARCH_URL % searchstring) # TODO: error handling in case request fails resp_html = resp.content print(gamedict["title"]) print(resp.content) print() # TODO: parse html, e.g. with beautifulsoup (https://beautiful-soup-4.readthedocs.io/en/latest/) to get the first hit, get its ID from "data-ds-appid" # TODO: make get request for STEAM_CHECK_GAME % ID and get reviews etc and store it in freegames dict def build_email_body(freegames): pass # TODO: build nice email html body def send_email(body): pass # TODO: do something like in https://github.com/anne17/bevaka-tandvard/blob/main/bevakning.py#L125 if __name__ == "__main__": freegames = get_from_epic() get_from_steam(freegames) # TODO: do some stuff to avoid multiple notifications for the same games email_body = build_email_body(freegames) send_email(email_body)