master 9d13805bbfb0 cached
6 files
30.8 KB
8.6k tokens
12 symbols
1 requests
Download .txt
Repository: AB1908/GOG-Galaxy-Export-Script
Branch: master
Commit: 9d13805bbfb0
Files: 6
Total size: 30.8 KB

Directory structure:
gitextract_v_956o0z/

├── .gitignore
├── LICENSE
├── galaxy_library_export.py
├── helper_scripts/
│   └── print_gameDB.py
├── readme.md
└── settings.json

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitignore
================================================
gameDB.csv
helper_scripts/my_games.txt
.idea/

================================================
FILE: LICENSE
================================================
MIT License

Copyright (c) 2020 AB1908

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


================================================
FILE: galaxy_library_export.py
================================================
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import csv
import json
import re
import sqlite3
import time
from enum import Enum
from os.path import exists
from sys import platform

from natsort import natsorted


class Arguments():
	""" argparse wrapper, to reduce code verbosity """
	__parser = None
	__args = None
	__bAll = False  # Extract all fields

	def __init__(self, args, **kwargs):
		import argparse
		self.__parser = argparse.ArgumentParser(**kwargs)
		for arg in args:
			self.__parser.add_argument(*arg[0], **arg[1])
		self.__args = self.__parser.parse_args()
		self.__bAll = getattr(self.__args, 'all')

	def help(self):
		self.__parser.print_help()

	def anyOption(self, exceptions):
		self.notExportOptions = exceptions
		for k,v in self.__args.__dict__.items():
			if (k not in exceptions) and v:
				return True
		return False

	def extractAll(self):
		self.__bAll = True

	def __getitem__(self, name):
		return self.__getattr__(name)

	def __getattr__(self, name):
		ret = getattr(self.__args, name)
		if isinstance(ret, bool) and (name not in self.notExportOptions):
			ret = self.__bAll or ret
		elif isinstance(ret, list) and (1 == len(ret)):
			ret = ret[0]
		return ret

class Type(Enum):
	""" used to specify the field type while parsing the raw data """
	STRING = 0
	STRING_JSON = 1
	INTEGER = 10
	DATE = 20
	LIST = 30

class Positions(dict):
	""" small dictionary to avoid errors while parsing non-exported field positions """
	def __getitem__(self, key):
		try:
			return dict.__getitem__(self, key)
		except KeyError:
			return None

def extractData(args):
	database_location = args.fileDB
	platforms = {"3do": "3DO Interactive Multiplayer", "3ds": "Nintendo 3DS", "aion": "Aion", "aionl": "Aion: Legions of War", "amazon": "Amazon", "amiga": "Amiga", "arc": "ARC", "atari": "Atari 2600", "battlenet": "Battle.net", "bb": "BestBuy", "beamdog": "Beamdog", "bethesda": "Bethesda.net", "blade": "Blade & Soul", "c64": "Commodore 64", "d2d": "Direct2Drive", "dc": "Dreamcast", "discord": "Discord", "dotemu": "DotEmu", "egg": "Newegg", "elites": "Elite Dangerous", "epic": "Epic Games Store", "eso": "The Elder Scrolls Online", "fanatical": "Fanatical", "ffxi": "Final Fantasy XI", "ffxiv": "Final Fantasy XIV", "fxstore": "Placeholder", "gamehouse": "GameHouse", "gamesessions": "GameSessions", "gameuk": "GAME UK", "generic": "Other", "gg": "GamersGate", "glyph": "Trion World", "gmg": "Green Man Gaming", "gog": "GOG", "gw": "Guild Wars", "gw2": "Guild Wars 2", "humble": "Humble Bundle", "indiegala": "IndieGala", "itch": "Itch.io", "jaguar": "Atari Jaguar", "kartridge": "Kartridge", "lin2": "Lineage 2", "minecraft": "Minecraft", "n64": "Nintendo 64", "ncube": "Nintendo GameCube", "nds": "Nintendo DS", "neo": "NeoGeo", "nes": "Nintendo Entertainment System", "ngameboy": "Game Boy", "nswitch": "Nintendo Switch", "nuuvem": "Nuuvem", "nwii": "Wii", "nwiiu": "Wii U", "oculus": "Oculus", "origin": "Origin", "paradox": "Paradox Plaza", "pathofexile": "Path of Exile", "pce": "PC Engine", "playasia": "Play-Asia", "playfire": "Playfire", "ps2": "PlayStation 2", "psn": "PlayStation Network", "psp": "PlayStation Portable", "psvita": "PlayStation Vita", "psx": "PlayStation", "riot": "Riot", "rockstar": "Rockstar Games Launcher", "saturn": "Sega Saturn", "sega32": "32X", "segacd": "Sega CD", "segag": "Sega Genesis", "sms": "Sega Master System", "snes": "Super Nintendo Entertainment System", "stadia": "Google Stadia", "star": "Star Citizen", "steam": "Steam", "test": "Test", "totalwar": "Total War", "twitch": "Twitch", "unknown": "Unknown", "uplay": "Uplay", "vision": "ColecoVision", "wargaming": "Wargaming", "weplay": "WePlay", "winstore": "Windows Store", "xboxog": "Xbox", "xboxone": "Xbox Live", "zx": "ZX Spectrum PC"}

	def loadOptions():
		""" Loads options from `settings.json` and initialises defaults """
		defaults = {"TreatDLCAsGame": [], "TreatReleaseAsDLC": {}}

		# Load settings from disk
		try:
			with open('settings.json', 'r', encoding='utf-8') as f:
				o = json.load(f)
		except:
			o = {}
		
		# Initialise defaults
		for k, v in defaults.items():
			if k not in o:
				o[k] = v

		return o

	def id(name):
		""" Returns the numeric ID for the specified type """
		return cursor.execute('SELECT id FROM GamePieceTypes WHERE type="{}"'.format(name)).fetchone()[0]

	def clean(s):
		""" Cleans strings for CSV consumption """
		for f in clean.filters:
			s = f.sub('\\\\n', s)  # Convert CRLF, LF, <br> into '\n' string
		return s
	clean.filters = [
		re.compile(r'\s*?(?:\r?\n|<br\s*/?>)')
	]

	def jld(name, bReturnParsed=False, object=None):
		""" json.loads(`name`), optionally returning the purified sub-object of the same name,
		    for cases such as {`name`: {`name`: "string"}}
		"""
		v = json.loads((object if object else result)[positions[name]])
		if not bReturnParsed:
			return v
		v = v[name]
		return clean(v) if isinstance(v, str) else v

	def prepare(resultName, fields, dbField=None, dbRef=None, dbCondition=None, dbCustomJoin=None, dbResultField=None, dbGroupBy=None):
		""" Wrapper around the statement preparation and result parsing\n
			`resultName` cli argument variable name\n
			`fields` {`title` to be inserted in the CSV: `boolean condition`, …}\n
			SELECT `dbField` FROM `dbRef` WHERE `dbCondition`\n
			SELECT `dbResultField` FROM MasterDB GROUP BY `dbGroupBy`
		"""
		# CSV field names
		for name,condition in fields.items():
			if condition:
				fieldnames.append(name)

		if dbField:
			og_fields.append(', {}'.format(dbField))
			# Position of the results
			positions[resultName] = prepare.nextPos
			prepare.nextPos += 1
		if dbRef:
			og_references.append(', {}'.format(dbRef))
		if dbCondition:
			og_conditions.append(' AND ({})'.format(dbCondition))
		if dbCustomJoin:
			og_joins.append(' ' + dbCustomJoin)
		if dbResultField:
			og_resultFields.append(dbResultField)
		if dbGroupBy:
			og_resultGroupBy.append(dbGroupBy)
	
	def includeField(object, columnName, fieldName=None, fieldType=Type.STRING, paramName=None, delimiter=','):
		if args[columnName if not paramName else paramName]:
			if None is fieldName:
				fieldName = columnName
			try:
				if Type.INTEGER is fieldType:
					row[columnName] = round(object[fieldName])
				elif Type.DATE is fieldType:
					row[columnName] = time.strftime("%Y-%m-%d", time.localtime(object[fieldName]))
				elif Type.STRING is fieldType:
					row[columnName] = object[fieldName]
				elif Type.STRING_JSON is fieldType:
					row[columnName] = jld(fieldName, True)
				elif Type.LIST is fieldType:
					s = object[fieldName].split(delimiter)
					row[columnName] = set(s) if 1 < len(s) else objectFieldName
			except:
				row[columnName] = object[fieldName]

	from contextlib import contextmanager
	@contextmanager
	def OpenDB():
		# Prepare the DB connection
		_connection = sqlite3.connect(database_location)
		_cursor = _connection.cursor()

		_exception = None
		try:
			yield _cursor
		except Exception as e:
			_exception = e

		# Close the DB connection
		_cursor.close()
		_connection.close()

		# Re-raise the unhandled exception if needed
		if _exception:
			raise _exception

	# Load options before opening the DB
	options = loadOptions()

	with OpenDB() as cursor:
		# Create a view of ProductPurchaseDates (= purchased/added games) joined on GamePieces for a full owned game data DB
		owned_game_database = """CREATE TEMP VIEW MasterList AS
				SELECT GamePieces.releaseKey, GamePieces.gamePieceTypeId, GamePieces.value FROM ProductPurchaseDates
				JOIN GamePieces ON ProductPurchaseDates.gameReleaseKey = GamePieces.releaseKey;"""

		# Set up default queries and processing metadata, and always extract the game title along with any parameters
		prepare.nextPos = 2
		positions = Positions({'releaseKey': 0, 'title': 1})
		fieldnames = ['title']
		og_fields = ["""CREATE TEMP VIEW MasterDB AS SELECT DISTINCT(MasterList.releaseKey) AS releaseKey, MasterList.value AS title, PLATFORMS.value AS platformList"""]
		og_references = [""" FROM MasterList, MasterList AS PLATFORMS"""]
		og_joins = []
		og_conditions = [""" WHERE MasterList.gamePieceTypeId={} AND PLATFORMS.releaseKey=MasterList.releaseKey AND PLATFORMS.gamePieceTypeId={}""".format(
					id('title'),
					id('allGameReleases')
				)]
		og_order = """ ORDER BY title;"""
		og_resultFields = ['GROUP_CONCAT(DISTINCT MasterDB.releaseKey)', 'MasterDB.title']
		og_resultGroupBy = ['MasterDB.platformList']

		# title can be customized by user, allow extraction of original title
		if args.originalTitle:
			prepare(
				'originalTitle',
				{'originalTitle': True},
				dbField='ORIGINALTITLE.value AS originalTitle',
				dbRef='MasterList AS ORIGINALTITLE',
				dbCondition='ORIGINALTITLE.releaseKey=MasterList.releaseKey AND ORIGINALTITLE.gamePieceTypeId={}'.format(id('originalTitle')),
				dbResultField='MasterDB.originalTitle'
			)

		# (User customised) sorting title, same export data sorting as in the Galaxy client
		prepare(
			'sortingTitle',
			{'sortingTitle': args.sortingTitle},
			dbField='SORTINGTITLE.value AS sortingTitle',
			dbRef='MasterList AS SORTINGTITLE',
			dbCondition='(SORTINGTITLE.releaseKey=MasterList.releaseKey) AND (SORTINGTITLE.gamePieceTypeId={})'.format(id('sortingTitle')),
			dbResultField='MasterDB.sortingTitle'
		)

		# Create parameterised filtered view of owned games using multiple joins: the order
		# in which we `prepare` them, is the same as they will appear as CSV columns
		if args.summary:
			prepare(
				'summary',
				{'summary': True},
				dbField='SUMMARY.value AS summary',
				dbRef='MasterList AS SUMMARY',
				dbCondition='(SUMMARY.releaseKey=MasterList.releaseKey) AND (SUMMARY.gamePieceTypeId={})'.format(id('summary')),
				dbResultField='MasterDB.summary'
			)

		if args.platforms:
			prepare(
				'platforms',
				{'platformList': True},
			)

		if args.myRating:
			prepare(
				'myRating',
				{'myRating': True},
				dbField='MYRATING.value AS myRating',
				dbRef='MasterList AS MYRATING',
				dbCondition='(MYRATING.releaseKey=MasterList.releaseKey) AND (MYRATING.gamePieceTypeId={})'.format(id('myRating')),
				dbResultField='MasterDB.myRating'
			)

		# add filednames of metadata and orginalMetadata
		# this allows to order them in a way that metadata values are followed by their related originalMetadata value in the export
		for name,condition in {
					'criticsScore': args.criticsScore,
					'developers': args.developers,
					'genres': args.genres,
					'publishers': args.publishers,
					'releaseDate': args.releaseDate,
					'originalReleaseDate': args.originalReleaseDate,
					'themes': args.themes,
				}.items():
			if condition:
				fieldnames.append(name)
			
		if args.criticsScore or args.developers or args.genres or args.publishers or args.releaseDate or args.themes:
			prepare(
				'metadata',
				{}, # fieldnames are added separateley together with their related originalMetadata fields
				dbField='METADATA.value AS metadata',
				dbRef='MasterList AS METADATA',
				dbCondition='METADATA.releaseKey=MasterList.releaseKey AND METADATA.gamePieceTypeId={}'.format(id('meta')),
				dbResultField='MasterDB.metadata'
			)

		if args.originalReleaseDate:
			prepare(
				'originalMetadata',
				{}, # fieldnames are added separateley together with their related metadata fields
				dbField='ORIGINALMETADATA.value AS originalMetadata',
				dbRef='MasterList AS ORIGINALMETADATA',
				dbCondition='ORIGINALMETADATA.releaseKey=MasterList.releaseKey AND ORIGINALMETADATA.gamePieceTypeId={}'.format(id('originalMeta')),
				dbResultField='MasterDB.originalMetadata'
			)

		if args.playtime:
			prepare(
				'playtime',
				{'gameMins': True},
				dbField='GAMETIMES.minutesInGame AS time',
				dbRef='GAMETIMES',
				dbCondition='GAMETIMES.releaseKey=MasterList.releaseKey',
				dbResultField='sum(MasterDB.time)'
			)

		if args.lastPlayed:
			prepare(
				'lastPlayed',
				{'lastPlayed': True},
				dbField='LASTPLAYEDDATES.lastPlayedDate as lastPlayed',
				dbCustomJoin='LEFT JOIN LASTPLAYEDDATES ON LASTPLAYEDDATES.gameReleaseKey=MasterList.releaseKey',
				dbResultField='MasterDB.lastPlayed'
			)


		if args.tags:
			prepare(
				'tags',
				{'tags': True},
				dbField='USERRELEASETAGS.tag AS tags',
				dbCustomJoin='LEFT JOIN USERRELEASETAGS ON USERRELEASETAGS.releaseKey=MasterList.releaseKey',
				dbResultField='GROUP_CONCAT(MasterDB.tags)'
			)

		prepare(  # Grab a list of DLCs for filtering, regardless of whether we're exporting them or not
			'dlcs',
			{'dlcs': args.dlcs},
			# concatenate all dlcs of the game in one list to make sure all dlcs from the different platforms are found
			dbField="""DLC.value AS dlcs,
						CASE
							WHEN DLC.value IS NULL OR DLC.value IN ('{"dlcs":null}', '{"dlcs":[]}')
							THEN NULL
							ELSE REPLACE(REPLACE(DLC.value, '{"dlcs":[', ''), ']}', '')
						END AS dlcList""",
			dbRef='MasterList AS DLC',
			dbCondition='(DLC.releaseKey=MasterList.releaseKey) AND (DLC.gamePieceTypeId={})'.format(id('dlcs')),
			dbResultField="""'{"dlcs":[' || COALESCE(GROUP_CONCAT(MasterDB.dlcList), '') || ']}'"""
		)

		if args.isHidden:
			prepare(
				'isHidden',
				{'isHidden': True},
				dbField='UserReleaseProperties.isHidden AS isHidden',
				dbCustomJoin='LEFT JOIN USERRELEASEPROPERTIES ON USERRELEASEPROPERTIES.releaseKey=MasterList.releaseKey',
				dbResultField='CASE WHEN MasterDB.isHidden = 1 THEN \'True\' ELSE \'False\' END'
			)

		if args.osCompatibility:
			prepare(
				'osCompatibility',
				{'osCompatibility': True},
				# concatenate lists of operating systems because different platforms can support different systems
				dbField="""OSCOMPATIBILITY.value AS osCompatibility,
							CASE
								WHEN OSCOMPATIBILITY.value IS NULL OR OSCOMPATIBILITY.value IN ('{"supported":[]}', '{"supported":null}') 
								THEN NULL 
								ELSE REPLACE(REPLACE(OSCOMPATIBILITY.value, '{"supported":[', ''), ']}', '')
							END AS osList""",
				dbRef='MasterList AS OSCOMPATIBILITY',
				dbCondition='OSCOMPATIBILITY.releaseKey=MasterList.releaseKey AND OSCOMPATIBILITY.gamePieceTypeId={}'.format(id('osCompatibility')),
				dbResultField="""'{"supported":[' || COALESCE(GROUP_CONCAT(MasterDB.osList), '') || ']}'"""
			)

		if args.imageBackground or args.imageSquare or args.imageVertical:
			prepare(
				'images',
				{
					'backgroundImage': args.imageBackground,
					'squareIcon': args.imageSquare,
					'verticalCover': args.imageVertical
				},
				dbField='IMAGES.value AS images',
				dbRef='MasterList AS IMAGES',
				dbCondition='(IMAGES.releaseKey=MasterList.releaseKey) AND (IMAGES.gamePieceTypeId={})'.format(id('originalImages')),
				dbResultField='MasterDB.images'
			)

		# Display each game and its details along with corresponding release key grouped by releasesList
		unique_game_data = """SELECT {} FROM MasterDB GROUP BY {} ORDER BY MasterDB.title;""".format(
			', '.join(og_resultFields),
			', '.join(og_resultGroupBy)
		)

		# Perform the queries
		cursor.execute(owned_game_database)
		cursor.execute(''.join(og_fields + og_references + og_joins + og_conditions) + og_order)
		cursor.execute(unique_game_data)

		# Prepare a list of games and DLCs
		results = []
		dlcs = set()
		while True:
			result = cursor.fetchone()
			if not result: break
			results.append((result[0].split(','), result))
			d = jld('dlcs', True)
			if d:
				for dlc in d:
					dlcs.add(dlc)
		results = natsorted(results, key=lambda r: str.casefold(str(json.loads(r[1][positions['sortingTitle']])['title'])))

		# Exclude games mistakenly treated as DLCs, such as "3 out of 10, EP2"
		for dlc in options['TreatDLCAsGame']:
			dlcs.discard(dlc)

		# Add dlcs mistakenly treated as games, such as "Grey Goo - Emergence Campaign"
		additionalDLCs = options['TreatReleaseAsDLC']
		for game in additionalDLCs.keys():
			dlcs.update(additionalDLCs[game])

		# There are spurious random dlcNUMBERa entries in the library, plus a few DLCs which appear
		# multiple times in different ways and are not attached to a game
		titleExclusion = re.compile(r'^(?:'
				r'dlc_?[0-9]+_?a'
				r'|alternative look for yennefer(?:\s+\[[a-z]+\])?'
				r'|beard and hairstyle set for geralt(?:\s+\[[a-z]+\])?'
				r'|new quest - contract: missing miners(?:\s+\[[a-z]+\])?'
				r'|temerian armor set(?:\s+\[[a-z]+\])?'
		r')$')

		# Compile the CSV
		try:
			with open(args.fileCSV, 'w', encoding='utf-8', newline='') as csvfile:
				writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter=args.delimiter)
				writer.writeheader()
				for (ids, result) in results:
					# Only consider games for the list, not DLCs
					if not args.exportDlcDetails and 0 < len([x for x in ids if x in dlcs]):
						continue

					try:
						# JSON string needs to be converted to dict
						# For json.load() to work correctly, all double quotes must be correctly escaped
						try:
							row = {'title': jld('title', True)}
							if (not row['title']) or (titleExclusion.match(str.casefold(row['title']))):
								continue
						except:
							# No title or {'title': null}
							continue


						# SortingTitle
						if args.sortingTitle:
							try:
								sortingTitle = jld('sortingTitle')
								row['sortingTitle'] = sortingTitle['title']
							except:
								row['sortingTitle'] = ''

						# OriginalTitle
						if args.originalTitle:
							try:
								originalTitle = jld('originalTitle')
								row['originalTitle'] = originalTitle['title']
							except:
								row['originalTitle'] = ''


						# Playtime
						includeField(result, 'gameMins', positions['playtime'], paramName='playtime')

						# LastPlayed
						includeField(result, 'lastPlayed', positions['lastPlayed'])

						# Summaries
						includeField(result, 'summary', fieldType=Type.STRING_JSON)

						# Platforms
						if args.platforms:
							rkeys = result[positions['releaseKey']].split(',')
							if any(platform in releaseKey for platform in platforms for releaseKey in rkeys):
								row['platformList'] = set(platforms[platform] for releaseKey in rkeys for platform in platforms if releaseKey.startswith(platform))
							else:
								row['platformList'] = []

						# User rating
						includeField(result, 'myRating', fieldType=Type.STRING_JSON)

						# Various metadata
						if args.criticsScore or args.developers or args.genres or args.publishers or args.releaseDate or args.themes:
							metadata = jld('metadata')
							includeField(metadata, 'criticsScore', fieldType=Type.INTEGER)
							includeField(metadata, 'developers')
							includeField(metadata, 'genres')
							includeField(metadata, 'publishers')
							includeField(metadata, 'releaseDate', fieldType=Type.DATE)
							includeField(metadata, 'themes')

						# Original metadata
						if args.originalReleaseDate:
							originalMetadata = jld('originalMetadata')
							includeField(originalMetadata, 'originalReleaseDate', 'releaseDate', fieldType=Type.DATE)

						# Original images
						if args.imageBackground or args.imageSquare or args.imageVertical:
							images = jld('images')
							includeField(images, 'backgroundImage', 'background', paramName='imageBackground')
							includeField(images, 'squareIcon', paramName='imageSquare')
							includeField(images, 'verticalCover', paramName='imageVertical')
						
						# DLCs
						if args.dlcs:
							row['dlcs'] = set()
							dlcList = jld('dlcs', True)
							if dlcList == None:
								dlcList = []
							if options["TreatReleaseAsDLC"]:
								rkeys = result[positions['releaseKey']].split(',')
								for key in rkeys:
									if options["TreatReleaseAsDLC"].get(key) != None:
										dlcList.extend(options["TreatReleaseAsDLC"][key])
								
							for dlc in dlcList:
								try:
									# Check the availability of the DLC in the games list (uncertain)
									d = next(x[1] for x in results if dlc in x[0])
									if d:
										row['dlcs'].add(jld('title', True, d))
								except StopIteration:
									pass

						# Tags
						if args.tags:
							includeField(result, 'tags', positions['tags'], fieldType=Type.LIST)

						# isHidden
						if args.isHidden:
							includeField(result, 'isHidden', positions['isHidden'], fieldType=Type.STRING)

						# osCompatibility
						if args.osCompatibility:
							row['osCompatibility'] = set()
							osCompatibility = jld('osCompatibility')
							osList = osCompatibility['supported']
							if osList:
								for operatingSystem in osList:
									row['osCompatibility'].add(operatingSystem['name'])

						# Set conversion, list sorting, empty value reset
						for k,v in row.items():
							if v:
								if list == type(v) or set == type(v):
									row[k] = natsorted(list(row[k]), key=str.casefold)
									if not args.pythonLists:
										row[k] = args.delimiter.join(row[k])
							else:
								row[k] = ''

						writer.writerow(row)
					except Exception as e:
						print('Parsing failed on: {}'.format(result))
						raise e
		except FileNotFoundError:
			print('Unable to write to “{}”, make sure that the path exists and that you have the write permissions'.format(args.fileCSV))
			return

if __name__ == "__main__":
	# macOS
	if platform == "darwin":
		defaultDBlocation = "/Users/Shared/GOG.com/Galaxy/Storage/galaxy-2.0.db"
	# Windows
	elif platform == "win32":
		defaultDBlocation = "C:\\ProgramData\\GOG.com\\Galaxy\\storage\\galaxy-2.0.db"

	def ba(variableName, description, defaultValue=False):
		""" Boolean argument: creates a default boolean argument with the name of the storage variable and
			the description to be shown in the help screen
		"""
		return {
			'action': 'store_true',
			'required': defaultValue,
			'help': description,
			'dest': variableName,
		}

	# Set up the arguments
	args = Arguments(
		[
			[
				['-i', '--input'],
				{
					'default': defaultDBlocation,
					'type': str,
					'nargs': 1,
					'required': False,
					'metavar': 'FN',
					'help': 'pathname of the galaxy2 database',
					'dest': 'fileDB',
				}
			],
			[
				['-o', '--output'],
				{
					'default': 'gameDB.csv',
					'type': str,
					'nargs': 1,
					'required': False,
					'metavar': 'FN',
					'help': 'pathname of the generated CSV',
					'dest': 'fileCSV',
				}
			],
			[
				['-d'],
				{
					'default': '\t',
					'type': str,
					'required': False,
					'metavar': 'CHARACTER',
					'help': 'CSV field separator, defaults to tab',
					'dest': 'delimiter',
				}
			],
			[['-a', '--all'], ba('all', '(default) extracts all the fields')],
			[['--sorting-title'], ba('sortingTitle', '(user customised) sorting title')],
			[['--title-original'], ba('originalTitle', 'original title independent of any user changes')],
			[['--my-rating'], ba('myRating', 'user rating score')],
			[['--critics-score'], ba('criticsScore', 'critics rating score')],
			[['--developers'], ba('developers', 'list of developers')],
			[['--dlcs'], ba('dlcs', 'list of dlc titles for the specified game')],
			[['--genres'], ba('genres', 'game genres')],
			[['--image-background'], ba('imageBackground', 'background image')],
			[['--image-square'], ba('imageSquare', 'square icon')],
			[['--image-vertical'], ba('imageVertical', 'vertical cover image')],
			[['--platforms'], ba('platforms', 'list of platforms the game is available on')],
			[['--publishers'], ba('publishers', 'list of publishers')],
			[['--release-date'], ba('releaseDate', '(user customized) release date of the software')],
			[['--release-date-original'], ba('originalReleaseDate', 'original release date independent of any user changes')],
			[['--summary'], ba('summary', 'game summary')],
			[['--tags'], ba('tags', 'user tags')],
			[['--hidden'], ba('isHidden', 'is gamne hidden in galaxy client')],
			[['--os-compatibility'], ba('osCompatibility', 'list of supported operating systems')],
			[['--themes'], ba('themes', 'game themes')],
			[['--playtime'], ba('playtime', 'time spent playing the game')],
			[['--last-played'], ba('lastPlayed', 'last time the game was played')],
			[['--dlcs-details'], ba('exportDlcDetails', 'add a separate entry for each dlc with all available information to the exported csv')],
			[['--py-lists'], ba('pythonLists', 'export lists as Python parseable instead of delimiter separated strings')],
		],
		description='GOG Galaxy 2 exporter: scans the local Galaxy 2 database to export a list of games and related information into a CSV'
	)

	if not args.anyOption(['delimiter', 'fileCSV', 'fileDB', 'pythonLists', 'exportDlcDetails']):
		args.extractAll()
	if exists(args.fileDB):
		extractData(args)
	else:
		print('Unable to find the DB “{}”, make sure that {}'.format(
			args.fileDB,
			'GOG Galaxy 2 is installed' if defaultDBlocation == args.fileDB else 'you specified the correct path'
		))



================================================
FILE: helper_scripts/print_gameDB.py
================================================

# PRINTS NUMBERED LIST OF GAMES FROM CSV FILE TO TEXT FILE

import csv

# String that contains all the games.
my_games = ""

# Index for the game.
i = 0

# Open csv file to reader
with open('../gameDB.csv', 'r', encoding='utf-8') as csv_file:
    reader = csv.reader(csv_file)

    # The name of the game is on the first part of the row
    for row in reader:
        row_string = ' '.join([str(elem) for elem in row])

        name = row[0].split('\t')

        # Add the index to the game.
        if i > 0:
            my_games += str(i) + ". " + name[0] + "\n"
        print(name[0])
        i += 1

# Write the string to file.
with open('my_games.txt', 'w', encoding='utf-8') as games_file:
    games_file.write(my_games)

# Close the file.
games_file.close()


================================================
FILE: readme.md
================================================
# GOG Galaxy 2.0 Export Script

This script helps a user export their GOG Galaxy 2.0 Library.

## TL;DR / brief how to use

1. Install Python 3, through Windows Store or manually if you prefer
2. Download the [source files](https://github.com/AB1908/GOG-Galaxy-Export-Script/archive/refs/heads/master.zip) and unzip it in a directory of your choice
3. Open the command prompt (`Win+R`, write `cmd` and press Enter) and enter the directory you chose with `cd /d DIRECTORY`, replacing `DIRECTORY` with the directory in which `galaxy_library_export.py` resides
4. Install python's requirements:
   ```
   python -m pip install csv natsort
   ```
5. Export the CSV with:
   ```
   python galaxy_library_export.py
   ```

By default this scripts export everything it can into the CSV. If you would like to customize the results, read below.

## Usage

Through the use of command line parameters, you can decide what data you want exported to the CSV. Some of the options include the list of platforms (`--platforms`), playtime in minutes (`--playtime`), developers, publishers, genres and much more. You can read the help manual by invoking the script without parameters, to find an up to date list of all the possible export options.

If you want to use the CSV in a different tool, such as the [HTML5 library exporter](https://github.com/Varstahl/GOG-Galaxy-HTML5-exporter), you can default to the `-a` parameter to export everything.

When a different locale wants a different CSV delimiter (such as the Italian), you can manually specify the character to use (`-d <character>`).

Also, you can manually specify the database location (`-i`) and the CSV location (`-o`), instead of using the default ones.

If the CSV has to be read by a Python script, you can use the option `--py-lists` to export python compatible list strings that can be reconverted in python objects through `ast`'s `literal_eval`, which avoids several (potentially incorrect) string split/joins.

If you also want to export all available dlcs use the `--dlcs-details` argument. With that all dlcs are handled as "games" and will be exported with all available infos.

## settings.json

The settings.json allows to handle dlcs as game and export them accordingly or treat any release (game, dlc, soundtrack, goodies pack etc.) as dlc of another game, if they are not already linked in the database.

- *TreatDLCAsGame*: Aarray of release-keys which should be treated as a game instead of a dlc
  - Used to mark games which are (mistakenly) treated as dlcs by the gog galaxy client as games.
  - All entries with a matching release-key will be exported with all available data.
  - This will not change the link between a dlc and his parent game if it really is a dlc.
- *TreatReleaseAsDLC*: Dictionary which maps the releaseKey of a game to a list of dlcs.
  - Used to mark any release which is (mistakenly) treated as a game by the gog galaxy client as dlc.
  - The dlcs specified in the settings.json are joined with the list of dlcs found in the database.
  - *TreatReleaseAsDLC* is evaluated after *TreatDLCAsGame*. If a release-key is present in *TreatDLCAsGame* and also mapped to another release-key with *TreatReleaseAsDLC* than this release-key is handled as dlc.
  - This will not override the original link between a dlc and it's parent game. If a dlc is mapped to another game it will be present in the dlc list of this game and the original game.


## Dependencies

- Python 3
  - csv
  - natsort

## Platform Support

All platforms from the [official list](https://github.com/gogcom/galaxy-integrations-python-api/blob/master/PLATFORM_IDs.md) are supported. Some are not listed at the moment but should still show up correctly in the output.

## Wiki

Check the [Wiki tab](https://github.com/AB1908/GOG-Galaxy-Export-Script/wiki).

## Roadmap 

Check the [Projects tab](https://github.com/AB1908/GOG-Galaxy-Export-Script/projects).

## Contribution

Feel free to add issues and pull requests.

## License

This repository is licensed under the [MIT License](https://github.com/AB1908/GOG-Galaxy-Export-Script/blob/master/LICENSE).


================================================
FILE: settings.json
================================================
{
	"TreatDLCAsGame": [
		"epic_1317e4e3b3ed40c289dde85b194347d3"
	],
	"TreatReleaseAsDLC": {
		"steam_290790": ["steam_357180","steam_341810"],
		"steam_365450": ["steam_408710"]
	}
}
Download .txt
gitextract_v_956o0z/

├── .gitignore
├── LICENSE
├── galaxy_library_export.py
├── helper_scripts/
│   └── print_gameDB.py
├── readme.md
└── settings.json
Download .txt
SYMBOL INDEX (12 symbols across 1 files)

FILE: galaxy_library_export.py
  class Arguments (line 16) | class Arguments():
    method __init__ (line 22) | def __init__(self, args, **kwargs):
    method help (line 30) | def help(self):
    method anyOption (line 33) | def anyOption(self, exceptions):
    method extractAll (line 40) | def extractAll(self):
    method __getitem__ (line 43) | def __getitem__(self, name):
    method __getattr__ (line 46) | def __getattr__(self, name):
  class Type (line 54) | class Type(Enum):
  class Positions (line 62) | class Positions(dict):
    method __getitem__ (line 64) | def __getitem__(self, key):
  function extractData (line 70) | def extractData(args):
  function ba (line 561) | def ba(variableName, description, defaultValue=False):
Condensed preview — 6 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (36K chars).
[
  {
    "path": ".gitignore",
    "chars": 45,
    "preview": "gameDB.csv\nhelper_scripts/my_games.txt\n.idea/"
  },
  {
    "path": "LICENSE",
    "chars": 1063,
    "preview": "MIT License\n\nCopyright (c) 2020 AB1908\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof "
  },
  {
    "path": "galaxy_library_export.py",
    "chars": 25373,
    "preview": "#!/usr/bin/env python\r\n# -*- coding: utf-8 -*-\r\n\r\nimport csv\r\nimport json\r\nimport re\r\nimport sqlite3\r\nimport time\r\nfrom "
  },
  {
    "path": "helper_scripts/print_gameDB.py",
    "chars": 766,
    "preview": "\n# PRINTS NUMBERED LIST OF GAMES FROM CSV FILE TO TEXT FILE\n\nimport csv\n\n# String that contains all the games.\nmy_games "
  },
  {
    "path": "readme.md",
    "chars": 4114,
    "preview": "# GOG Galaxy 2.0 Export Script\n\nThis script helps a user export their GOG Galaxy 2.0 Library.\n\n## TL;DR / brief how to u"
  },
  {
    "path": "settings.json",
    "chars": 183,
    "preview": "{\n\t\"TreatDLCAsGame\": [\n\t\t\"epic_1317e4e3b3ed40c289dde85b194347d3\"\n\t],\n\t\"TreatReleaseAsDLC\": {\n\t\t\"steam_290790\": [\"steam_3"
  }
]

About this extraction

This page contains the full source code of the AB1908/GOG-Galaxy-Export-Script GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 6 files (30.8 KB), approximately 8.6k tokens, and a symbol index with 12 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!