Last active
March 30, 2022 18:12
-
-
Save CptSpaceToaster/fb23e7b536fc9b4c1d700827275ff0d0 to your computer and use it in GitHub Desktop.
Crawls the v2 pokemontcg.io API and prints card prices to console
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 # pip install requests | |
# Getting a POKEMONTCG_IO_API_KEY is optional. | |
# Read more about the API's use of keys here: https://docs.pokemontcg.io/getting-started/authentication | |
# https://able.bio/rhett/how-to-set-and-get-environment-variables-in-python--274rgt5 | |
POKEMONTCG_IO_API_KEY = os.environ.get('POKEMONTCG_IO_API_KEY') | |
POKEMONIO_API_URL = 'https://api.pokemontcg.io' | |
POKEMONIO_API_VERSION = 'v2' | |
def get_all_sets(): | |
page = 1 | |
all_sets = [] | |
set_response_json = {} | |
while len(all_sets) == 0 or len(all_sets) < set_response_json['totalCount']: | |
# Make a request to the API for every set | |
response = session.get(f"{POKEMONIO_API_URL}/{POKEMONIO_API_VERSION}/sets?page={page}&pageSize=250") | |
if response.status_code != 200: | |
print(f"The API returned a non-200 response code: {response.status_code}") | |
return [] | |
# Get access to the JSON data, which returns a python dictionary | |
set_response_json = response.json() | |
print(f" Got {set_response_json['count']} sets") | |
all_sets += set_response_json['data'] | |
# If we don't have every card yet, we might need to request the next page of data. | |
page += 1 | |
return all_sets | |
def get_all_cards_for_set(set): | |
page = 1 | |
all_cards_in_set = [] | |
card_response_json = {} | |
while len(all_cards_in_set) == 0 or len(all_cards_in_set) < card_response_json['totalCount']: | |
# Make a request to the API for every pokemon card in the set | |
response = session.get(f"{POKEMONIO_API_URL}/{POKEMONIO_API_VERSION}/cards?q=set.id:{set['id']}&page={page}&pageSize=250") | |
if response.status_code != 200: | |
print(f"The API returned a non-200 response code: {response.status_code}") | |
return [] | |
# Get access to the JSON data, which returns a python dictionary | |
card_response_json = response.json() | |
print(f" Got {card_response_json['count']} cards") | |
all_cards_in_set += card_response_json['data'] | |
# If we don't have every card yet, we might need to request the next page of data. | |
page += 1 | |
# Return a list of dictionaries | |
return all_cards_in_set | |
if __name__ == '__main__': | |
session = requests.Session() | |
if POKEMONTCG_IO_API_KEY is not None: | |
session.headers.update({'X-Api-Key': POKEMONTCG_IO_API_KEY}) | |
print('Getting every set') | |
all_sets = get_all_sets() | |
if all_sets == []: | |
print('There was an error getting every set') | |
exit(1) | |
for set in all_sets: | |
print(f"Getting cards for {set['name']} {set['id']}") | |
all_cards = get_all_cards_for_set(set) | |
if all_cards == []: | |
print(f"There was an error getting cards for set: {set['name']} {set['id']}") | |
for card in all_cards: | |
if card.get('tcgplayer') is not None and card['tcgplayer'].get('prices') is not None: | |
for type_of_price in card['tcgplayer']['prices'].items(): | |
print(f" {card['name']} {card['number']}/{card['set']['printedTotal']} {card['id']} {type_of_price}") | |
print(f" updated at: {card['tcgplayer']['updatedAt']}") | |
else: | |
print(f" Prices are not available for {card['name']} {card['number']}/{card['set']['printedTotal']} {card['id']}") | |
# Remove this break to loop through every set | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment