[
  {
    "path": ".gitignore",
    "content": "gameDB.csv\nhelper_scripts/my_games.txt\n.idea/"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2020 AB1908\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "galaxy_library_export.py",
    "content": "#!/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 enum import Enum\r\nfrom os.path import exists\r\nfrom sys import platform\r\n\r\nfrom natsort import natsorted\r\n\r\n\r\nclass Arguments():\r\n\t\"\"\" argparse wrapper, to reduce code verbosity \"\"\"\r\n\t__parser = None\r\n\t__args = None\r\n\t__bAll = False  # Extract all fields\r\n\r\n\tdef __init__(self, args, **kwargs):\r\n\t\timport argparse\r\n\t\tself.__parser = argparse.ArgumentParser(**kwargs)\r\n\t\tfor arg in args:\r\n\t\t\tself.__parser.add_argument(*arg[0], **arg[1])\r\n\t\tself.__args = self.__parser.parse_args()\r\n\t\tself.__bAll = getattr(self.__args, 'all')\r\n\r\n\tdef help(self):\r\n\t\tself.__parser.print_help()\r\n\r\n\tdef anyOption(self, exceptions):\r\n\t\tself.notExportOptions = exceptions\r\n\t\tfor k,v in self.__args.__dict__.items():\r\n\t\t\tif (k not in exceptions) and v:\r\n\t\t\t\treturn True\r\n\t\treturn False\r\n\r\n\tdef extractAll(self):\r\n\t\tself.__bAll = True\r\n\r\n\tdef __getitem__(self, name):\r\n\t\treturn self.__getattr__(name)\r\n\r\n\tdef __getattr__(self, name):\r\n\t\tret = getattr(self.__args, name)\r\n\t\tif isinstance(ret, bool) and (name not in self.notExportOptions):\r\n\t\t\tret = self.__bAll or ret\r\n\t\telif isinstance(ret, list) and (1 == len(ret)):\r\n\t\t\tret = ret[0]\r\n\t\treturn ret\r\n\r\nclass Type(Enum):\r\n\t\"\"\" used to specify the field type while parsing the raw data \"\"\"\r\n\tSTRING = 0\r\n\tSTRING_JSON = 1\r\n\tINTEGER = 10\r\n\tDATE = 20\r\n\tLIST = 30\r\n\r\nclass Positions(dict):\r\n\t\"\"\" small dictionary to avoid errors while parsing non-exported field positions \"\"\"\r\n\tdef __getitem__(self, key):\r\n\t\ttry:\r\n\t\t\treturn dict.__getitem__(self, key)\r\n\t\texcept KeyError:\r\n\t\t\treturn None\r\n\r\ndef extractData(args):\r\n\tdatabase_location = args.fileDB\r\n\tplatforms = {\"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\"}\r\n\r\n\tdef loadOptions():\r\n\t\t\"\"\" Loads options from `settings.json` and initialises defaults \"\"\"\r\n\t\tdefaults = {\"TreatDLCAsGame\": [], \"TreatReleaseAsDLC\": {}}\r\n\r\n\t\t# Load settings from disk\r\n\t\ttry:\r\n\t\t\twith open('settings.json', 'r', encoding='utf-8') as f:\r\n\t\t\t\to = json.load(f)\r\n\t\texcept:\r\n\t\t\to = {}\r\n\t\t\r\n\t\t# Initialise defaults\r\n\t\tfor k, v in defaults.items():\r\n\t\t\tif k not in o:\r\n\t\t\t\to[k] = v\r\n\r\n\t\treturn o\r\n\r\n\tdef id(name):\r\n\t\t\"\"\" Returns the numeric ID for the specified type \"\"\"\r\n\t\treturn cursor.execute('SELECT id FROM GamePieceTypes WHERE type=\"{}\"'.format(name)).fetchone()[0]\r\n\r\n\tdef clean(s):\r\n\t\t\"\"\" Cleans strings for CSV consumption \"\"\"\r\n\t\tfor f in clean.filters:\r\n\t\t\ts = f.sub('\\\\\\\\n', s)  # Convert CRLF, LF, <br> into '\\n' string\r\n\t\treturn s\r\n\tclean.filters = [\r\n\t\tre.compile(r'\\s*?(?:\\r?\\n|<br\\s*/?>)')\r\n\t]\r\n\r\n\tdef jld(name, bReturnParsed=False, object=None):\r\n\t\t\"\"\" json.loads(`name`), optionally returning the purified sub-object of the same name,\r\n\t\t    for cases such as {`name`: {`name`: \"string\"}}\r\n\t\t\"\"\"\r\n\t\tv = json.loads((object if object else result)[positions[name]])\r\n\t\tif not bReturnParsed:\r\n\t\t\treturn v\r\n\t\tv = v[name]\r\n\t\treturn clean(v) if isinstance(v, str) else v\r\n\r\n\tdef prepare(resultName, fields, dbField=None, dbRef=None, dbCondition=None, dbCustomJoin=None, dbResultField=None, dbGroupBy=None):\r\n\t\t\"\"\" Wrapper around the statement preparation and result parsing\\n\r\n\t\t\t`resultName` cli argument variable name\\n\r\n\t\t\t`fields` {`title` to be inserted in the CSV: `boolean condition`, …}\\n\r\n\t\t\tSELECT `dbField` FROM `dbRef` WHERE `dbCondition`\\n\r\n\t\t\tSELECT `dbResultField` FROM MasterDB GROUP BY `dbGroupBy`\r\n\t\t\"\"\"\r\n\t\t# CSV field names\r\n\t\tfor name,condition in fields.items():\r\n\t\t\tif condition:\r\n\t\t\t\tfieldnames.append(name)\r\n\r\n\t\tif dbField:\r\n\t\t\tog_fields.append(', {}'.format(dbField))\r\n\t\t\t# Position of the results\r\n\t\t\tpositions[resultName] = prepare.nextPos\r\n\t\t\tprepare.nextPos += 1\r\n\t\tif dbRef:\r\n\t\t\tog_references.append(', {}'.format(dbRef))\r\n\t\tif dbCondition:\r\n\t\t\tog_conditions.append(' AND ({})'.format(dbCondition))\r\n\t\tif dbCustomJoin:\r\n\t\t\tog_joins.append(' ' + dbCustomJoin)\r\n\t\tif dbResultField:\r\n\t\t\tog_resultFields.append(dbResultField)\r\n\t\tif dbGroupBy:\r\n\t\t\tog_resultGroupBy.append(dbGroupBy)\r\n\t\r\n\tdef includeField(object, columnName, fieldName=None, fieldType=Type.STRING, paramName=None, delimiter=','):\r\n\t\tif args[columnName if not paramName else paramName]:\r\n\t\t\tif None is fieldName:\r\n\t\t\t\tfieldName = columnName\r\n\t\t\ttry:\r\n\t\t\t\tif Type.INTEGER is fieldType:\r\n\t\t\t\t\trow[columnName] = round(object[fieldName])\r\n\t\t\t\telif Type.DATE is fieldType:\r\n\t\t\t\t\trow[columnName] = time.strftime(\"%Y-%m-%d\", time.localtime(object[fieldName]))\r\n\t\t\t\telif Type.STRING is fieldType:\r\n\t\t\t\t\trow[columnName] = object[fieldName]\r\n\t\t\t\telif Type.STRING_JSON is fieldType:\r\n\t\t\t\t\trow[columnName] = jld(fieldName, True)\r\n\t\t\t\telif Type.LIST is fieldType:\r\n\t\t\t\t\ts = object[fieldName].split(delimiter)\r\n\t\t\t\t\trow[columnName] = set(s) if 1 < len(s) else objectFieldName\r\n\t\t\texcept:\r\n\t\t\t\trow[columnName] = object[fieldName]\r\n\r\n\tfrom contextlib import contextmanager\r\n\t@contextmanager\r\n\tdef OpenDB():\r\n\t\t# Prepare the DB connection\r\n\t\t_connection = sqlite3.connect(database_location)\r\n\t\t_cursor = _connection.cursor()\r\n\r\n\t\t_exception = None\r\n\t\ttry:\r\n\t\t\tyield _cursor\r\n\t\texcept Exception as e:\r\n\t\t\t_exception = e\r\n\r\n\t\t# Close the DB connection\r\n\t\t_cursor.close()\r\n\t\t_connection.close()\r\n\r\n\t\t# Re-raise the unhandled exception if needed\r\n\t\tif _exception:\r\n\t\t\traise _exception\r\n\r\n\t# Load options before opening the DB\r\n\toptions = loadOptions()\r\n\r\n\twith OpenDB() as cursor:\r\n\t\t# Create a view of ProductPurchaseDates (= purchased/added games) joined on GamePieces for a full owned game data DB\r\n\t\towned_game_database = \"\"\"CREATE TEMP VIEW MasterList AS\r\n\t\t\t\tSELECT GamePieces.releaseKey, GamePieces.gamePieceTypeId, GamePieces.value FROM ProductPurchaseDates\r\n\t\t\t\tJOIN GamePieces ON ProductPurchaseDates.gameReleaseKey = GamePieces.releaseKey;\"\"\"\r\n\r\n\t\t# Set up default queries and processing metadata, and always extract the game title along with any parameters\r\n\t\tprepare.nextPos = 2\r\n\t\tpositions = Positions({'releaseKey': 0, 'title': 1})\r\n\t\tfieldnames = ['title']\r\n\t\tog_fields = [\"\"\"CREATE TEMP VIEW MasterDB AS SELECT DISTINCT(MasterList.releaseKey) AS releaseKey, MasterList.value AS title, PLATFORMS.value AS platformList\"\"\"]\r\n\t\tog_references = [\"\"\" FROM MasterList, MasterList AS PLATFORMS\"\"\"]\r\n\t\tog_joins = []\r\n\t\tog_conditions = [\"\"\" WHERE MasterList.gamePieceTypeId={} AND PLATFORMS.releaseKey=MasterList.releaseKey AND PLATFORMS.gamePieceTypeId={}\"\"\".format(\r\n\t\t\t\t\tid('title'),\r\n\t\t\t\t\tid('allGameReleases')\r\n\t\t\t\t)]\r\n\t\tog_order = \"\"\" ORDER BY title;\"\"\"\r\n\t\tog_resultFields = ['GROUP_CONCAT(DISTINCT MasterDB.releaseKey)', 'MasterDB.title']\r\n\t\tog_resultGroupBy = ['MasterDB.platformList']\r\n\r\n\t\t# title can be customized by user, allow extraction of original title\r\n\t\tif args.originalTitle:\r\n\t\t\tprepare(\r\n\t\t\t\t'originalTitle',\r\n\t\t\t\t{'originalTitle': True},\r\n\t\t\t\tdbField='ORIGINALTITLE.value AS originalTitle',\r\n\t\t\t\tdbRef='MasterList AS ORIGINALTITLE',\r\n\t\t\t\tdbCondition='ORIGINALTITLE.releaseKey=MasterList.releaseKey AND ORIGINALTITLE.gamePieceTypeId={}'.format(id('originalTitle')),\r\n\t\t\t\tdbResultField='MasterDB.originalTitle'\r\n\t\t\t)\r\n\r\n\t\t# (User customised) sorting title, same export data sorting as in the Galaxy client\r\n\t\tprepare(\r\n\t\t\t'sortingTitle',\r\n\t\t\t{'sortingTitle': args.sortingTitle},\r\n\t\t\tdbField='SORTINGTITLE.value AS sortingTitle',\r\n\t\t\tdbRef='MasterList AS SORTINGTITLE',\r\n\t\t\tdbCondition='(SORTINGTITLE.releaseKey=MasterList.releaseKey) AND (SORTINGTITLE.gamePieceTypeId={})'.format(id('sortingTitle')),\r\n\t\t\tdbResultField='MasterDB.sortingTitle'\r\n\t\t)\r\n\r\n\t\t# Create parameterised filtered view of owned games using multiple joins: the order\r\n\t\t# in which we `prepare` them, is the same as they will appear as CSV columns\r\n\t\tif args.summary:\r\n\t\t\tprepare(\r\n\t\t\t\t'summary',\r\n\t\t\t\t{'summary': True},\r\n\t\t\t\tdbField='SUMMARY.value AS summary',\r\n\t\t\t\tdbRef='MasterList AS SUMMARY',\r\n\t\t\t\tdbCondition='(SUMMARY.releaseKey=MasterList.releaseKey) AND (SUMMARY.gamePieceTypeId={})'.format(id('summary')),\r\n\t\t\t\tdbResultField='MasterDB.summary'\r\n\t\t\t)\r\n\r\n\t\tif args.platforms:\r\n\t\t\tprepare(\r\n\t\t\t\t'platforms',\r\n\t\t\t\t{'platformList': True},\r\n\t\t\t)\r\n\r\n\t\tif args.myRating:\r\n\t\t\tprepare(\r\n\t\t\t\t'myRating',\r\n\t\t\t\t{'myRating': True},\r\n\t\t\t\tdbField='MYRATING.value AS myRating',\r\n\t\t\t\tdbRef='MasterList AS MYRATING',\r\n\t\t\t\tdbCondition='(MYRATING.releaseKey=MasterList.releaseKey) AND (MYRATING.gamePieceTypeId={})'.format(id('myRating')),\r\n\t\t\t\tdbResultField='MasterDB.myRating'\r\n\t\t\t)\r\n\r\n\t\t# add filednames of metadata and orginalMetadata\r\n\t\t# this allows to order them in a way that metadata values are followed by their related originalMetadata value in the export\r\n\t\tfor name,condition in {\r\n\t\t\t\t\t'criticsScore': args.criticsScore,\r\n\t\t\t\t\t'developers': args.developers,\r\n\t\t\t\t\t'genres': args.genres,\r\n\t\t\t\t\t'publishers': args.publishers,\r\n\t\t\t\t\t'releaseDate': args.releaseDate,\r\n\t\t\t\t\t'originalReleaseDate': args.originalReleaseDate,\r\n\t\t\t\t\t'themes': args.themes,\r\n\t\t\t\t}.items():\r\n\t\t\tif condition:\r\n\t\t\t\tfieldnames.append(name)\r\n\t\t\t\r\n\t\tif args.criticsScore or args.developers or args.genres or args.publishers or args.releaseDate or args.themes:\r\n\t\t\tprepare(\r\n\t\t\t\t'metadata',\r\n\t\t\t\t{}, # fieldnames are added separateley together with their related originalMetadata fields\r\n\t\t\t\tdbField='METADATA.value AS metadata',\r\n\t\t\t\tdbRef='MasterList AS METADATA',\r\n\t\t\t\tdbCondition='METADATA.releaseKey=MasterList.releaseKey AND METADATA.gamePieceTypeId={}'.format(id('meta')),\r\n\t\t\t\tdbResultField='MasterDB.metadata'\r\n\t\t\t)\r\n\r\n\t\tif args.originalReleaseDate:\r\n\t\t\tprepare(\r\n\t\t\t\t'originalMetadata',\r\n\t\t\t\t{}, # fieldnames are added separateley together with their related metadata fields\r\n\t\t\t\tdbField='ORIGINALMETADATA.value AS originalMetadata',\r\n\t\t\t\tdbRef='MasterList AS ORIGINALMETADATA',\r\n\t\t\t\tdbCondition='ORIGINALMETADATA.releaseKey=MasterList.releaseKey AND ORIGINALMETADATA.gamePieceTypeId={}'.format(id('originalMeta')),\r\n\t\t\t\tdbResultField='MasterDB.originalMetadata'\r\n\t\t\t)\r\n\r\n\t\tif args.playtime:\r\n\t\t\tprepare(\r\n\t\t\t\t'playtime',\r\n\t\t\t\t{'gameMins': True},\r\n\t\t\t\tdbField='GAMETIMES.minutesInGame AS time',\r\n\t\t\t\tdbRef='GAMETIMES',\r\n\t\t\t\tdbCondition='GAMETIMES.releaseKey=MasterList.releaseKey',\r\n\t\t\t\tdbResultField='sum(MasterDB.time)'\r\n\t\t\t)\r\n\r\n\t\tif args.lastPlayed:\r\n\t\t\tprepare(\r\n\t\t\t\t'lastPlayed',\r\n\t\t\t\t{'lastPlayed': True},\r\n\t\t\t\tdbField='LASTPLAYEDDATES.lastPlayedDate as lastPlayed',\r\n\t\t\t\tdbCustomJoin='LEFT JOIN LASTPLAYEDDATES ON LASTPLAYEDDATES.gameReleaseKey=MasterList.releaseKey',\r\n\t\t\t\tdbResultField='MasterDB.lastPlayed'\r\n\t\t\t)\r\n\r\n\r\n\t\tif args.tags:\r\n\t\t\tprepare(\r\n\t\t\t\t'tags',\r\n\t\t\t\t{'tags': True},\r\n\t\t\t\tdbField='USERRELEASETAGS.tag AS tags',\r\n\t\t\t\tdbCustomJoin='LEFT JOIN USERRELEASETAGS ON USERRELEASETAGS.releaseKey=MasterList.releaseKey',\r\n\t\t\t\tdbResultField='GROUP_CONCAT(MasterDB.tags)'\r\n\t\t\t)\r\n\r\n\t\tprepare(  # Grab a list of DLCs for filtering, regardless of whether we're exporting them or not\r\n\t\t\t'dlcs',\r\n\t\t\t{'dlcs': args.dlcs},\r\n\t\t\t# concatenate all dlcs of the game in one list to make sure all dlcs from the different platforms are found\r\n\t\t\tdbField=\"\"\"DLC.value AS dlcs,\r\n\t\t\t\t\t\tCASE\r\n\t\t\t\t\t\t\tWHEN DLC.value IS NULL OR DLC.value IN ('{\"dlcs\":null}', '{\"dlcs\":[]}')\r\n\t\t\t\t\t\t\tTHEN NULL\r\n\t\t\t\t\t\t\tELSE REPLACE(REPLACE(DLC.value, '{\"dlcs\":[', ''), ']}', '')\r\n\t\t\t\t\t\tEND AS dlcList\"\"\",\r\n\t\t\tdbRef='MasterList AS DLC',\r\n\t\t\tdbCondition='(DLC.releaseKey=MasterList.releaseKey) AND (DLC.gamePieceTypeId={})'.format(id('dlcs')),\r\n\t\t\tdbResultField=\"\"\"'{\"dlcs\":[' || COALESCE(GROUP_CONCAT(MasterDB.dlcList), '') || ']}'\"\"\"\r\n\t\t)\r\n\r\n\t\tif args.isHidden:\r\n\t\t\tprepare(\r\n\t\t\t\t'isHidden',\r\n\t\t\t\t{'isHidden': True},\r\n\t\t\t\tdbField='UserReleaseProperties.isHidden AS isHidden',\r\n\t\t\t\tdbCustomJoin='LEFT JOIN USERRELEASEPROPERTIES ON USERRELEASEPROPERTIES.releaseKey=MasterList.releaseKey',\r\n\t\t\t\tdbResultField='CASE WHEN MasterDB.isHidden = 1 THEN \\'True\\' ELSE \\'False\\' END'\r\n\t\t\t)\r\n\r\n\t\tif args.osCompatibility:\r\n\t\t\tprepare(\r\n\t\t\t\t'osCompatibility',\r\n\t\t\t\t{'osCompatibility': True},\r\n\t\t\t\t# concatenate lists of operating systems because different platforms can support different systems\r\n\t\t\t\tdbField=\"\"\"OSCOMPATIBILITY.value AS osCompatibility,\r\n\t\t\t\t\t\t\tCASE\r\n\t\t\t\t\t\t\t\tWHEN OSCOMPATIBILITY.value IS NULL OR OSCOMPATIBILITY.value IN ('{\"supported\":[]}', '{\"supported\":null}') \r\n\t\t\t\t\t\t\t\tTHEN NULL \r\n\t\t\t\t\t\t\t\tELSE REPLACE(REPLACE(OSCOMPATIBILITY.value, '{\"supported\":[', ''), ']}', '')\r\n\t\t\t\t\t\t\tEND AS osList\"\"\",\r\n\t\t\t\tdbRef='MasterList AS OSCOMPATIBILITY',\r\n\t\t\t\tdbCondition='OSCOMPATIBILITY.releaseKey=MasterList.releaseKey AND OSCOMPATIBILITY.gamePieceTypeId={}'.format(id('osCompatibility')),\r\n\t\t\t\tdbResultField=\"\"\"'{\"supported\":[' || COALESCE(GROUP_CONCAT(MasterDB.osList), '') || ']}'\"\"\"\r\n\t\t\t)\r\n\r\n\t\tif args.imageBackground or args.imageSquare or args.imageVertical:\r\n\t\t\tprepare(\r\n\t\t\t\t'images',\r\n\t\t\t\t{\r\n\t\t\t\t\t'backgroundImage': args.imageBackground,\r\n\t\t\t\t\t'squareIcon': args.imageSquare,\r\n\t\t\t\t\t'verticalCover': args.imageVertical\r\n\t\t\t\t},\r\n\t\t\t\tdbField='IMAGES.value AS images',\r\n\t\t\t\tdbRef='MasterList AS IMAGES',\r\n\t\t\t\tdbCondition='(IMAGES.releaseKey=MasterList.releaseKey) AND (IMAGES.gamePieceTypeId={})'.format(id('originalImages')),\r\n\t\t\t\tdbResultField='MasterDB.images'\r\n\t\t\t)\r\n\r\n\t\t# Display each game and its details along with corresponding release key grouped by releasesList\r\n\t\tunique_game_data = \"\"\"SELECT {} FROM MasterDB GROUP BY {} ORDER BY MasterDB.title;\"\"\".format(\r\n\t\t\t', '.join(og_resultFields),\r\n\t\t\t', '.join(og_resultGroupBy)\r\n\t\t)\r\n\r\n\t\t# Perform the queries\r\n\t\tcursor.execute(owned_game_database)\r\n\t\tcursor.execute(''.join(og_fields + og_references + og_joins + og_conditions) + og_order)\r\n\t\tcursor.execute(unique_game_data)\r\n\r\n\t\t# Prepare a list of games and DLCs\r\n\t\tresults = []\r\n\t\tdlcs = set()\r\n\t\twhile True:\r\n\t\t\tresult = cursor.fetchone()\r\n\t\t\tif not result: break\r\n\t\t\tresults.append((result[0].split(','), result))\r\n\t\t\td = jld('dlcs', True)\r\n\t\t\tif d:\r\n\t\t\t\tfor dlc in d:\r\n\t\t\t\t\tdlcs.add(dlc)\r\n\t\tresults = natsorted(results, key=lambda r: str.casefold(str(json.loads(r[1][positions['sortingTitle']])['title'])))\r\n\r\n\t\t# Exclude games mistakenly treated as DLCs, such as \"3 out of 10, EP2\"\r\n\t\tfor dlc in options['TreatDLCAsGame']:\r\n\t\t\tdlcs.discard(dlc)\r\n\r\n\t\t# Add dlcs mistakenly treated as games, such as \"Grey Goo - Emergence Campaign\"\r\n\t\tadditionalDLCs = options['TreatReleaseAsDLC']\r\n\t\tfor game in additionalDLCs.keys():\r\n\t\t\tdlcs.update(additionalDLCs[game])\r\n\r\n\t\t# There are spurious random dlcNUMBERa entries in the library, plus a few DLCs which appear\r\n\t\t# multiple times in different ways and are not attached to a game\r\n\t\ttitleExclusion = re.compile(r'^(?:'\r\n\t\t\t\tr'dlc_?[0-9]+_?a'\r\n\t\t\t\tr'|alternative look for yennefer(?:\\s+\\[[a-z]+\\])?'\r\n\t\t\t\tr'|beard and hairstyle set for geralt(?:\\s+\\[[a-z]+\\])?'\r\n\t\t\t\tr'|new quest - contract: missing miners(?:\\s+\\[[a-z]+\\])?'\r\n\t\t\t\tr'|temerian armor set(?:\\s+\\[[a-z]+\\])?'\r\n\t\tr')$')\r\n\r\n\t\t# Compile the CSV\r\n\t\ttry:\r\n\t\t\twith open(args.fileCSV, 'w', encoding='utf-8', newline='') as csvfile:\r\n\t\t\t\twriter = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter=args.delimiter)\r\n\t\t\t\twriter.writeheader()\r\n\t\t\t\tfor (ids, result) in results:\r\n\t\t\t\t\t# Only consider games for the list, not DLCs\r\n\t\t\t\t\tif not args.exportDlcDetails and 0 < len([x for x in ids if x in dlcs]):\r\n\t\t\t\t\t\tcontinue\r\n\r\n\t\t\t\t\ttry:\r\n\t\t\t\t\t\t# JSON string needs to be converted to dict\r\n\t\t\t\t\t\t# For json.load() to work correctly, all double quotes must be correctly escaped\r\n\t\t\t\t\t\ttry:\r\n\t\t\t\t\t\t\trow = {'title': jld('title', True)}\r\n\t\t\t\t\t\t\tif (not row['title']) or (titleExclusion.match(str.casefold(row['title']))):\r\n\t\t\t\t\t\t\t\tcontinue\r\n\t\t\t\t\t\texcept:\r\n\t\t\t\t\t\t\t# No title or {'title': null}\r\n\t\t\t\t\t\t\tcontinue\r\n\r\n\r\n\t\t\t\t\t\t# SortingTitle\r\n\t\t\t\t\t\tif args.sortingTitle:\r\n\t\t\t\t\t\t\ttry:\r\n\t\t\t\t\t\t\t\tsortingTitle = jld('sortingTitle')\r\n\t\t\t\t\t\t\t\trow['sortingTitle'] = sortingTitle['title']\r\n\t\t\t\t\t\t\texcept:\r\n\t\t\t\t\t\t\t\trow['sortingTitle'] = ''\r\n\r\n\t\t\t\t\t\t# OriginalTitle\r\n\t\t\t\t\t\tif args.originalTitle:\r\n\t\t\t\t\t\t\ttry:\r\n\t\t\t\t\t\t\t\toriginalTitle = jld('originalTitle')\r\n\t\t\t\t\t\t\t\trow['originalTitle'] = originalTitle['title']\r\n\t\t\t\t\t\t\texcept:\r\n\t\t\t\t\t\t\t\trow['originalTitle'] = ''\r\n\r\n\r\n\t\t\t\t\t\t# Playtime\r\n\t\t\t\t\t\tincludeField(result, 'gameMins', positions['playtime'], paramName='playtime')\r\n\r\n\t\t\t\t\t\t# LastPlayed\r\n\t\t\t\t\t\tincludeField(result, 'lastPlayed', positions['lastPlayed'])\r\n\r\n\t\t\t\t\t\t# Summaries\r\n\t\t\t\t\t\tincludeField(result, 'summary', fieldType=Type.STRING_JSON)\r\n\r\n\t\t\t\t\t\t# Platforms\r\n\t\t\t\t\t\tif args.platforms:\r\n\t\t\t\t\t\t\trkeys = result[positions['releaseKey']].split(',')\r\n\t\t\t\t\t\t\tif any(platform in releaseKey for platform in platforms for releaseKey in rkeys):\r\n\t\t\t\t\t\t\t\trow['platformList'] = set(platforms[platform] for releaseKey in rkeys for platform in platforms if releaseKey.startswith(platform))\r\n\t\t\t\t\t\t\telse:\r\n\t\t\t\t\t\t\t\trow['platformList'] = []\r\n\r\n\t\t\t\t\t\t# User rating\r\n\t\t\t\t\t\tincludeField(result, 'myRating', fieldType=Type.STRING_JSON)\r\n\r\n\t\t\t\t\t\t# Various metadata\r\n\t\t\t\t\t\tif args.criticsScore or args.developers or args.genres or args.publishers or args.releaseDate or args.themes:\r\n\t\t\t\t\t\t\tmetadata = jld('metadata')\r\n\t\t\t\t\t\t\tincludeField(metadata, 'criticsScore', fieldType=Type.INTEGER)\r\n\t\t\t\t\t\t\tincludeField(metadata, 'developers')\r\n\t\t\t\t\t\t\tincludeField(metadata, 'genres')\r\n\t\t\t\t\t\t\tincludeField(metadata, 'publishers')\r\n\t\t\t\t\t\t\tincludeField(metadata, 'releaseDate', fieldType=Type.DATE)\r\n\t\t\t\t\t\t\tincludeField(metadata, 'themes')\r\n\r\n\t\t\t\t\t\t# Original metadata\r\n\t\t\t\t\t\tif args.originalReleaseDate:\r\n\t\t\t\t\t\t\toriginalMetadata = jld('originalMetadata')\r\n\t\t\t\t\t\t\tincludeField(originalMetadata, 'originalReleaseDate', 'releaseDate', fieldType=Type.DATE)\r\n\r\n\t\t\t\t\t\t# Original images\r\n\t\t\t\t\t\tif args.imageBackground or args.imageSquare or args.imageVertical:\r\n\t\t\t\t\t\t\timages = jld('images')\r\n\t\t\t\t\t\t\tincludeField(images, 'backgroundImage', 'background', paramName='imageBackground')\r\n\t\t\t\t\t\t\tincludeField(images, 'squareIcon', paramName='imageSquare')\r\n\t\t\t\t\t\t\tincludeField(images, 'verticalCover', paramName='imageVertical')\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\t# DLCs\r\n\t\t\t\t\t\tif args.dlcs:\r\n\t\t\t\t\t\t\trow['dlcs'] = set()\r\n\t\t\t\t\t\t\tdlcList = jld('dlcs', True)\r\n\t\t\t\t\t\t\tif dlcList == None:\r\n\t\t\t\t\t\t\t\tdlcList = []\r\n\t\t\t\t\t\t\tif options[\"TreatReleaseAsDLC\"]:\r\n\t\t\t\t\t\t\t\trkeys = result[positions['releaseKey']].split(',')\r\n\t\t\t\t\t\t\t\tfor key in rkeys:\r\n\t\t\t\t\t\t\t\t\tif options[\"TreatReleaseAsDLC\"].get(key) != None:\r\n\t\t\t\t\t\t\t\t\t\tdlcList.extend(options[\"TreatReleaseAsDLC\"][key])\r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\tfor dlc in dlcList:\r\n\t\t\t\t\t\t\t\ttry:\r\n\t\t\t\t\t\t\t\t\t# Check the availability of the DLC in the games list (uncertain)\r\n\t\t\t\t\t\t\t\t\td = next(x[1] for x in results if dlc in x[0])\r\n\t\t\t\t\t\t\t\t\tif d:\r\n\t\t\t\t\t\t\t\t\t\trow['dlcs'].add(jld('title', True, d))\r\n\t\t\t\t\t\t\t\texcept StopIteration:\r\n\t\t\t\t\t\t\t\t\tpass\r\n\r\n\t\t\t\t\t\t# Tags\r\n\t\t\t\t\t\tif args.tags:\r\n\t\t\t\t\t\t\tincludeField(result, 'tags', positions['tags'], fieldType=Type.LIST)\r\n\r\n\t\t\t\t\t\t# isHidden\r\n\t\t\t\t\t\tif args.isHidden:\r\n\t\t\t\t\t\t\tincludeField(result, 'isHidden', positions['isHidden'], fieldType=Type.STRING)\r\n\r\n\t\t\t\t\t\t# osCompatibility\r\n\t\t\t\t\t\tif args.osCompatibility:\r\n\t\t\t\t\t\t\trow['osCompatibility'] = set()\r\n\t\t\t\t\t\t\tosCompatibility = jld('osCompatibility')\r\n\t\t\t\t\t\t\tosList = osCompatibility['supported']\r\n\t\t\t\t\t\t\tif osList:\r\n\t\t\t\t\t\t\t\tfor operatingSystem in osList:\r\n\t\t\t\t\t\t\t\t\trow['osCompatibility'].add(operatingSystem['name'])\r\n\r\n\t\t\t\t\t\t# Set conversion, list sorting, empty value reset\r\n\t\t\t\t\t\tfor k,v in row.items():\r\n\t\t\t\t\t\t\tif v:\r\n\t\t\t\t\t\t\t\tif list == type(v) or set == type(v):\r\n\t\t\t\t\t\t\t\t\trow[k] = natsorted(list(row[k]), key=str.casefold)\r\n\t\t\t\t\t\t\t\t\tif not args.pythonLists:\r\n\t\t\t\t\t\t\t\t\t\trow[k] = args.delimiter.join(row[k])\r\n\t\t\t\t\t\t\telse:\r\n\t\t\t\t\t\t\t\trow[k] = ''\r\n\r\n\t\t\t\t\t\twriter.writerow(row)\r\n\t\t\t\t\texcept Exception as e:\r\n\t\t\t\t\t\tprint('Parsing failed on: {}'.format(result))\r\n\t\t\t\t\t\traise e\r\n\t\texcept FileNotFoundError:\r\n\t\t\tprint('Unable to write to “{}”, make sure that the path exists and that you have the write permissions'.format(args.fileCSV))\r\n\t\t\treturn\r\n\r\nif __name__ == \"__main__\":\r\n\t# macOS\r\n\tif platform == \"darwin\":\r\n\t\tdefaultDBlocation = \"/Users/Shared/GOG.com/Galaxy/Storage/galaxy-2.0.db\"\r\n\t# Windows\r\n\telif platform == \"win32\":\r\n\t\tdefaultDBlocation = \"C:\\\\ProgramData\\\\GOG.com\\\\Galaxy\\\\storage\\\\galaxy-2.0.db\"\r\n\r\n\tdef ba(variableName, description, defaultValue=False):\r\n\t\t\"\"\" Boolean argument: creates a default boolean argument with the name of the storage variable and\r\n\t\t\tthe description to be shown in the help screen\r\n\t\t\"\"\"\r\n\t\treturn {\r\n\t\t\t'action': 'store_true',\r\n\t\t\t'required': defaultValue,\r\n\t\t\t'help': description,\r\n\t\t\t'dest': variableName,\r\n\t\t}\r\n\r\n\t# Set up the arguments\r\n\targs = Arguments(\r\n\t\t[\r\n\t\t\t[\r\n\t\t\t\t['-i', '--input'],\r\n\t\t\t\t{\r\n\t\t\t\t\t'default': defaultDBlocation,\r\n\t\t\t\t\t'type': str,\r\n\t\t\t\t\t'nargs': 1,\r\n\t\t\t\t\t'required': False,\r\n\t\t\t\t\t'metavar': 'FN',\r\n\t\t\t\t\t'help': 'pathname of the galaxy2 database',\r\n\t\t\t\t\t'dest': 'fileDB',\r\n\t\t\t\t}\r\n\t\t\t],\r\n\t\t\t[\r\n\t\t\t\t['-o', '--output'],\r\n\t\t\t\t{\r\n\t\t\t\t\t'default': 'gameDB.csv',\r\n\t\t\t\t\t'type': str,\r\n\t\t\t\t\t'nargs': 1,\r\n\t\t\t\t\t'required': False,\r\n\t\t\t\t\t'metavar': 'FN',\r\n\t\t\t\t\t'help': 'pathname of the generated CSV',\r\n\t\t\t\t\t'dest': 'fileCSV',\r\n\t\t\t\t}\r\n\t\t\t],\r\n\t\t\t[\r\n\t\t\t\t['-d'],\r\n\t\t\t\t{\r\n\t\t\t\t\t'default': '\\t',\r\n\t\t\t\t\t'type': str,\r\n\t\t\t\t\t'required': False,\r\n\t\t\t\t\t'metavar': 'CHARACTER',\r\n\t\t\t\t\t'help': 'CSV field separator, defaults to tab',\r\n\t\t\t\t\t'dest': 'delimiter',\r\n\t\t\t\t}\r\n\t\t\t],\r\n\t\t\t[['-a', '--all'], ba('all', '(default) extracts all the fields')],\r\n\t\t\t[['--sorting-title'], ba('sortingTitle', '(user customised) sorting title')],\r\n\t\t\t[['--title-original'], ba('originalTitle', 'original title independent of any user changes')],\r\n\t\t\t[['--my-rating'], ba('myRating', 'user rating score')],\r\n\t\t\t[['--critics-score'], ba('criticsScore', 'critics rating score')],\r\n\t\t\t[['--developers'], ba('developers', 'list of developers')],\r\n\t\t\t[['--dlcs'], ba('dlcs', 'list of dlc titles for the specified game')],\r\n\t\t\t[['--genres'], ba('genres', 'game genres')],\r\n\t\t\t[['--image-background'], ba('imageBackground', 'background image')],\r\n\t\t\t[['--image-square'], ba('imageSquare', 'square icon')],\r\n\t\t\t[['--image-vertical'], ba('imageVertical', 'vertical cover image')],\r\n\t\t\t[['--platforms'], ba('platforms', 'list of platforms the game is available on')],\r\n\t\t\t[['--publishers'], ba('publishers', 'list of publishers')],\r\n\t\t\t[['--release-date'], ba('releaseDate', '(user customized) release date of the software')],\r\n\t\t\t[['--release-date-original'], ba('originalReleaseDate', 'original release date independent of any user changes')],\r\n\t\t\t[['--summary'], ba('summary', 'game summary')],\r\n\t\t\t[['--tags'], ba('tags', 'user tags')],\r\n\t\t\t[['--hidden'], ba('isHidden', 'is gamne hidden in galaxy client')],\r\n\t\t\t[['--os-compatibility'], ba('osCompatibility', 'list of supported operating systems')],\r\n\t\t\t[['--themes'], ba('themes', 'game themes')],\r\n\t\t\t[['--playtime'], ba('playtime', 'time spent playing the game')],\r\n\t\t\t[['--last-played'], ba('lastPlayed', 'last time the game was played')],\r\n\t\t\t[['--dlcs-details'], ba('exportDlcDetails', 'add a separate entry for each dlc with all available information to the exported csv')],\r\n\t\t\t[['--py-lists'], ba('pythonLists', 'export lists as Python parseable instead of delimiter separated strings')],\r\n\t\t],\r\n\t\tdescription='GOG Galaxy 2 exporter: scans the local Galaxy 2 database to export a list of games and related information into a CSV'\r\n\t)\r\n\r\n\tif not args.anyOption(['delimiter', 'fileCSV', 'fileDB', 'pythonLists', 'exportDlcDetails']):\r\n\t\targs.extractAll()\r\n\tif exists(args.fileDB):\r\n\t\textractData(args)\r\n\telse:\r\n\t\tprint('Unable to find the DB “{}”, make sure that {}'.format(\r\n\t\t\targs.fileDB,\r\n\t\t\t'GOG Galaxy 2 is installed' if defaultDBlocation == args.fileDB else 'you specified the correct path'\r\n\t\t))\r\n\r\n"
  },
  {
    "path": "helper_scripts/print_gameDB.py",
    "content": "\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 = \"\"\n\n# Index for the game.\ni = 0\n\n# Open csv file to reader\nwith open('../gameDB.csv', 'r', encoding='utf-8') as csv_file:\n    reader = csv.reader(csv_file)\n\n    # The name of the game is on the first part of the row\n    for row in reader:\n        row_string = ' '.join([str(elem) for elem in row])\n\n        name = row[0].split('\\t')\n\n        # Add the index to the game.\n        if i > 0:\n            my_games += str(i) + \". \" + name[0] + \"\\n\"\n        print(name[0])\n        i += 1\n\n# Write the string to file.\nwith open('my_games.txt', 'w', encoding='utf-8') as games_file:\n    games_file.write(my_games)\n\n# Close the file.\ngames_file.close()\n"
  },
  {
    "path": "readme.md",
    "content": "# 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 use\n\n1. Install Python 3, through Windows Store or manually if you prefer\n2. 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\n3. 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\n4. Install python's requirements:\n   ```\n   python -m pip install csv natsort\n   ```\n5. Export the CSV with:\n   ```\n   python galaxy_library_export.py\n   ```\n\nBy default this scripts export everything it can into the CSV. If you would like to customize the results, read below.\n\n## Usage\n\nThrough 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.\n\nIf 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.\n\nWhen a different locale wants a different CSV delimiter (such as the Italian), you can manually specify the character to use (`-d <character>`).\n\nAlso, you can manually specify the database location (`-i`) and the CSV location (`-o`), instead of using the default ones.\n\nIf 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.\n\nIf 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.\n\n## settings.json\n\nThe 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.\n\n- *TreatDLCAsGame*: Aarray of release-keys which should be treated as a game instead of a dlc\n  - Used to mark games which are (mistakenly) treated as dlcs by the gog galaxy client as games.\n  - All entries with a matching release-key will be exported with all available data.\n  - This will not change the link between a dlc and his parent game if it really is a dlc.\n- *TreatReleaseAsDLC*: Dictionary which maps the releaseKey of a game to a list of dlcs.\n  - Used to mark any release which is (mistakenly) treated as a game by the gog galaxy client as dlc.\n  - The dlcs specified in the settings.json are joined with the list of dlcs found in the database.\n  - *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.\n  - 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.\n\n\n## Dependencies\n\n- Python 3\n  - csv\n  - natsort\n\n## Platform Support\n\nAll 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.\n\n## Wiki\n\nCheck the [Wiki tab](https://github.com/AB1908/GOG-Galaxy-Export-Script/wiki).\n\n## Roadmap \n\nCheck the [Projects tab](https://github.com/AB1908/GOG-Galaxy-Export-Script/projects).\n\n## Contribution\n\nFeel free to add issues and pull requests.\n\n## License\n\nThis repository is licensed under the [MIT License](https://github.com/AB1908/GOG-Galaxy-Export-Script/blob/master/LICENSE).\n"
  },
  {
    "path": "settings.json",
    "content": "{\n\t\"TreatDLCAsGame\": [\n\t\t\"epic_1317e4e3b3ed40c289dde85b194347d3\"\n\t],\n\t\"TreatReleaseAsDLC\": {\n\t\t\"steam_290790\": [\"steam_357180\",\"steam_341810\"],\n\t\t\"steam_365450\": [\"steam_408710\"]\n\t}\n}"
  }
]