|
#!/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)) |