Skip to content

Instantly share code, notes, and snippets.

@woctezuma
Last active July 15, 2023 20:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woctezuma/a8a9cbde6b03868b8631d2f436bbcfab to your computer and use it in GitHub Desktop.
Save woctezuma/a8a9cbde6b03868b8631d2f436bbcfab to your computer and use it in GitHub Desktop.
Respect rate-limits of SteamSpy API
import json
import time
from pathlib import Path
import steamspypi
def get_cooldown():
cooldown = 70 # 1 minute plus a cushion
return cooldown
def get_some_sleep():
cooldown = get_cooldown()
print("Sleeping for {} seconds on {}".format(cooldown, time.asctime()))
time.sleep(cooldown)
return
def download_a_single_page(page_no=0):
print("Downloading page={} on {}".format(page_no, time.asctime()))
data_request = dict()
data_request["request"] = "all"
data_request["page"] = str(page_no)
data = steamspypi.download(data_request)
return data
def get_file_name(page_no):
# Get current day as yyyymmdd format
date_format = "%Y%m%d"
current_date = time.strftime(date_format)
file_name = "{}_steamspy_page_{}.json".format(current_date, page_no)
return file_name
def download_all_pages(num_pages):
# Download
for page_no in range(num_pages):
file_name = get_file_name(page_no)
if not Path(file_name).is_file():
page_data = download_a_single_page(page_no=page_no)
with open(file_name, "w", encoding="utf8") as f:
json.dump(page_data, f)
if page_no != (num_pages - 1):
get_some_sleep()
# Aggregate
data = dict()
for page_no in range(num_pages):
file_name = get_file_name(page_no)
with open(file_name, "r", encoding="utf8") as f:
page_data = json.load(f)
data.update(page_data)
return data
if __name__ == "__main__":
# TODO: one would have to figure out the number of pages, it should be close to 40 as of August 2020.
data = download_all_pages(num_pages=40)
@kphanipavan
Copy link

Update to the number of pages as of 13th April, 2022: there are 53 Pages

@Ahmed-E-86
Copy link

Update to the number of pages as of 13th April, 2022: there are 53 Pages

How many pages now?

@woctezuma
Copy link
Author

woctezuma commented Jul 14, 2023

You can run the script with a larger number and figure out the correct number of pages: i) it should not be much more since it increased from 40 in August 2020 to 53 in April 2022 and we are in July 2023, ii) except for the last page, a page consists of 1000 games.

@Ahmed-E-86
Copy link

It is 64 pages now.

@woctezuma
Copy link
Author

Thank you for the info!

@Ahmed-E-86
Copy link

Ahmed-E-86 commented Jul 15, 2023

You forgot to add a command to save the final JSON file after connecting all the pages together. Moreover, adding the date to the downloaded files make them unusable after running the script in the next day. Furthermore, there are some lines of code that can be removed, and the script will run the same.

@woctezuma
Copy link
Author

woctezuma commented Jul 15, 2023

You forgot to add a command to save the final JSON file after connecting all the pages together.

  1. It is not important to save the aggregate as pages can be aggregated again really fast after they have been saved to disk.

Moreover, adding the date to the downloaded files make them unusable after running the script in the next day.

  1. I had in mind to download data from scratch on different days. If you want to download data only once and then reuse the result on different days, you could save the aggregate as you pointed in 1), and you can choose a filename of your choosing for the saved aggregate.

Furthermore, there are some lines of code that can be removed, and the script will run the same.

  1. This is an example with readability in mind. The limiting factor for performance is SteamSpy's rate-limit, so code cannot be optimized much.

@Ahmed-E-86
Copy link

First, thanks for the module and the gist, they do the job perfectly.

Is there another an API that can be used to retrieve data like "executable", "arguments", and "steam_deck_compatibility". The only thing that I have found so far is https://steamdb.info/ which does not have an API, and prohibit web scraping, and steamcmd does not work with games that are not in my library.

@woctezuma
Copy link
Author

woctezuma commented Jul 15, 2023

Is there another an API that can be used to retrieve data like "executable", "arguments", and "steam_deck_compatibility"?

I don't know for "executable" or "arguments". For "steam_deck_compatibility", you may be able to use resolved_category from:
https://store.steampowered.com/saleaction/ajaxgetdeckappcompatibilityreport?nAppID=570

@Ahmed-E-86
Copy link

I will figure something. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment