Skip to content

Instantly share code, notes, and snippets.

@altbdoor
Last active March 24, 2024 16:24
Show Gist options
  • Select an option

  • Save altbdoor/8c3099baf957c8c8add30f2db657ea42 to your computer and use it in GitHub Desktop.

Select an option

Save altbdoor/8c3099baf957c8c8add30f2db657ea42 to your computer and use it in GitHub Desktop.
Pack the GOG games entries into a SQLite database
#!/usr/bin/env python
"""
Pack the GOG games entries into a SQLite database
License: WTFPL
https://github.com/ezerear/gog-games.to_backup
"""
import json
import os
import sqlite3
from datetime import datetime
ALWAYS_PURGE_DB = os.getenv("PURGE", "False").lower() == "true"
DB_PATH = "./games.db"
if ALWAYS_PURGE_DB:
if os.path.exists(DB_PATH):
os.remove(DB_PATH)
con = sqlite3.connect(DB_PATH)
cur = con.cursor()
cur.execute(
"""
CREATE TABLE games (
id INTEGER PRIMARY KEY,
title TEXT,
developer TEXT,
publisher TEXT,
category TEXT,
last_upload DATETIME,
linksGameJSON TEXT,
linksPatchesJSON TEXT,
linksGoodiesJSON TEXT
);
"""
)
con.commit()
def convert_str_epoch_to_iso(input_str: str) -> str:
convert_input = int(input_str)
convert_input = datetime.fromtimestamp(convert_input)
return convert_input.strftime("%Y-%m-%d %H:%M:%S")
def simplify_links(entries):
simplified_entries = []
for entry in entries:
for link in entry["links"]:
simplified_entries.append(
{
"name": entry["name"],
"link": link["link"],
}
)
return json.dumps(simplified_entries)
games_list = []
for file in os.scandir(path="./games"):
if not file.name.endswith(".json"):
continue
with open(file.path, mode="r", encoding="utf8") as fp:
data = json.load(fp)
games_list.append(
[
int(data["MSG"]["id"]),
data["MSG"]["title"],
data["MSG"]["developer"],
data["MSG"]["publisher"],
data["MSG"]["category"],
convert_str_epoch_to_iso(data["MSG"]["last_upload"]),
simplify_links(data["MSG"]["links"]["GAME"]),
simplify_links(data["MSG"]["links"]["PATCHES"]),
simplify_links(data["MSG"]["links"]["GOODIES"]),
]
)
cur.executemany("INSERT INTO games VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", games_list)
con.commit()
cur.execute("VACUUM")
con.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment