Created
January 23, 2020 00:23
-
-
Save bcantoni/a9a689b441330d83ab74ea859b76e06d to your computer and use it in GitHub Desktop.
Revisions
-
Brian Cantoni created this gist
Jan 23, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,82 @@ #!/usr/bin/env python ''' By just doing a HEAD operation, we can look at the location returned and extract game system and title: $ curl -i --head "https://www.pricecharting.com/search-products?type=videogames&q=096427015055" HTTP/2 307 content-type: text/html; charset=utf-8 location: https://www.pricecharting.com/game/nintendo-ds/cooking-mama-2-dinner-with-friends?q=096427015055 x-appengine-log-flush-count: 0 x-cloud-trace-context: 95201b263e270f913954a519333070f4 date: Fri, 03 Jan 2020 18:11:43 GMT server: Google Frontend ''' import csv import os import platform import re import requests import requests_cache import time from titlecase import titlecase def abbreviations(word, **kwargs): if word == 'xbox': return("XBox") if word == 'ds': return("DS") if word == 'gamecube': return("GameCube") return None def extract_title(url): ''' from a pricecharting.com game URL, extract the game system and title ''' matches = re.search(r"pricecharting\.com/game/(.*)/(.*)\?", url) if not matches: return False # remove dashes and change to title case to be more readable gamesystem = titlecase(re.sub(r"-", r" ", matches[1]), callback=abbreviations) title = titlecase(re.sub(r"-", r" ", matches[2]), callback=abbreviations) return(gamesystem, title) def lookup_barcode(barcode): ''' do a head request to pricecharting.com to convert barcode number into a game title ''' url = "https://www.pricecharting.com/search-products?type=videogames&q={}".format(barcode) req = requests.head(url) assert req.status_code == 307 loc = req.headers['Location'] if re.search(r"category=no-results", loc): return("unknown", "unknown", "unknown") (gamesystem, title) = extract_title(loc) return(gamesystem, title, loc) if __name__ == '__main__': requests_cache.install_cache('barcode') filename = "{}.csv".format(int(time.time())) with open(filename, mode='w') as csv_file: csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) csv_writer.writerow(['System', 'Title', 'Barcode', 'Link']) barcode = '' while barcode != 'q': barcode = input("scan a game, or q to quit: ") if barcode != 'q': (gamesystem, title, url) = lookup_barcode(barcode) print("{} - {}".format(gamesystem, title)) csv_writer.writerow([gamesystem, title, "=\"" + str(barcode) + "\"", url]) if platform.system() == 'Darwin': os.system("say -r 300 '{}'".format(title)) print("Done - see file {} for results".format(filename)) 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ Barcode Scanning Used Games This is a pair of Python scripts for looking up video game titles from UPC barcodes. Built and tested with Python 3.8 on macOS, but should probably work anywhere. Additional packages may be required: pip install requests requests_cache titlecase The scripts included here are: * pricecharting.py - uses https://www.pricecharting.com, good for console games but not PC games * upcitemdb.py - uses published API https://www.upcitemdb.com/api/explorer#!/lookup/get_trial_lookup, good for 100 requests/day for free 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,77 @@ #!/usr/bin/env python ''' Use upcitemdb.com API to search games by barcode: https://www.upcitemdb.com/api/explorer#!/lookup/get_trial_lookup Trial calls available without registration and work for up to 100 requests/day. $ curl --silent https://api.upcitemdb.com/prod/trial/lookup?upc=014633190366 | jq . { "code": "OK", "total": 1, "offset": 0, "items": [ { "ean": "0014633190366", "title": "Battlefield Bad Company 2 (PC DVD)", "description": "Players can compete in 4-pla..." } ... } ''' import csv import os import platform import requests import requests_cache import time def lookup_barcode(barcode): ''' use upcitemdb.com to convert barcode number into a game title ''' url = "https://api.upcitemdb.com/prod/trial/lookup?upc={}".format(barcode) req = requests.get(url) if req.status_code != requests.codes.ok: return("unknown", "unknown") data = req.json() total = data['total'] item = data['items'][0] title = item['title'] if total <= 0: return("unknown", "unknown") if 'asin' in item: asin = item['asin'] if 'upc' in item: upc = item['upc'] else: upc = barcode loc = "https://www.upcitemdb.com/upc/{}".format(upc) return(title, loc) if __name__ == '__main__': requests_cache.install_cache('barcode') filename = "{}.csv".format(int(time.time())) with open(filename, mode='w') as csv_file: csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) csv_writer.writerow(['Title', 'Barcode', 'Link']) barcode = '' while barcode != 'q': barcode = input("scan a game, or q to quit: ") if barcode != 'q': (title, url) = lookup_barcode(barcode) print("{}".format(title)) csv_writer.writerow([title, "=\"" + str(barcode) + "\"", url]) if platform.system() == 'Darwin': os.system("say -r 300 '{}'".format(title)) print("Done - see file {} for results".format(filename))