Handle absence of all group and torrent data fields
authorJakob Cornell <jakob+gpg@jcornell.net>
Sat, 9 Oct 2021 20:44:40 +0000 (15:44 -0500)
committerJakob Cornell <jakob+gpg@jcornell.net>
Sat, 9 Oct 2021 20:44:40 +0000 (15:44 -0500)
python/scripts/ingest_ptp_json.py

index 793f6141baf6754e405e19c9467989838bc699a1..50771131ddfbc82a107b7697cd60b93a246270b9 100644 (file)
@@ -21,6 +21,7 @@ for p in paths:
 
 def ingest(doc, db_cursor):
        for group in doc["Movies"]:
+               group_id = group["GroupId"]
                stmt = (
                        "insert into ptp.torrent_group (id, title, year, imdb_id)"
                        + " values (%s, %s, %s, %s)"
@@ -29,7 +30,7 @@ def ingest(doc, db_cursor):
                )
                db_cursor.execute(
                        stmt,
-                       (group["GroupId"], group["Title"], group["Year"], group.get("ImdbId"))
+                       (group_id, group.get("Title"), group.get("Year"), group.get("ImdbId"))
                )
 
                group_cols = "(group_id, torrentid, quality, source, container, codec, resolution, seeders, leechers, snatched)"
@@ -40,13 +41,18 @@ def ingest(doc, db_cursor):
                        + ";"
                )
                for torrent in group["Torrents"]:
+                       id_ = torrent["Id"]
+                       quality = torrent.get("Quality")
+                       source = torrent.get("Source")
+                       container = torrent.get("Container")
+                       codec = torrent.get("Codec")
+                       resolution = torrent.get("Resolution")
+                       seeders = int(torrent["Seeders"]) if "Seeders" in torrent else None
+                       leechers = int(torrent["Leechers"]) if "Leechers" in torrent else None
+                       snatched = int(torrent["Snatched"]) if "Snatched" in torrent else None
                        db_cursor.execute(
                                stmt,
-                               (
-                                       group["GroupId"], torrent["Id"], torrent["Quality"], torrent["Source"], torrent["Container"],
-                                       torrent["Codec"], torrent["Resolution"], int(torrent["Seeders"]), int(torrent["Leechers"]),
-                                       int(torrent["Snatched"]),
-                               )
+                               (group_id, id_, quality, source, container, codec, resolution, seeders, leechers, snatched),
                        )