├── .gitignore ├── LICENSE ├── galaxy_library_export.py ├── helper_scripts └── print_gameDB.py ├── readme.md └── settings.json /.gitignore: -------------------------------------------------------------------------------- 1 | gameDB.csv 2 | helper_scripts/my_games.txt 3 | .idea/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 AB1908 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /galaxy_library_export.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import csv 5 | import json 6 | import re 7 | import sqlite3 8 | import time 9 | from enum import Enum 10 | from os.path import exists 11 | from sys import platform 12 | 13 | from natsort import natsorted 14 | 15 | 16 | class Arguments(): 17 | """ argparse wrapper, to reduce code verbosity """ 18 | __parser = None 19 | __args = None 20 | __bAll = False # Extract all fields 21 | 22 | def __init__(self, args, **kwargs): 23 | import argparse 24 | self.__parser = argparse.ArgumentParser(**kwargs) 25 | for arg in args: 26 | self.__parser.add_argument(*arg[0], **arg[1]) 27 | self.__args = self.__parser.parse_args() 28 | self.__bAll = getattr(self.__args, 'all') 29 | 30 | def help(self): 31 | self.__parser.print_help() 32 | 33 | def anyOption(self, exceptions): 34 | self.notExportOptions = exceptions 35 | for k,v in self.__args.__dict__.items(): 36 | if (k not in exceptions) and v: 37 | return True 38 | return False 39 | 40 | def extractAll(self): 41 | self.__bAll = True 42 | 43 | def __getitem__(self, name): 44 | return self.__getattr__(name) 45 | 46 | def __getattr__(self, name): 47 | ret = getattr(self.__args, name) 48 | if isinstance(ret, bool) and (name not in self.notExportOptions): 49 | ret = self.__bAll or ret 50 | elif isinstance(ret, list) and (1 == len(ret)): 51 | ret = ret[0] 52 | return ret 53 | 54 | class Type(Enum): 55 | """ used to specify the field type while parsing the raw data """ 56 | STRING = 0 57 | STRING_JSON = 1 58 | INTEGER = 10 59 | DATE = 20 60 | LIST = 30 61 | 62 | class Positions(dict): 63 | """ small dictionary to avoid errors while parsing non-exported field positions """ 64 | def __getitem__(self, key): 65 | try: 66 | return dict.__getitem__(self, key) 67 | except KeyError: 68 | return None 69 | 70 | def extractData(args): 71 | database_location = args.fileDB 72 | 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"} 73 | 74 | def loadOptions(): 75 | """ Loads options from `settings.json` and initialises defaults """ 76 | defaults = {"TreatDLCAsGame": [], "TreatReleaseAsDLC": {}} 77 | 78 | # Load settings from disk 79 | try: 80 | with open('settings.json', 'r', encoding='utf-8') as f: 81 | o = json.load(f) 82 | except: 83 | o = {} 84 | 85 | # Initialise defaults 86 | for k, v in defaults.items(): 87 | if k not in o: 88 | o[k] = v 89 | 90 | return o 91 | 92 | def id(name): 93 | """ Returns the numeric ID for the specified type """ 94 | return cursor.execute('SELECT id FROM GamePieceTypes WHERE type="{}"'.format(name)).fetchone()[0] 95 | 96 | def clean(s): 97 | """ Cleans strings for CSV consumption """ 98 | for f in clean.filters: 99 | s = f.sub('\\\\n', s) # Convert CRLF, LF,
into '\n' string 100 | return s 101 | clean.filters = [ 102 | re.compile(r'\s*?(?:\r?\n|)') 103 | ] 104 | 105 | def jld(name, bReturnParsed=False, object=None): 106 | """ json.loads(`name`), optionally returning the purified sub-object of the same name, 107 | for cases such as {`name`: {`name`: "string"}} 108 | """ 109 | v = json.loads((object if object else result)[positions[name]]) 110 | if not bReturnParsed: 111 | return v 112 | v = v[name] 113 | return clean(v) if isinstance(v, str) else v 114 | 115 | def prepare(resultName, fields, dbField=None, dbRef=None, dbCondition=None, dbCustomJoin=None, dbResultField=None, dbGroupBy=None): 116 | """ Wrapper around the statement preparation and result parsing\n 117 | `resultName` cli argument variable name\n 118 | `fields` {`title` to be inserted in the CSV: `boolean condition`, …}\n 119 | SELECT `dbField` FROM `dbRef` WHERE `dbCondition`\n 120 | SELECT `dbResultField` FROM MasterDB GROUP BY `dbGroupBy` 121 | """ 122 | # CSV field names 123 | for name,condition in fields.items(): 124 | if condition: 125 | fieldnames.append(name) 126 | 127 | if dbField: 128 | og_fields.append(', {}'.format(dbField)) 129 | # Position of the results 130 | positions[resultName] = prepare.nextPos 131 | prepare.nextPos += 1 132 | if dbRef: 133 | og_references.append(', {}'.format(dbRef)) 134 | if dbCondition: 135 | og_conditions.append(' AND ({})'.format(dbCondition)) 136 | if dbCustomJoin: 137 | og_joins.append(' ' + dbCustomJoin) 138 | if dbResultField: 139 | og_resultFields.append(dbResultField) 140 | if dbGroupBy: 141 | og_resultGroupBy.append(dbGroupBy) 142 | 143 | def includeField(object, columnName, fieldName=None, fieldType=Type.STRING, paramName=None, delimiter=','): 144 | if args[columnName if not paramName else paramName]: 145 | if None is fieldName: 146 | fieldName = columnName 147 | try: 148 | if Type.INTEGER is fieldType: 149 | row[columnName] = round(object[fieldName]) 150 | elif Type.DATE is fieldType: 151 | row[columnName] = time.strftime("%Y-%m-%d", time.localtime(object[fieldName])) 152 | elif Type.STRING is fieldType: 153 | row[columnName] = object[fieldName] 154 | elif Type.STRING_JSON is fieldType: 155 | row[columnName] = jld(fieldName, True) 156 | elif Type.LIST is fieldType: 157 | s = object[fieldName].split(delimiter) 158 | row[columnName] = set(s) if 1 < len(s) else objectFieldName 159 | except: 160 | row[columnName] = object[fieldName] 161 | 162 | from contextlib import contextmanager 163 | @contextmanager 164 | def OpenDB(): 165 | # Prepare the DB connection 166 | _connection = sqlite3.connect(database_location) 167 | _cursor = _connection.cursor() 168 | 169 | _exception = None 170 | try: 171 | yield _cursor 172 | except Exception as e: 173 | _exception = e 174 | 175 | # Close the DB connection 176 | _cursor.close() 177 | _connection.close() 178 | 179 | # Re-raise the unhandled exception if needed 180 | if _exception: 181 | raise _exception 182 | 183 | # Load options before opening the DB 184 | options = loadOptions() 185 | 186 | with OpenDB() as cursor: 187 | # Create a view of ProductPurchaseDates (= purchased/added games) joined on GamePieces for a full owned game data DB 188 | owned_game_database = """CREATE TEMP VIEW MasterList AS 189 | SELECT GamePieces.releaseKey, GamePieces.gamePieceTypeId, GamePieces.value FROM ProductPurchaseDates 190 | JOIN GamePieces ON ProductPurchaseDates.gameReleaseKey = GamePieces.releaseKey;""" 191 | 192 | # Set up default queries and processing metadata, and always extract the game title along with any parameters 193 | prepare.nextPos = 2 194 | positions = Positions({'releaseKey': 0, 'title': 1}) 195 | fieldnames = ['title'] 196 | og_fields = ["""CREATE TEMP VIEW MasterDB AS SELECT DISTINCT(MasterList.releaseKey) AS releaseKey, MasterList.value AS title, PLATFORMS.value AS platformList"""] 197 | og_references = [""" FROM MasterList, MasterList AS PLATFORMS"""] 198 | og_joins = [] 199 | og_conditions = [""" WHERE MasterList.gamePieceTypeId={} AND PLATFORMS.releaseKey=MasterList.releaseKey AND PLATFORMS.gamePieceTypeId={}""".format( 200 | id('title'), 201 | id('allGameReleases') 202 | )] 203 | og_order = """ ORDER BY title;""" 204 | og_resultFields = ['GROUP_CONCAT(DISTINCT MasterDB.releaseKey)', 'MasterDB.title'] 205 | og_resultGroupBy = ['MasterDB.platformList'] 206 | 207 | # title can be customized by user, allow extraction of original title 208 | if args.originalTitle: 209 | prepare( 210 | 'originalTitle', 211 | {'originalTitle': True}, 212 | dbField='ORIGINALTITLE.value AS originalTitle', 213 | dbRef='MasterList AS ORIGINALTITLE', 214 | dbCondition='ORIGINALTITLE.releaseKey=MasterList.releaseKey AND ORIGINALTITLE.gamePieceTypeId={}'.format(id('originalTitle')), 215 | dbResultField='MasterDB.originalTitle' 216 | ) 217 | 218 | # (User customised) sorting title, same export data sorting as in the Galaxy client 219 | prepare( 220 | 'sortingTitle', 221 | {'sortingTitle': args.sortingTitle}, 222 | dbField='SORTINGTITLE.value AS sortingTitle', 223 | dbRef='MasterList AS SORTINGTITLE', 224 | dbCondition='(SORTINGTITLE.releaseKey=MasterList.releaseKey) AND (SORTINGTITLE.gamePieceTypeId={})'.format(id('sortingTitle')), 225 | dbResultField='MasterDB.sortingTitle' 226 | ) 227 | 228 | # Create parameterised filtered view of owned games using multiple joins: the order 229 | # in which we `prepare` them, is the same as they will appear as CSV columns 230 | if args.summary: 231 | prepare( 232 | 'summary', 233 | {'summary': True}, 234 | dbField='SUMMARY.value AS summary', 235 | dbRef='MasterList AS SUMMARY', 236 | dbCondition='(SUMMARY.releaseKey=MasterList.releaseKey) AND (SUMMARY.gamePieceTypeId={})'.format(id('summary')), 237 | dbResultField='MasterDB.summary' 238 | ) 239 | 240 | if args.platforms: 241 | prepare( 242 | 'platforms', 243 | {'platformList': True}, 244 | ) 245 | 246 | if args.myRating: 247 | prepare( 248 | 'myRating', 249 | {'myRating': True}, 250 | dbField='MYRATING.value AS myRating', 251 | dbRef='MasterList AS MYRATING', 252 | dbCondition='(MYRATING.releaseKey=MasterList.releaseKey) AND (MYRATING.gamePieceTypeId={})'.format(id('myRating')), 253 | dbResultField='MasterDB.myRating' 254 | ) 255 | 256 | # add filednames of metadata and orginalMetadata 257 | # this allows to order them in a way that metadata values are followed by their related originalMetadata value in the export 258 | for name,condition in { 259 | 'criticsScore': args.criticsScore, 260 | 'developers': args.developers, 261 | 'genres': args.genres, 262 | 'publishers': args.publishers, 263 | 'releaseDate': args.releaseDate, 264 | 'originalReleaseDate': args.originalReleaseDate, 265 | 'themes': args.themes, 266 | }.items(): 267 | if condition: 268 | fieldnames.append(name) 269 | 270 | if args.criticsScore or args.developers or args.genres or args.publishers or args.releaseDate or args.themes: 271 | prepare( 272 | 'metadata', 273 | {}, # fieldnames are added separateley together with their related originalMetadata fields 274 | dbField='METADATA.value AS metadata', 275 | dbRef='MasterList AS METADATA', 276 | dbCondition='METADATA.releaseKey=MasterList.releaseKey AND METADATA.gamePieceTypeId={}'.format(id('meta')), 277 | dbResultField='MasterDB.metadata' 278 | ) 279 | 280 | if args.originalReleaseDate: 281 | prepare( 282 | 'originalMetadata', 283 | {}, # fieldnames are added separateley together with their related metadata fields 284 | dbField='ORIGINALMETADATA.value AS originalMetadata', 285 | dbRef='MasterList AS ORIGINALMETADATA', 286 | dbCondition='ORIGINALMETADATA.releaseKey=MasterList.releaseKey AND ORIGINALMETADATA.gamePieceTypeId={}'.format(id('originalMeta')), 287 | dbResultField='MasterDB.originalMetadata' 288 | ) 289 | 290 | if args.playtime: 291 | prepare( 292 | 'playtime', 293 | {'gameMins': True}, 294 | dbField='GAMETIMES.minutesInGame AS time', 295 | dbRef='GAMETIMES', 296 | dbCondition='GAMETIMES.releaseKey=MasterList.releaseKey', 297 | dbResultField='sum(MasterDB.time)' 298 | ) 299 | 300 | if args.lastPlayed: 301 | prepare( 302 | 'lastPlayed', 303 | {'lastPlayed': True}, 304 | dbField='LASTPLAYEDDATES.lastPlayedDate as lastPlayed', 305 | dbCustomJoin='LEFT JOIN LASTPLAYEDDATES ON LASTPLAYEDDATES.gameReleaseKey=MasterList.releaseKey', 306 | dbResultField='MasterDB.lastPlayed' 307 | ) 308 | 309 | 310 | if args.tags: 311 | prepare( 312 | 'tags', 313 | {'tags': True}, 314 | dbField='USERRELEASETAGS.tag AS tags', 315 | dbCustomJoin='LEFT JOIN USERRELEASETAGS ON USERRELEASETAGS.releaseKey=MasterList.releaseKey', 316 | dbResultField='GROUP_CONCAT(MasterDB.tags)' 317 | ) 318 | 319 | prepare( # Grab a list of DLCs for filtering, regardless of whether we're exporting them or not 320 | 'dlcs', 321 | {'dlcs': args.dlcs}, 322 | # concatenate all dlcs of the game in one list to make sure all dlcs from the different platforms are found 323 | dbField="""DLC.value AS dlcs, 324 | CASE 325 | WHEN DLC.value IS NULL OR DLC.value IN ('{"dlcs":null}', '{"dlcs":[]}') 326 | THEN NULL 327 | ELSE REPLACE(REPLACE(DLC.value, '{"dlcs":[', ''), ']}', '') 328 | END AS dlcList""", 329 | dbRef='MasterList AS DLC', 330 | dbCondition='(DLC.releaseKey=MasterList.releaseKey) AND (DLC.gamePieceTypeId={})'.format(id('dlcs')), 331 | dbResultField="""'{"dlcs":[' || COALESCE(GROUP_CONCAT(MasterDB.dlcList), '') || ']}'""" 332 | ) 333 | 334 | if args.isHidden: 335 | prepare( 336 | 'isHidden', 337 | {'isHidden': True}, 338 | dbField='UserReleaseProperties.isHidden AS isHidden', 339 | dbCustomJoin='LEFT JOIN USERRELEASEPROPERTIES ON USERRELEASEPROPERTIES.releaseKey=MasterList.releaseKey', 340 | dbResultField='CASE WHEN MasterDB.isHidden = 1 THEN \'True\' ELSE \'False\' END' 341 | ) 342 | 343 | if args.osCompatibility: 344 | prepare( 345 | 'osCompatibility', 346 | {'osCompatibility': True}, 347 | # concatenate lists of operating systems because different platforms can support different systems 348 | dbField="""OSCOMPATIBILITY.value AS osCompatibility, 349 | CASE 350 | WHEN OSCOMPATIBILITY.value IS NULL OR OSCOMPATIBILITY.value IN ('{"supported":[]}', '{"supported":null}') 351 | THEN NULL 352 | ELSE REPLACE(REPLACE(OSCOMPATIBILITY.value, '{"supported":[', ''), ']}', '') 353 | END AS osList""", 354 | dbRef='MasterList AS OSCOMPATIBILITY', 355 | dbCondition='OSCOMPATIBILITY.releaseKey=MasterList.releaseKey AND OSCOMPATIBILITY.gamePieceTypeId={}'.format(id('osCompatibility')), 356 | dbResultField="""'{"supported":[' || COALESCE(GROUP_CONCAT(MasterDB.osList), '') || ']}'""" 357 | ) 358 | 359 | if args.imageBackground or args.imageSquare or args.imageVertical: 360 | prepare( 361 | 'images', 362 | { 363 | 'backgroundImage': args.imageBackground, 364 | 'squareIcon': args.imageSquare, 365 | 'verticalCover': args.imageVertical 366 | }, 367 | dbField='IMAGES.value AS images', 368 | dbRef='MasterList AS IMAGES', 369 | dbCondition='(IMAGES.releaseKey=MasterList.releaseKey) AND (IMAGES.gamePieceTypeId={})'.format(id('originalImages')), 370 | dbResultField='MasterDB.images' 371 | ) 372 | 373 | # Display each game and its details along with corresponding release key grouped by releasesList 374 | unique_game_data = """SELECT {} FROM MasterDB GROUP BY {} ORDER BY MasterDB.title;""".format( 375 | ', '.join(og_resultFields), 376 | ', '.join(og_resultGroupBy) 377 | ) 378 | 379 | # Perform the queries 380 | cursor.execute(owned_game_database) 381 | cursor.execute(''.join(og_fields + og_references + og_joins + og_conditions) + og_order) 382 | cursor.execute(unique_game_data) 383 | 384 | # Prepare a list of games and DLCs 385 | results = [] 386 | dlcs = set() 387 | while True: 388 | result = cursor.fetchone() 389 | if not result: break 390 | results.append((result[0].split(','), result)) 391 | d = jld('dlcs', True) 392 | if d: 393 | for dlc in d: 394 | dlcs.add(dlc) 395 | results = natsorted(results, key=lambda r: str.casefold(str(json.loads(r[1][positions['sortingTitle']])['title']))) 396 | 397 | # Exclude games mistakenly treated as DLCs, such as "3 out of 10, EP2" 398 | for dlc in options['TreatDLCAsGame']: 399 | dlcs.discard(dlc) 400 | 401 | # Add dlcs mistakenly treated as games, such as "Grey Goo - Emergence Campaign" 402 | additionalDLCs = options['TreatReleaseAsDLC'] 403 | for game in additionalDLCs.keys(): 404 | dlcs.update(additionalDLCs[game]) 405 | 406 | # There are spurious random dlcNUMBERa entries in the library, plus a few DLCs which appear 407 | # multiple times in different ways and are not attached to a game 408 | titleExclusion = re.compile(r'^(?:' 409 | r'dlc_?[0-9]+_?a' 410 | r'|alternative look for yennefer(?:\s+\[[a-z]+\])?' 411 | r'|beard and hairstyle set for geralt(?:\s+\[[a-z]+\])?' 412 | r'|new quest - contract: missing miners(?:\s+\[[a-z]+\])?' 413 | r'|temerian armor set(?:\s+\[[a-z]+\])?' 414 | r')$') 415 | 416 | # Compile the CSV 417 | try: 418 | with open(args.fileCSV, 'w', encoding='utf-8', newline='') as csvfile: 419 | writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter=args.delimiter) 420 | writer.writeheader() 421 | for (ids, result) in results: 422 | # Only consider games for the list, not DLCs 423 | if not args.exportDlcDetails and 0 < len([x for x in ids if x in dlcs]): 424 | continue 425 | 426 | try: 427 | # JSON string needs to be converted to dict 428 | # For json.load() to work correctly, all double quotes must be correctly escaped 429 | try: 430 | row = {'title': jld('title', True)} 431 | if (not row['title']) or (titleExclusion.match(str.casefold(row['title']))): 432 | continue 433 | except: 434 | # No title or {'title': null} 435 | continue 436 | 437 | 438 | # SortingTitle 439 | if args.sortingTitle: 440 | try: 441 | sortingTitle = jld('sortingTitle') 442 | row['sortingTitle'] = sortingTitle['title'] 443 | except: 444 | row['sortingTitle'] = '' 445 | 446 | # OriginalTitle 447 | if args.originalTitle: 448 | try: 449 | originalTitle = jld('originalTitle') 450 | row['originalTitle'] = originalTitle['title'] 451 | except: 452 | row['originalTitle'] = '' 453 | 454 | 455 | # Playtime 456 | includeField(result, 'gameMins', positions['playtime'], paramName='playtime') 457 | 458 | # LastPlayed 459 | includeField(result, 'lastPlayed', positions['lastPlayed']) 460 | 461 | # Summaries 462 | includeField(result, 'summary', fieldType=Type.STRING_JSON) 463 | 464 | # Platforms 465 | if args.platforms: 466 | rkeys = result[positions['releaseKey']].split(',') 467 | if any(platform in releaseKey for platform in platforms for releaseKey in rkeys): 468 | row['platformList'] = set(platforms[platform] for releaseKey in rkeys for platform in platforms if releaseKey.startswith(platform)) 469 | else: 470 | row['platformList'] = [] 471 | 472 | # User rating 473 | includeField(result, 'myRating', fieldType=Type.STRING_JSON) 474 | 475 | # Various metadata 476 | if args.criticsScore or args.developers or args.genres or args.publishers or args.releaseDate or args.themes: 477 | metadata = jld('metadata') 478 | includeField(metadata, 'criticsScore', fieldType=Type.INTEGER) 479 | includeField(metadata, 'developers') 480 | includeField(metadata, 'genres') 481 | includeField(metadata, 'publishers') 482 | includeField(metadata, 'releaseDate', fieldType=Type.DATE) 483 | includeField(metadata, 'themes') 484 | 485 | # Original metadata 486 | if args.originalReleaseDate: 487 | originalMetadata = jld('originalMetadata') 488 | includeField(originalMetadata, 'originalReleaseDate', 'releaseDate', fieldType=Type.DATE) 489 | 490 | # Original images 491 | if args.imageBackground or args.imageSquare or args.imageVertical: 492 | images = jld('images') 493 | includeField(images, 'backgroundImage', 'background', paramName='imageBackground') 494 | includeField(images, 'squareIcon', paramName='imageSquare') 495 | includeField(images, 'verticalCover', paramName='imageVertical') 496 | 497 | # DLCs 498 | if args.dlcs: 499 | row['dlcs'] = set() 500 | dlcList = jld('dlcs', True) 501 | if dlcList == None: 502 | dlcList = [] 503 | if options["TreatReleaseAsDLC"]: 504 | rkeys = result[positions['releaseKey']].split(',') 505 | for key in rkeys: 506 | if options["TreatReleaseAsDLC"].get(key) != None: 507 | dlcList.extend(options["TreatReleaseAsDLC"][key]) 508 | 509 | for dlc in dlcList: 510 | try: 511 | # Check the availability of the DLC in the games list (uncertain) 512 | d = next(x[1] for x in results if dlc in x[0]) 513 | if d: 514 | row['dlcs'].add(jld('title', True, d)) 515 | except StopIteration: 516 | pass 517 | 518 | # Tags 519 | if args.tags: 520 | includeField(result, 'tags', positions['tags'], fieldType=Type.LIST) 521 | 522 | # isHidden 523 | if args.isHidden: 524 | includeField(result, 'isHidden', positions['isHidden'], fieldType=Type.STRING) 525 | 526 | # osCompatibility 527 | if args.osCompatibility: 528 | row['osCompatibility'] = set() 529 | osCompatibility = jld('osCompatibility') 530 | osList = osCompatibility['supported'] 531 | if osList: 532 | for operatingSystem in osList: 533 | row['osCompatibility'].add(operatingSystem['name']) 534 | 535 | # Set conversion, list sorting, empty value reset 536 | for k,v in row.items(): 537 | if v: 538 | if list == type(v) or set == type(v): 539 | row[k] = natsorted(list(row[k]), key=str.casefold) 540 | if not args.pythonLists: 541 | row[k] = args.delimiter.join(row[k]) 542 | else: 543 | row[k] = '' 544 | 545 | writer.writerow(row) 546 | except Exception as e: 547 | print('Parsing failed on: {}'.format(result)) 548 | raise e 549 | except FileNotFoundError: 550 | print('Unable to write to “{}”, make sure that the path exists and that you have the write permissions'.format(args.fileCSV)) 551 | return 552 | 553 | if __name__ == "__main__": 554 | # macOS 555 | if platform == "darwin": 556 | defaultDBlocation = "/Users/Shared/GOG.com/Galaxy/Storage/galaxy-2.0.db" 557 | # Windows 558 | elif platform == "win32": 559 | defaultDBlocation = "C:\\ProgramData\\GOG.com\\Galaxy\\storage\\galaxy-2.0.db" 560 | 561 | def ba(variableName, description, defaultValue=False): 562 | """ Boolean argument: creates a default boolean argument with the name of the storage variable and 563 | the description to be shown in the help screen 564 | """ 565 | return { 566 | 'action': 'store_true', 567 | 'required': defaultValue, 568 | 'help': description, 569 | 'dest': variableName, 570 | } 571 | 572 | # Set up the arguments 573 | args = Arguments( 574 | [ 575 | [ 576 | ['-i', '--input'], 577 | { 578 | 'default': defaultDBlocation, 579 | 'type': str, 580 | 'nargs': 1, 581 | 'required': False, 582 | 'metavar': 'FN', 583 | 'help': 'pathname of the galaxy2 database', 584 | 'dest': 'fileDB', 585 | } 586 | ], 587 | [ 588 | ['-o', '--output'], 589 | { 590 | 'default': 'gameDB.csv', 591 | 'type': str, 592 | 'nargs': 1, 593 | 'required': False, 594 | 'metavar': 'FN', 595 | 'help': 'pathname of the generated CSV', 596 | 'dest': 'fileCSV', 597 | } 598 | ], 599 | [ 600 | ['-d'], 601 | { 602 | 'default': '\t', 603 | 'type': str, 604 | 'required': False, 605 | 'metavar': 'CHARACTER', 606 | 'help': 'CSV field separator, defaults to tab', 607 | 'dest': 'delimiter', 608 | } 609 | ], 610 | [['-a', '--all'], ba('all', '(default) extracts all the fields')], 611 | [['--sorting-title'], ba('sortingTitle', '(user customised) sorting title')], 612 | [['--title-original'], ba('originalTitle', 'original title independent of any user changes')], 613 | [['--my-rating'], ba('myRating', 'user rating score')], 614 | [['--critics-score'], ba('criticsScore', 'critics rating score')], 615 | [['--developers'], ba('developers', 'list of developers')], 616 | [['--dlcs'], ba('dlcs', 'list of dlc titles for the specified game')], 617 | [['--genres'], ba('genres', 'game genres')], 618 | [['--image-background'], ba('imageBackground', 'background image')], 619 | [['--image-square'], ba('imageSquare', 'square icon')], 620 | [['--image-vertical'], ba('imageVertical', 'vertical cover image')], 621 | [['--platforms'], ba('platforms', 'list of platforms the game is available on')], 622 | [['--publishers'], ba('publishers', 'list of publishers')], 623 | [['--release-date'], ba('releaseDate', '(user customized) release date of the software')], 624 | [['--release-date-original'], ba('originalReleaseDate', 'original release date independent of any user changes')], 625 | [['--summary'], ba('summary', 'game summary')], 626 | [['--tags'], ba('tags', 'user tags')], 627 | [['--hidden'], ba('isHidden', 'is gamne hidden in galaxy client')], 628 | [['--os-compatibility'], ba('osCompatibility', 'list of supported operating systems')], 629 | [['--themes'], ba('themes', 'game themes')], 630 | [['--playtime'], ba('playtime', 'time spent playing the game')], 631 | [['--last-played'], ba('lastPlayed', 'last time the game was played')], 632 | [['--dlcs-details'], ba('exportDlcDetails', 'add a separate entry for each dlc with all available information to the exported csv')], 633 | [['--py-lists'], ba('pythonLists', 'export lists as Python parseable instead of delimiter separated strings')], 634 | ], 635 | description='GOG Galaxy 2 exporter: scans the local Galaxy 2 database to export a list of games and related information into a CSV' 636 | ) 637 | 638 | if not args.anyOption(['delimiter', 'fileCSV', 'fileDB', 'pythonLists', 'exportDlcDetails']): 639 | args.extractAll() 640 | if exists(args.fileDB): 641 | extractData(args) 642 | else: 643 | print('Unable to find the DB “{}”, make sure that {}'.format( 644 | args.fileDB, 645 | 'GOG Galaxy 2 is installed' if defaultDBlocation == args.fileDB else 'you specified the correct path' 646 | )) 647 | 648 | -------------------------------------------------------------------------------- /helper_scripts/print_gameDB.py: -------------------------------------------------------------------------------- 1 | 2 | # PRINTS NUMBERED LIST OF GAMES FROM CSV FILE TO TEXT FILE 3 | 4 | import csv 5 | 6 | # String that contains all the games. 7 | my_games = "" 8 | 9 | # Index for the game. 10 | i = 0 11 | 12 | # Open csv file to reader 13 | with open('../gameDB.csv', 'r', encoding='utf-8') as csv_file: 14 | reader = csv.reader(csv_file) 15 | 16 | # The name of the game is on the first part of the row 17 | for row in reader: 18 | row_string = ' '.join([str(elem) for elem in row]) 19 | 20 | name = row[0].split('\t') 21 | 22 | # Add the index to the game. 23 | if i > 0: 24 | my_games += str(i) + ". " + name[0] + "\n" 25 | print(name[0]) 26 | i += 1 27 | 28 | # Write the string to file. 29 | with open('my_games.txt', 'w', encoding='utf-8') as games_file: 30 | games_file.write(my_games) 31 | 32 | # Close the file. 33 | games_file.close() 34 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # GOG Galaxy 2.0 Export Script 2 | 3 | This script helps a user export their GOG Galaxy 2.0 Library. 4 | 5 | ## TL;DR / brief how to use 6 | 7 | 1. Install Python 3, through Windows Store or manually if you prefer 8 | 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 9 | 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 10 | 4. Install python's requirements: 11 | ``` 12 | python -m pip install csv natsort 13 | ``` 14 | 5. Export the CSV with: 15 | ``` 16 | python galaxy_library_export.py 17 | ``` 18 | 19 | By default this scripts export everything it can into the CSV. If you would like to customize the results, read below. 20 | 21 | ## Usage 22 | 23 | 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. 24 | 25 | 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. 26 | 27 | When a different locale wants a different CSV delimiter (such as the Italian), you can manually specify the character to use (`-d `). 28 | 29 | Also, you can manually specify the database location (`-i`) and the CSV location (`-o`), instead of using the default ones. 30 | 31 | 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. 32 | 33 | 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. 34 | 35 | ## settings.json 36 | 37 | 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. 38 | 39 | - *TreatDLCAsGame*: Aarray of release-keys which should be treated as a game instead of a dlc 40 | - Used to mark games which are (mistakenly) treated as dlcs by the gog galaxy client as games. 41 | - All entries with a matching release-key will be exported with all available data. 42 | - This will not change the link between a dlc and his parent game if it really is a dlc. 43 | - *TreatReleaseAsDLC*: Dictionary which maps the releaseKey of a game to a list of dlcs. 44 | - Used to mark any release which is (mistakenly) treated as a game by the gog galaxy client as dlc. 45 | - The dlcs specified in the settings.json are joined with the list of dlcs found in the database. 46 | - *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. 47 | - 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. 48 | 49 | 50 | ## Dependencies 51 | 52 | - Python 3 53 | - csv 54 | - natsort 55 | 56 | ## Platform Support 57 | 58 | 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. 59 | 60 | ## Wiki 61 | 62 | Check the [Wiki tab](https://github.com/AB1908/GOG-Galaxy-Export-Script/wiki). 63 | 64 | ## Roadmap 65 | 66 | Check the [Projects tab](https://github.com/AB1908/GOG-Galaxy-Export-Script/projects). 67 | 68 | ## Contribution 69 | 70 | Feel free to add issues and pull requests. 71 | 72 | ## License 73 | 74 | This repository is licensed under the [MIT License](https://github.com/AB1908/GOG-Galaxy-Export-Script/blob/master/LICENSE). 75 | -------------------------------------------------------------------------------- /settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "TreatDLCAsGame": [ 3 | "epic_1317e4e3b3ed40c289dde85b194347d3" 4 | ], 5 | "TreatReleaseAsDLC": { 6 | "steam_290790": ["steam_357180","steam_341810"], 7 | "steam_365450": ["steam_408710"] 8 | } 9 | } --------------------------------------------------------------------------------