Wrote LoggingService and TMDBService.
This commit is contained in:
parent
73cf2ab9b1
commit
b65ef44f52
58
Universal/loggingservice.py
Normal file
58
Universal/loggingservice.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
class Debugging:
|
||||||
|
def __init__(self, state):
|
||||||
|
self.state = state
|
||||||
|
|
||||||
|
def getDebugState(self):
|
||||||
|
return self.state
|
||||||
|
|
||||||
|
def setDebugState(self, state):
|
||||||
|
self.state = state
|
||||||
|
|
||||||
|
class Logging:
|
||||||
|
def __init__(self, state, directory):
|
||||||
|
self.state = state
|
||||||
|
self.directory = directory
|
||||||
|
|
||||||
|
def setState(self, state):
|
||||||
|
self.state = state
|
||||||
|
|
||||||
|
def setDirectory(self, directory):
|
||||||
|
self.directory = directory
|
||||||
|
|
||||||
|
def outputFile(self, message):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Output:
|
||||||
|
def __init__(self, d_state, l_state, l_directory="./Output.log"):
|
||||||
|
self.debug = Debugging(d_state)
|
||||||
|
self.log = Logging(l_state, l_directory)
|
||||||
|
|
||||||
|
def infoReport(self, message, console=True):
|
||||||
|
if console:
|
||||||
|
print("[INFO] " + message)
|
||||||
|
if self.log.state:
|
||||||
|
self.log.outputFile("[INFO] " + message)
|
||||||
|
|
||||||
|
def warningReport(self, message, console=True):
|
||||||
|
if console:
|
||||||
|
print("[WARNING] " + message)
|
||||||
|
if self.log.state:
|
||||||
|
self.log.outputFile("[WARNING] " + message)
|
||||||
|
|
||||||
|
def debugReport(self, message, console=True):
|
||||||
|
if console and self.debug.getDebugState():
|
||||||
|
print("[DEBUG] " + message)
|
||||||
|
if self.log.state:
|
||||||
|
self.log.outputFile("[DEBUG] " + message)
|
||||||
|
|
||||||
|
def errorReport(self, message, console=True, quit=False):
|
||||||
|
if console:
|
||||||
|
print("[ERROR] " + message)
|
||||||
|
if self.log.state:
|
||||||
|
self.log.outputFile("[ERROR] " + message)
|
||||||
|
if quit:
|
||||||
|
time.sleep(10)
|
||||||
|
quit()
|
95
Video/Services/tmdbservice.py
Normal file
95
Video/Services/tmdbservice.py
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
class TMDBService:
|
||||||
|
def __init__(self, loggingservice):
|
||||||
|
self.logger = loggingservice
|
||||||
|
self.report(2, "Initializing")
|
||||||
|
try:
|
||||||
|
import tmdbsimple
|
||||||
|
self.tmdb = tmdbsimple
|
||||||
|
self.report(2, "API Found")
|
||||||
|
self.enabled = True
|
||||||
|
except ImportError:
|
||||||
|
self.report(3, "Module 'tmdbsimple' was not found")
|
||||||
|
self.report(3, "Disabling TMDB API")
|
||||||
|
self.enabled = False
|
||||||
|
self.connected = False
|
||||||
|
|
||||||
|
def report(self, level, message):
|
||||||
|
match level:
|
||||||
|
case 0:
|
||||||
|
self.logger.infoReport("[TMDB] " + message)
|
||||||
|
case 1:
|
||||||
|
self.logger.warningReport("[TMDB] " + message)
|
||||||
|
case 2:
|
||||||
|
self.logger.debugReport("[TMDB] " + message)
|
||||||
|
case 3:
|
||||||
|
self.logger.errorReport("[TMDB] " + message)
|
||||||
|
|
||||||
|
def connectToTMDB(self, api_key):
|
||||||
|
self.report(2, "connectToTMDB")
|
||||||
|
if not self.connected:
|
||||||
|
try:
|
||||||
|
self.report(2, "Attempting to Login")
|
||||||
|
self.tmdb.API_KEY = api_key
|
||||||
|
self.connected = True
|
||||||
|
self.report(2, "Connected")
|
||||||
|
except self.tmdb.APIKeyError:
|
||||||
|
self.report(3, "API Key is incorrect")
|
||||||
|
self.report(3, "Disabling TMDB API")
|
||||||
|
self.enabled = False
|
||||||
|
else:
|
||||||
|
self.report(2, "Already connected")
|
||||||
|
|
||||||
|
def isConnected(self):
|
||||||
|
self.report(2, "isConnected")
|
||||||
|
return self.connected
|
||||||
|
|
||||||
|
def searchMovies(self, name):
|
||||||
|
self.report(2, "searchMovies")
|
||||||
|
tmp_search = self.tmdb.Search()
|
||||||
|
tmp_response = tmp_search.movie(query=name)
|
||||||
|
tmp_list = []
|
||||||
|
for movie in tmp_search.results:
|
||||||
|
tmp_runtime = self.tmdb.Movies(movie['id']).info()['runtime']
|
||||||
|
try:
|
||||||
|
tmp_releasedate = movie['release_date']
|
||||||
|
except:
|
||||||
|
tmp_releasedate = "9999"
|
||||||
|
if not tmp_runtime or tmp_runtime is None:
|
||||||
|
tmp_runtime = "0"
|
||||||
|
if not tmp_releasedate or tmp_releasedate is None:
|
||||||
|
tmp_releasedate = "9999"
|
||||||
|
tmp_list.append([movie['title'], int(tmp_runtime), int(tmp_releasedate[:4])])
|
||||||
|
return tmp_list
|
||||||
|
|
||||||
|
def getBestMovie(self, name, length=None, year=None):
|
||||||
|
self.report(2, "getBestMovie")
|
||||||
|
tmp_movies = self.searchMovies(name)
|
||||||
|
if not year and not length:
|
||||||
|
self.report(0, "Does not know year or runtime... Selecting first response.")
|
||||||
|
return tmp_movies[0]
|
||||||
|
elif not year:
|
||||||
|
self.report(0, "Got runtime but no year. Selecting closest match based on runtime.")
|
||||||
|
tmp_time = []
|
||||||
|
for movies in tmp_movies:
|
||||||
|
if movies[1] == None:
|
||||||
|
movies[1] = 0
|
||||||
|
tmp_time.append(movies[1])
|
||||||
|
return tmp_movies[min(enumerate(tmp_time), key=lambda x:abs(x[1]-length))[0]]
|
||||||
|
else:
|
||||||
|
tmp_year = []
|
||||||
|
for movies in tmp_movies:
|
||||||
|
if movies[2] == None:
|
||||||
|
movies[2] = 9999
|
||||||
|
tmp_year.append(movies[2])
|
||||||
|
return tmp_movies[min(enumerate(tmp_year), key=lambda x:abs(x[1]-year))[0]]
|
||||||
|
|
||||||
|
def getShowName(self, name):
|
||||||
|
self.report(2, "getShowName")
|
||||||
|
tmp_search = self.tmdb.Search()
|
||||||
|
tmp_response = tmp_search.tv(query=name)
|
||||||
|
return tmp_search.results[0]
|
||||||
|
|
||||||
|
def getEpisodeName(self, name, season, episode):
|
||||||
|
self.report(2, "getEpisodeName")
|
||||||
|
self.tmdb.Search().tv(query=name)
|
||||||
|
return self.tmdb.TV_Episodes(self.tmdb.results[0]['id'], season, episode).info()['name']
|
Loading…
Reference in New Issue
Block a user