Files
new_dbsync/update_mongo.py
T

208 lines
6.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import time
import sys
import url.urls
import url.process
import api.tvmaze.cast as mazecast
import api.tvmaze.crew as mazecrew
import api.tvmaze.episodes as mazeepisodes
import api.tvmaze.series as mazeseries
import db.sql
import jprocessor.process
import logs.Logger
from db.functions import settype, dbmongo
from datetime import datetime
response = None
def set_apienv(urls, uprocess, jget, dbengine, dbExec, updatesBase, lprint):
"""Populate updates table from the API and return available updates.
Args:
urls: URL configuration with an "updatesurl" key.
uprocess: URL loader instance with `get_data`.
jget: JSON processor with `jconvert`.
dbengine: database engine dict (expects key "engine").
dbExec: database execution helper with `update_tvupdates` and `rawsql_select`.
updatesBase: list-like containing tables (uses index 8 for updates table).
lprint: logger instance with `logprint`.
Returns:
List of tuples (seriesid, timestamp) representing available updates.
"""
updatetable = updatesBase[8]
inputdata = uprocess.get_data(urls["updatesurl"])
availableupdates = jget.jconvert(inputdata)
lprint.logprint("info", f"Retrieved {len(availableupdates)} rows for processing......")
dbExec.update_tvupdates(dbengine["engine"], availableupdates, updatetable)
newupdates = dbExec.rawsql_select(
dbengine["engine"],
"select seriesid,timestamp from updates.tvupdates",
lprint
)
return newupdates
def print_starttime(lprint, ts1):
lprint.logprint("info", f"Sync start time: {str(ts1)}")
def get_tvdb(tvdburls):
return tvdburls.get_data()
def get_tvmaze(tvmazeurls):
return tvmazeurls.get_data()
def main() -> int:
ROOTDIR = os.getcwd()
directories = {}
# Use cross-platform path builders and ensure directories exist
LOGDIR = os.path.join(ROOTDIR, "log")
directories["SERIESDIR"] = os.path.join(ROOTDIR, "cache", "series") + os.sep
directories["EPISODEDIR"] = os.path.join(ROOTDIR, "cache", "episodes") + os.sep
directories["CASTDIR"] = os.path.join(ROOTDIR, "cache", "cast") + os.sep
directories["CREWDIR"] = os.path.join(ROOTDIR, "cache", "crew") + os.sep
# Create directories if missing so later code doesn't fail
try:
os.makedirs(LOGDIR, exist_ok=True)
os.makedirs(os.path.join(ROOTDIR, "cache", "series"), exist_ok=True)
os.makedirs(os.path.join(ROOTDIR, "cache", "episodes"), exist_ok=True)
os.makedirs(os.path.join(ROOTDIR, "cache", "cast"), exist_ok=True)
os.makedirs(os.path.join(ROOTDIR, "cache", "crew"), exist_ok=True)
except Exception:
# If directory creation fails, let the logging initialization surface the error
pass
lprint = logs.Logger.LPrint(LOGDIR, False)
lprint.start_log()
import settings.config
config_options = settings.config.Config(ROOTDIR)
options = config_options.config_options
dbtype, apitype = options["dbtype"], options["apitype"]
dbs = settype(dbtype, apitype, options)
dbclass, dbengine, apiengine = dbs.dbclass, dbs.dbengine, dbs.apiengine
uprocess = url.process.Loadurl()
TVMAZEURLS = url.urls.Tvmazeurls()
TVMAZE_URLS = get_tvmaze(TVMAZEURLS)
TVDBURLS = url.urls.TVDBurls()
TVDB_URLS = get_tvdb(TVDBURLS)
#mseries = mazeseries.tvshow()
#mcast = mazecast.cast()
#mepisode = mazeepisodes.episodes()
#mcrew = mazecrew.crew()
countrydictionary = {}
updateRange = None
lprint.logprint("info", "Creating JSON instance for data retrieval.")
jget = jprocessor.process.Jsonload()
lprint.logprint("info", "JSON instance created.")
lprint.logprint("info", f"Update type is {options['updatetype']}")
timedifference = options["updatetype"]
currenttimestamp = time.time()
if timedifference.lower() == "full":
timedifference = int(currenttimestamp)
basetime = str(currenttimestamp).split(".")
currenttimestamp = int(basetime[0])
ts1 = str(datetime.now()).split(".")[0]
lprint.logprint("info", "Sync start time: " + str(ts1))
print_starttime(lprint, ts1)
dboBase = dbengine["dboBase"]
dbExec = db.sql.Dbexec()
createTables = dbclass["Createtables"]
createTempTables = dbclass["CreateTemptables"]
new_updates = {}
ct = createTempTables(
dbengine["session"],
dbengine["metadata"],
dbengine["engine"]
)
tempTableList = ct.tablelist
if options["apitype"].lower() == "tvmaze":
try:
new_updates = set_apienv(
TVMAZE_URLS,
uprocess,
jget,
dbengine,
dbExec,
tempTableList,
lprint,
)
except Exception as exc: # noqa: PLC0301 - broad handler to log failures
lprint.logprint("error", f"Failed to set API env: {exc}")
return 1
from cache.process import Cacher
cacher = Cacher()
try:
cacher.process(options, jget, new_updates, ROOTDIR, TVMAZE_URLS)
except Exception as exc:
lprint.logprint("error", f"Cache processing failed: {exc}")
return 1
timeDifference = options["updatetype"]
currentTimestamp = time.time()
if timeDifference.lower() == "full" or options["initload"] == "1":
tdifference = 0
else:
tdifference = int(currentTimestamp) - int(
options[options["updatetype"]]
)
needed_updates = []
updateList = []
for result in new_updates:
updateid = result[0]
updatestamp = int(result[1])
tdif = int(tdifference)
if updatestamp > tdif:
needed_updates.append(updateid)
updateList.append(updateid)
lprint.logprint("info", "Update Type: " + str(options["updatetype"]))
updateRange = len(updateList)
lprint.logprint("info", "Number of updates: " + str(updateRange))
try:
cacher.update_cache(
jget, updateList, ROOTDIR, TVMAZE_URLS, options, lprint
)
except Exception as exc:
lprint.logprint("error", f"update_cache failed: {exc}")
return 1
countrylisting = dbExec.rawsql_select(
dbengine["engine"],
"select row_id, country_name from dbo.countrydata",
lprint
)
for country in countrylisting:
country_name = country[1]
country_id = country[0]
countrydictionary[country_name] = country_id
mongo_updater = dbmongo(options)
mongo_updater.update_mongo(
updateList,
ROOTDIR,
directories,
jget,
countrydictionary,
dbExec,
dbengine,
dboBase,
lprint,
)
return 0
if __name__ == "__main__":
sys.exit(main())