Created
December 19, 2024 15:37
-
-
Save bbroke/38a55953aef117c8e7e5956d743ad95c to your computer and use it in GitHub Desktop.
Download all TIC-80 games from https://tic80.com/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
from bs4 import BeautifulSoup | |
BASE_URL = "https://tic80.com" | |
CATEGORY = 0 # "cat=0" are games, change for other stuff, see https://tic80.com/play?cat=0&sort=2 | |
MAX_PAGES = 40 # change to maximum number of pages in selected CATEGORY | |
DOWNLOAD_DIR = "tic80_games" | |
def create_download_dir(): | |
if not os.path.exists(DOWNLOAD_DIR): | |
os.makedirs(DOWNLOAD_DIR) | |
def get_cartridge_links(category_url): | |
response = requests.get(category_url) | |
if response.status_code != 200: | |
print(f"Failed to access {category_url}") | |
return [] | |
soup = BeautifulSoup(response.text, "html.parser") | |
cartridge_links = [] | |
# Find all cartridge links | |
for a_tag in soup.select("a[href^='/play?cart']"): | |
cartridge_links.append(BASE_URL + a_tag['href']) | |
return cartridge_links | |
def download_cartridge(cartridge_url): | |
response = requests.get(cartridge_url) | |
if response.status_code != 200: | |
print(f"Failed to access {cartridge_url}") | |
return | |
soup = BeautifulSoup(response.text, "html.parser") | |
download_button = soup.find("a", text="download cartridge") | |
if not download_button: | |
print(f"No download button found for {cartridge_url}") | |
return | |
download_url = BASE_URL + download_button['href'] | |
cartridge_title = soup.find("h1").text | |
download_url = BASE_URL + download_button['href'] | |
filename = os.path.basename(download_button['href']) # Extract the original filename from the URL | |
file_path = os.path.join(DOWNLOAD_DIR, filename) | |
# Download the cartridge | |
response = requests.get(download_url) | |
if response.status_code == 200: | |
with open(file_path, "wb") as file: | |
file.write(response.content) | |
print(f"Downloaded {cartridge_title}") | |
else: | |
print(f"Failed to download {cartridge_title}") | |
def main(): | |
create_download_dir() | |
for page in range(MAX_PAGES): | |
category_url = f"{BASE_URL}/play?cat={CATEGORY}&sort=2&page={page}" | |
print(f"Fetching links from Page {page + 1}/{MAX_PAGES} ...") | |
cartridge_links = get_cartridge_links(category_url) | |
print(f"Found {len(cartridge_links)} cartridges. Starting download...") | |
for cartridge_url in cartridge_links: | |
download_cartridge(cartridge_url) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment