def scanCollections(self): # scan collections for movies self._logger.debug("Updating collections ...") cursor = self._connection.cursor(dictionary=True, buffered=True) cursor.execute( "SELECT idCollection, scraperID, scraperName FROM movie_collections WHERE forceUpdate = 1 OR title IS NULL;" ) for c in cursor.fetchall(): for s in self._scrapers: if s.__class__.__name__ == c["scraperName"]: self._logger.debug("Getting " + str(s.__class__.__name__) + " results for id" + str(c["scraperID"])) data = s.getCollection(c["scraperID"]) queryData = { "idCollection": c["idCollection"], "title": data["title"], "overview": data["overview"], "icon": encodeImg(data["icon"]), "fanart": encodeImg(data["fanart"]), "premiered": data["premiered"], "forceUpdate": 0, } cursor.execute( "UPDATE movie_collections SET title = %(title)s, icon = %(icon)s, fanart = %(fanart)s,premiered = %(premiered)s, overview = %(overview)s, forceUpdate = %(forceUpdate)s WHERE idCollection = %(idCollection)s;", queryData, ) break self._logger.debug("End of collections update")
def scanSeasons(self): # scan seasons for a tv_show commit = False scraperID = self._tvs[self._currentTVS]["scraperID"] idShow = self._tvs[self._currentTVS]["idShow"] cursor = self._connection.cursor(dictionary=True, buffered=True) noUpdate = [] existingSeasons = [] cursor.execute( "SELECT season, forceUpdate FROM seasons WHERE idShow = %(idShow)s;", {"idShow": idShow}, ) for s in cursor.fetchall(): existingSeasons.append(s["season"]) if s["forceUpdate"] != 1: noUpdate.append(s["season"]) for s in self._scrapers: if s.__class__.__name__ == self._tvs[ self._currentTVS]["scraperName"]: self._logger.debug("Getting " + str(s.__class__.__name__) + " results") for season in self._seasons: if season not in existingSeasons: # season don't already exists, and must be created data = s.getTVSSeason(scraperID, season) queryData = { "idShow": idShow, "title": data["title"], "overview": data["overview"], "icon": encodeImg(data["icon"]), "premiered": data["premiered"], "forceUpdate": 0, "season": season, } cursor.execute( "INSERT INTO seasons (idShow, title, icon, season, premiered, overview, forceUpdate) VALUES (%(idShow)s, %(title)s, %(icon)s, %(season)s, %(premiered)s, %(overview)s, %(forceUpdate)s);", queryData, ) commit = True elif season not in noUpdate: # season already exists and must be updated data = s.getTVSSeason(scraperID, season) queryData = { "idShow": idShow, "title": data["title"], "overview": data["overview"], "icon": encodeImg(data["icon"]), "premiered": data["premiered"], "forceUpdate": 0, } cursor.execute( "UPDATE seasons SET title = %(title)s, icon = %(icon)s, season = %(season)s, premiered = %(premiered)s, overview = %(overview)s, forceUpdate = %(forceUpdate)s WHERE idShow = %(idShow)s;", queryData, ) commit = True break return commit
def scanUpcomingEpisodes(self): cursor = self._connection.cursor(dictionary=True, buffered=True) cursor.execute( "DELETE FROM upcoming_episodes WHERE date < DATE(SYSDATE())") cursor.execute( "SELECT idShow, scraperName, scraperID " "FROM tv_shows " "WHERE multipleResults IS NULL AND idShow NOT IN (SELECT idShow FROM upcoming_episodes)" ) for tvs in cursor.fetchall(): for s in self._scrapers: if s.__class__.__name__ == tvs["scraperName"]: self._logger.info("Getting " + str(s.__class__.__name__) + " results") ep = s.getUpcomingEpisode(tvs["scraperID"]) if ep is not None: queryData = { "title": ep.get("title"), "overview": ep.get("overview"), "season": ep.get("season"), "episode": ep.get("episode"), "date": ep.get("date"), "icon": encodeImg(ep.get("icon")), "idShow": tvs["idShow"], } cursor.execute( "INSERT INTO upcoming_episodes (title, overview, season, episode, date, icon, idShow) " "VALUES (%(title)s, %(overview)s, %(season)s, %(episode)s, %(date)s, %(icon)s, %(idShow)s)", queryData, ) self._connection.commit() self._logger.debug(str(cursor.rowcount) + "were affected")
def scanShowData(self): # scan tags and people for a tv_show commit = False scraperID = self._tvs[self._currentTVS]["scraperID"] idShow = self._tvs[self._currentTVS]["idShow"] cursor = self._connection.cursor(dictionary=True, buffered=True) for s in self._scrapers: if s.__class__.__name__ == self._tvs[ self._currentTVS]["scraperName"]: self._logger.debug("Getting " + str(s.__class__.__name__) + " results") # tags part newTags = [] for t in s.getTags(scraperID): cursor.execute( "SELECT idTag FROM tags where name = %(name)s AND value = %(value)s;", { "name": t[0], "value": t[1] }, ) idTag = cursor.fetchone() if idTag == None: # create tag if new cursor.execute( "INSERT INTO tags (name, value, icon) VALUES (%(name)s, %(value)s, %(icon)s);", { "name": t[0], "value": t[1], "icon": encodeImg(t[2]) }, ) # get tag id cursor.execute( "SELECT idTag FROM tags where name = %(name)s AND value = %(value)s;", { "name": t[0], "value": t[1] }, ) idTag = cursor.fetchone() commit = True newTags.append(idTag["idTag"]) # get existing tags for this tvs cursor.execute( "SELECT idTag FROM tags_link WHERE mediaType = 2 AND idMedia = %(idShow)s;", {"idShow": idShow}, ) existingTags = [] for i in cursor.fetchall(): existingTags.append(i["idTag"]) # link new tags to this tv_show for i in newTags: if i not in existingTags: cursor.execute( "INSERT INTO tags_link (idTag, idMedia, mediaType) VALUES (%(idTag)s, %(idShow)s, 2);", { "idTag": i, "idShow": idShow }, ) commit = True # people part tvsPeople = s.getPeople(scraperID) tvsPeopleIDs = [] for p in tvsPeople: cursor.execute( "SELECT idPers FROM people WHERE name = %(name)s;", {"name": p[0]}, ) idPers = cursor.fetchone() if idPers == None: # create person if new cursor.execute( "INSERT INTO people (name) VALUES (%(name)s);", {"name": p[0]}, ) # get person id cursor.execute( "SELECT idPers FROM people WHERE name = %(name)s;", {"name": p[0]}, ) idPers = cursor.fetchone() commit = True tvsPeopleIDs.append(idPers["idPers"]) # get existing people for this tvs cursor.execute( "SELECT idPers FROM people_link WHERE mediaType = 2 AND idMedia = %(idShow)s;", {"idShow": idShow}, ) existingPers = [] for i in cursor.fetchall(): existingPers.append(i["idPers"]) # link new tags to this tv_show for i in range(len(tvsPeopleIDs)): if tvsPeopleIDs[i] not in existingPers: cursor.execute( "INSERT INTO people_link (idPers, idMedia, mediaType, role) VALUES (%(idPers)s, %(idShow)s, 2, %(role)s);", { "idPers": tvsPeopleIDs[i], "idShow": idShow, "role": tvsPeople[i][1], }, ) return commit
def scanEpisode(self, path, item): extension = item[item.rfind(".") + 1:] self._logger.debug("The extension for: " + self._currentTVS + " is: " + extension) cursor = self._connection.cursor(dictionary=True, buffered=True) commit = False if extension in self._supportedFiles and self._currentTVS: self._logger.debug("This is a supported file") # create entry for episode season = re.findall("(?i)(?:s)(\\d+)(?:e)", item) episode = re.findall("(?i)(?:s\\d+e)(\\d+)(?:\\.)", item) if len(season) > 0 and len(episode) > 0: season = int(season[0]) if season not in self._seasons: self._seasons.append(season) episode = int(episode[0]) epCode = str(season) + "." + str(episode) self._logger.debug("The episode code is: " + str(epCode)) if epCode not in self._existingEp or epCode in self._forceUpdateEp: self._logger.debug( "No entries are available for this episode or it is marked as forceUpdate" ) # create empty dict result = { "title": item, "overview": None, "icon": None, "season": season, "episode": episode, "rating": None, "id": None, } for s in self._scrapers: if (s.__class__.__name__ == self._tvs[self._currentTVS] ["scraperName"]): self._logger.debug("Getting " + str(s.__class__.__name__) + " results") result.update( s.getTVSEp( self._tvs[self._currentTVS]["scraperID"], season, episode, self._tvs[self._currentTVS]["scraperData"], )) break forceUpdate = 0 if "overview" not in result or ("overview" in result and result["overview"] == ""): self._logger.debug( "Episode overview not available, setting as future forceUpdate" ) forceUpdate = 1 if result["id"] == None: self._logger.warning("Episode ID is null") if path != "": filePath = (self._tvs[self._currentTVS]["path"] + "/" + path + "/" + item) else: filePath = self._tvs[ self._currentTVS]["path"] + "/" + item if epCode not in self._existingEp: self._logger.debug("Creating new entry") data = ( result["title"], result["overview"], encodeImg(result["icon"]), result["season"], result["episode"], result["rating"], self._tvs[self._currentTVS]["scraperName"], result["id"], addFile(filePath.encode(), 1), datetime.now().strftime("%Y-%m-%d-%H:%M:%S"), self._tvs[self._currentTVS]["idShow"], forceUpdate, ) cursor.execute( "INSERT INTO episodes (title, overview, icon, season, episode, rating, scraperName, scraperID, idVid, addDate, idShow, forceUpdate) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);", data, ) commit = True elif epCode in self._forceUpdateEp: self._logger.debug( "Updating existing entry (forceUpdate)") data = ( result["title"], result["overview"], encodeImg(result["icon"]), result["season"], result["episode"], result["rating"], self._tvs[self._currentTVS]["scraperName"], result["id"], self._tvs[self._currentTVS]["idShow"], forceUpdate, self._idUpdateEp[epCode], ) cursor.execute( "UPDATE episodes SET title = %s, overview = %s, icon = %s, season = %s, episode = %s, rating = %s, scraperName = %s, scraperID = %s, idShow = %s, forceUpdate = %s WHERE idEpisode = %s;", data, ) commit = True self._logger.debug(b"Updating database with: " + str(data).encode()) else: self._logger.debug( "Entries for this episode already exists") else: self._logger.warning( "Cannot extract season or episode from file name") if commit: self._connection.commit() self._logger.debug(str(cursor.rowcount) + "were affected")
def scanTVS(self, path, item): cursor = self._connection.cursor(dictionary=True, buffered=True) commit = False if item in self._paths: self._logger.debug("D- Entries for this item exists in database") if (self._tvs[item]["forceUpdate"] and self._tvs[item]["scraperName"] and self._tvs[item]["scraperID"]): self._logger.debug("D- Item is marked as force update") # tvs must be updated result = [] for s in self._scrapers: # create empty dict result = { "title": None, "overview": None, "icon": None, "fanart": None, "premiered": None, "rating": None, "scraperData": None, } if s.__class__.__name__ == self._tvs[item]["scraperName"]: result.update(s.getTVS(self._tvs[item]["scraperID"])) break data = ( result["title"], result["overview"], encodeImg(result["icon"]), encodeImg(result["fanart"]), result["rating"], result["premiered"], self._tvs[item]["idShow"], ) cursor.execute( "UPDATE tv_shows SET title = %s, overview = %s, icon = %s, fanart = %s, rating = %s, premiered = %s, forceUpdate = 0, multipleResults = NULL WHERE idShow = %s;", data, ) commit = True self._logger.debug("Updating database with: " + str(data)) elif self._tvs[item]["multipleResults"]: if self._tvs[item]["multipleResults"][0] == "[": # there are multiple matches for scraper, cannot create entries self._logger.debug( "D- Item match multipleResults, ignoring") else: # the multipleResults field stores a new "search title" self._logger.debug("New search") results = [] for s in self._scrapers: data = s.searchTVS(item) if isinstance(data, dict): data = [data] results += data self._logger.debug("The multipleResults are: " + str(results)) cursor.execute( "UPDATE tv_shows SET multipleResults = %(mR)s WHERE idShow = %(idShow)s;", { "mR": json.dumps(results), "idShow": self._tvs[item]["idShow"], }, ) commit = True else: # tvs is ok, call scan on tvs folder self._seasons = [] self._currentTVS = item # scan for subfolders/files self.scanDir(os.path.join(path, item), True) # scan for seasons if self.scanSeasons(): commit = True # scan for tags and people if self.scanShowData(): commit = True self._logger.debug("D- Item ok, scanning subdirectories") else: # entries for this tvs doesn't exists, create entry with multipleResults self._logger.debug( "Entries for this item doesn't exists in database") results = [] for s in self._scrapers: data = s.searchTVS(item) if isinstance(data, dict): data = [data] results += data cursor.execute( "INSERT INTO tv_shows (title, multipleResults, path) VALUES (%s, %s, %s);", (item, json.dumps(results), item), ) commit = True if commit: self._connection.commit() self._logger.debug(str(cursor.rowcount) + "were affected")
def pickImage(self, data): d = self.pickBest(data) if d is None or d[0:5] == "aHR0c": return d else: return encodeImg(d)
def scanMovie(self, item: bytes): cursor = self._connection.cursor(dictionary=True, buffered=True) commit = False if item in self._paths: self._logger.debug("Entries for this item exists in database") if (self._movies[item]["forceUpdate"] and self._movies[item]["scraperName"] and self._movies[item]["scraperID"]): self._logger.debug("Item is marked as force update") # movie must be updated result = [] for s in self._scrapers: # create empty dict result = { "title": None, "desc": None, "icon": None, "fanart": None, "premiered": None, "rating": None, "scraperData": None, "collection": None, } if s.__class__.__name__ == self._movies[item][ "scraperName"]: result.update( s.getMovie(self._movies[item]["scraperID"])) break idCollection = None if result["collection"] is not None: queryData = { "id": result["collection"], "name": self._movies[item]["scraperName"], } cursor.execute( "SELECT idCollection FROM movie_collections WHERE scraperID = %(id)s AND scraperName = %(name)s;", queryData, ) data = cursor.fetchone() if data is not None and "idCollection" in data: idCollection = data["idCollection"] else: cursor.execute( "INSERT INTO movie_collections (scraperID, scraperName, forceUpdate) VALUES (%(id)s, %(name)s, 1);", queryData, ) cursor.execute( "SELECT idCollection FROM movie_collections WHERE scraperID = %(id)s AND scraperName = %(name)s;", queryData, ) idCollection = cursor.fetchone()["idCollection"] data = { "title": result["title"], "overview": result["overview"], "icon": encodeImg(result["icon"]), "fanart": encodeImg(result["fanart"]), "rating": result["rating"], "premiered": result["premiered"], "idCollection": idCollection, "idMovie": self._movies[item]["idMovie"], } cursor.execute( "UPDATE movies SET title = %(title)s, overview = %(overview)s, icon = %(icon)s, fanart = %(fanart)s, rating = %(rating)s, premiered = %(premiered)s, idCollection = %(idCollection)s, forceUpdate = 0, multipleResults = NULL WHERE idMovie = %(idMovie)s;", data, ) # update tags and people self.scanMovieData( self._movies[item]["scraperName"], self._movies[item]["scraperID"], self._movies[item]["idMovie"], ) commit = True self._logger.debug("Updating database with: " + str(data)) elif self._movies[item]["multipleResults"]: if self._movies[item]["multipleResults"][0] == "[": # there are multiple matches for scraper, cannot create entries self._logger.debug("Item match multipleResults, ignoring") else: # the multipleResults field stores a new "search title" self._logger.debug("New search") results = [] for s in self._scrapers: data = s.searchMovie( self._movies[item]["multipleResults"]) if isinstance(data, dict): data = [data] results += data cursor.execute( "UPDATE movies SET multipleResults = %(mR)s WHERE idMovie = %(idMovie)s;", { "mR": json.dumps(results), "idMovie": self._movies[item]["idMovie"], }, ) commit = True else: # entries for this tvs doesn't exists, create entry with multipleResults self._logger.debug( "Entries for this item doesn't exists in database") item_s = item.decode("utf-8") if item_s[item_s.rfind(".") + 1:] in self._supportedFiles: results = [] for s in self._scrapers: data = s.searchMovie(*self.getName(item)) if isinstance(data, dict): data = [data] results += data cursor.execute( "INSERT INTO movies (title, multipleResults, idVid, addDate) VALUES (%s, %s, %s, %s);", ( item, json.dumps(results), addFile(item, 3), datetime.now().strftime("%Y-%m-%d-%H:%M:%S"), ), ) commit = True if commit: self._connection.commit() self._logger.debug(str(cursor.rowcount) + "were affected")