File: gog_gameid.py

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (56 lines) | stat: -rw-r--r-- 2,082 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3

# This script takes the game name as parameter and returns the GOG Galaxy game id
# Return with exit code 127 if no game was found for the given title
# Return with exit code 1 if there are multiple games for the given name
# Return with exit code 0 if there is exactly one match found

import requests
import argparse
import urllib.parse
from requests_html import HTMLSession
import sys

parser = argparse.ArgumentParser()
parser.add_argument('-n', '--name', required=True, help="The GOG Galaxy game name")
parser.add_argument('-a', '--all', action='store_true', help="Show all matches, not just the exact match")
parser.add_argument('-v', '--verbose', action='store_true', help="Also print some meta information next to the GOG Galaxy game id")
args = parser.parse_args()

searchurl = "https://gogdb.org/products?search={0}".format(urllib.parse.quote_plus(args.name, safe='!'))
if args.verbose:
	sys.stderr.write('query url: {0}\n'.format(searchurl))

try:
	session = HTMLSession()
	response = session.get(searchurl)
	game_rows = response.html.xpath("//table[@id='product-table']/tr/td")
	game_columns = 5 # thumb, id, name, type, os
	entries = int(len(game_rows) / game_columns)
	if args.verbose:
		sys.stderr.write('found {0} games\n'.format(entries))

	matches = 0
	for i in range(entries):
		idx               = game_columns * i
		game_thumb        = game_rows[idx + 0].text.strip()
		game_id           = game_rows[idx + 1].text.strip()
		game_name         = game_rows[idx + 2].text.strip()
		game_type         = game_rows[idx + 3].text.strip()
		game_os           = game_rows[idx + 4].text.strip().replace(" ","")
		if not args.all and game_name != args.name:
			if args.verbose:
				sys.stderr.write('found {0} - no match for {1}\n'.format(game_name, args.name))
			continue
		if args.verbose:
			print("{0} {1} {2}#{3}".format(game_id, game_type, game_os, game_name))
		else:
			print("{0}".format(game_id))
		matches += 1

	if matches == 0:
		sys.exit(127)
	if matches > 1:
		sys.exit(1)
except requests.exceptions.RequestException as e:
	print(e)