diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..1a5764f --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,22 @@ +services: + - docker:dind + +stages: + - build-and_push + +variables: + DOCKER_DRIVER: overlay2 + DOCKER_HOST: tcp://docker:2375/ # 设置DOCKER_HOST指向docker服务 + DOCKER_TLS_CERTDIR: "" # 禁用TLS验证(仅在安全环境下使用) + +before_script: + - docker login -u $DOCKER_HUB_USERNAME -p $DOCKER_HUB_PASSWORD + +build-and_push: + stage: build-and_push + script: + - docker build -t media_server_tools . + - docker tag media_server_tools $DOCKER_HUB_USERNAME/media_server_tools:latest + - docker push $DOCKER_HUB_USERNAME/media_server_tools:latest + only: + - main \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 5c768e6..8650a07 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,5 +5,7 @@ WORKDIR /app COPY . /app RUN pip install -r requirements.txt +RUN pip install --index-url https://nexus3.smwap.top:88/repository/python/simple network +RUN pip install --index-url https://nexus3.smwap.top:88/repository/python/simple system CMD ["python", "main.py"] diff --git a/media.py b/api/media.py similarity index 74% rename from media.py rename to api/media.py index ee01477..3ce9728 100644 --- a/media.py +++ b/api/media.py @@ -12,95 +12,101 @@ from api.server.jellyfin import jellyfin from api.server.plex import plex from api.tmdb import tmdb -from system.config import config +from system.config import Config from system.log import log class media: - mediaservertype = None - nastoolsclient = None - meidiaserverclient = None - tmdbclient = None - doubanclient = None - languagelist = None - threadpool = None - updatepeople = None - updateoverview = None - updateseasongroup = None - taskdonespace = None - delnotimagepeople = None - checkmediasearch = None - configload = None - sqlclient = None - configinfo = None - threadnum = None + media_server_type = None + nas_tools_client = None + meidia_server_client = None + tmdb_client = None + douban_client = None + language_list = None + thread_pool = None + update_people = None + update_overview = None + update_season_group = None + task_done_space = None + del_not_image_people = None + check_media_search = None + config_load = None + sql_client = None + config_info = None + thread_num = None semaphore = None - excludepath = None + exclude_path = None + exclude_media = None + update_title = None + update_score = None - def __init__(self, configinfo: config) -> None: + def __init__(self) -> None: """ :param configinfo 配置 """ try: - self.mediaservertype = configinfo.systemdata["mediaserver"] - self.configload = False - self.configinfo = configinfo - self.sqlclient = mediasql(configinfo=configinfo) - self.nastoolsclient = nastools( - host=configinfo.apidata["nastools"]["host"], - authorization=configinfo.apidata["nastools"]["authorization"], - username=configinfo.apidata["nastools"]["username"], - passwd=configinfo.apidata["nastools"]["passwd"], + self.config_info = Config().get_config() + self.media_server_type = self.config_info["system"]["mediaserver"] + self.config_load = False + self.sql_client = mediasql() + self.nas_tools_client = nastools( + host=self.config_info["api"]["nastools"]["host"], + authorization=self.config_info["api"]["nastools"]["authorization"], + username=self.config_info["api"]["nastools"]["username"], + passwd=self.config_info["api"]["nastools"]["passwd"], ) - self.tmdbclient = tmdb( - key=configinfo.apidata["tmdb"]["key"], - host=configinfo.apidata["tmdb"].get( + self.tmdb_client = tmdb( + key=self.config_info["api"]["tmdb"]["key"], + host=self.config_info["api"]["tmdb"].get( "host", "https://api.themoviedb.org/3" ), - proxy=configinfo.apidata["tmdb"].get("proxy", None), + proxy=self.config_info["api"]["tmdb"].get("proxy", None), ) - self.doubanclient = douban( - key=configinfo.apidata["douban"]["key"], - cookie=configinfo.apidata["douban"]["cookie"], + self.douban_client = douban( + key=self.config_info["api"]["douban"]["key"], + cookie=self.config_info["api"]["douban"]["cookie"], ) - self.languagelist = ["zh-CN", "zh-SG", "zh-TW", "zh-HK"] - self.threadnum = configinfo.systemdata["threadnum"] - self.semaphore = BoundedSemaphore(self.threadnum) - self.threadpool = ThreadPoolExecutor(max_workers=self.threadnum) - self.updatepeople = configinfo.systemdata["updatepeople"] - self.updateoverview = configinfo.systemdata["updateoverview"] - self.taskdonespace = configinfo.systemdata["taskdonespace"] - self.delnotimagepeople = configinfo.systemdata["delnotimagepeople"] - self.updateseasongroup = configinfo.systemdata["updateseasongroup"] - self.checkmediasearch = configinfo.systemdata["checkmediasearch"] - self.excludepath = configinfo.systemdata["excludepath"] - if "Emby" in self.mediaservertype: - self.meidiaserverclient = emby( - host=configinfo.apidata["emby"]["host"], - userid=configinfo.apidata["emby"]["userid"], - key=configinfo.apidata["emby"]["key"], + self.language_list = ["zh-CN", "zh-SG", "zh-TW", "zh-HK"] + self.thread_num = self.config_info["system"]["threadnum"] + self.semaphore = BoundedSemaphore(self.thread_num) + self.thread_pool = ThreadPoolExecutor(max_workers=self.thread_num) + self.update_people = self.config_info["system"]["updatepeople"] + self.update_overview = self.config_info["system"]["updateoverview"] + self.task_done_space = self.config_info["system"]["taskdonespace"] + self.del_not_image_people = self.config_info["system"]["delnotimagepeople"] + self.update_season_group = self.config_info["system"]["updateseasongroup"] + self.check_media_search = self.config_info["system"]["checkmediasearch"] + self.exclude_path = self.config_info["system"]["excludepath"] + self.exclude_media = self.config_info["system"]["excludemedia"] + self.update_title = self.config_info["system"]["updatetitle"] + self.update_score = self.config_info["system"]["updatescore"] + if "Emby" in self.media_server_type: + self.meidia_server_client = emby( + host=self.config_info["api"]["emby"]["host"], + userid=self.config_info["api"]["emby"]["userid"], + key=self.config_info["api"]["emby"]["key"], ) - self.configload = True - elif "Jellyfin" in self.mediaservertype: - self.meidiaserverclient = jellyfin( - host=configinfo.apidata["jellyfin"]["host"], - userid=configinfo.apidata["jellyfin"]["userid"], - key=configinfo.apidata["jellyfin"]["key"], + self.config_load = True + elif "Jellyfin" in self.media_server_type: + self.meidia_server_client = jellyfin( + host=self.config_info["api"]["jellyfin"]["host"], + userid=self.config_info["api"]["jellyfin"]["userid"], + key=self.config_info["api"]["jellyfin"]["key"], ) - self.configload = True - elif "Plex" in self.mediaservertype: - self.meidiaserverclient = plex( - host=configinfo.apidata["plex"]["host"], + self.config_load = True + elif "Plex" in self.media_server_type: + self.meidia_server_client = plex( + host=self.config_info["api"]["plex"]["host"], userid="", - key=configinfo.apidata["plex"]["key"], + key=self.config_info["api"]["plex"]["key"], ) - self.configload = True + self.config_load = True else: log().logger.info( - "当前设置媒体服务器[{}]不支持".format(self.mediaservertype) + "当前设置媒体服务器[{}]不支持".format(self.media_server_type) ) - self.configload = False + self.config_load = False except Exception as result: log().logger.info("配置异常错误, {}".format(result)) @@ -110,26 +116,26 @@ def start_scan_media(self): 开始扫描媒体 :return True or False """ - if not self.configload: + if not self.config_load: log().logger.info("配置未正常加载") return False # 获取媒体库根文件夹 - ret, itmes = self.meidiaserverclient.get_items() + ret, itmes = self.meidia_server_client.get_items() if not ret: log().logger.info( "获取[{}]媒体总列表失败, {}".format( - self.mediaservertype, self.meidiaserverclient.err + self.media_server_type, self.meidia_server_client.err ) ) return False - ret, iteminfo = self.meidiaserverclient.get_items_count() + ret, iteminfo = self.meidia_server_client.get_items_count() log().logger.info( "总媒体数量[{}]".format(iteminfo["MovieCount"] + iteminfo["SeriesCount"]) ) if not ret: log().logger.info( "获取[{}]媒体数量失败, {}".format( - self.mediaservertype, self.meidiaserverclient.err + self.media_server_type, self.meidia_server_client.err ) ) return False @@ -140,7 +146,7 @@ def start_scan_media(self): if not item: continue self.__submit_task__(item=item) - while self.semaphore._value != self.threadnum: + while self.semaphore._value != self.thread_num: time.sleep(0.1) return ret @@ -150,7 +156,7 @@ def __submit_task__(self, item): :param item """ self.semaphore.acquire() - self.threadpool.submit(self.__start_task__, item) + self.thread_pool.submit(self.__start_task__, item) def __start_task__(self, item): """ @@ -177,28 +183,30 @@ def __check_media_info__(self, itemlist): or "boxsets" not in item["CollectionType"] ): # 排除文件夹 - is_exclude_path = False - for exclude in self.excludepath: + is_exclude_media = False + for exclude in self.exclude_path: match = re.match(exclude, item["Name"]) if match: - is_exclude_path = True + is_exclude_media = True break - if is_exclude_path: + if is_exclude_media: log().logger.info( - "排除[{}]媒体[{}]".format( - self.mediaservertype, + "排除[{}]媒体文件夹[{}]".format( + self.media_server_type, item["Name"], ) ) continue - ret, items = self.meidiaserverclient.get_items(parentid=item["Id"]) + ret, items = self.meidia_server_client.get_items( + parentid=item["Id"] + ) if not ret: log().logger.info( "获取[{}]媒体[{}]ID[{}]列表失败, {}".format( - self.mediaservertype, + self.media_server_type, item["Name"], item["Id"], - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) continue @@ -206,6 +214,21 @@ def __check_media_info__(self, itemlist): yield tmpitem else: if "Series" in item["Type"] or "Movie" in item["Type"]: + # 排除文件夹 + is_exclude_media = False + for exclude in self.exclude_media: + match = re.match(exclude, item["Name"]) + if match: + is_exclude_media = True + break + if is_exclude_media: + log().logger.info( + "排除[{}]媒体[{}]".format( + self.media_server_type, + item["Name"], + ) + ) + continue yield item except Exception as result: @@ -223,14 +246,14 @@ def __check_media_info_search__(self, item): :param item 媒体信息 """ try: - ret, iteminfo = self.meidiaserverclient.get_item_info(itemid=item["Id"]) + ret, iteminfo = self.meidia_server_client.get_item_info(itemid=item["Id"]) if not ret: log().logger.info( "获取[{}]媒体[{}]ID[{}]信息失败, {}".format( - self.mediaservertype, + self.media_server_type, item["Name"], item["Id"], - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) return False @@ -263,17 +286,17 @@ def __check_media_info_search__(self, item): mediatype = "MOV" else: mediatype = "TV" - ret, mediainfo = self.nastoolsclient.media_info( + ret, mediainfo = self.nas_tools_client.media_info( name=name, year=year, type=mediatype ) if not ret: log().logger.info( "[{}]媒体名称[{}]与原始名称[{}]不一致可能识别错误, NasTools识别媒体[{}]失败, {}".format( - self.mediaservertype, + self.media_server_type, item["Name"], iteminfo["FileName"], name, - self.nastoolsclient.err, + self.nas_tools_client.err, ) ) return False @@ -293,41 +316,41 @@ def __check_media_info_search__(self, item): if len(tvinfo["seasons"]) < iteminfo["ChildCount"]: return False - ret, searchinfo = self.meidiaserverclient.search_movie( + ret, searchinfo = self.meidia_server_client.search_movie( itemid=item["Id"], tmdbid=testtmdbid, name=name, year=year ) if not ret: log().logger.info( "[{}]搜索媒体[{}]ID[{}]TMDB[{}]信息失败, {}".format( - self.mediaservertype, + self.media_server_type, item["Name"], item["Id"], testtmdbid, - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) return False for info in searchinfo: - if "Plex" not in self.mediaservertype: + if "Plex" not in self.media_server_type: info["Type"] = iteminfo["Type"] info["IsFolder"] = iteminfo["IsFolder"] - ret = self.meidiaserverclient.apply_search( + ret = self.meidia_server_client.apply_search( itemid=item["Id"], iteminfo=info ) if not ret: log().logger.info( "[{}]更新媒体[{}]ID[{}]TMDB[{}]信息失败, {}".format( - self.mediaservertype, + self.media_server_type, item["Name"], item["Id"], testtmdbid, - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) return False log().logger.info( "[{}]更新媒体[{}]ID[{}]TMDB[{}]更新为媒体[{}]TMDB[{}]".format( - self.mediaservertype, + self.media_server_type, item["Name"], item["Id"], tmdbid, @@ -353,108 +376,137 @@ def __to_deal_with_item__(self, item): :return True or False """ try: - if self.checkmediasearch: + if self.check_media_search: _ = self.__check_media_info_search__(item=item) - updatename = False - updatepeople = False - updateoverview = False - ret, iteminfo = self.meidiaserverclient.get_item_info(itemid=item["Id"]) + update_name = False + update_people = False + update_overview = False + update_score = False + + ret, item_info = self.meidia_server_client.get_item_info(itemid=item["Id"]) if not ret: log().logger.info( "获取[{}]媒体[{}]ID[{}]信息失败, {}".format( - self.mediaservertype, + self.media_server_type, item["Name"], item["Id"], - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) return False, item["Name"] - if "LockedFields" not in iteminfo: - iteminfo["LockedFields"] = [] + if "LockedFields" not in item_info: + item_info["LockedFields"] = [] tmdbid = None imdbid = None - if "Tmdb" in iteminfo["ProviderIds"]: - tmdbid = iteminfo["ProviderIds"]["Tmdb"] - elif "tmdb" in iteminfo["ProviderIds"]: - tmdbid = iteminfo["ProviderIds"]["tmdb"] + if "Tmdb" in item_info["ProviderIds"]: + tmdbid = item_info["ProviderIds"]["Tmdb"] + elif "tmdb" in item_info["ProviderIds"]: + tmdbid = item_info["ProviderIds"]["tmdb"] + + if "Imdb" in item_info["ProviderIds"]: + imdbid = item_info["ProviderIds"]["Imdb"] + elif "imdb" in item_info["ProviderIds"]: + imdbid = item_info["ProviderIds"]["imdb"] + + douban_media_info = None + original_name = item_info["Name"] + if "CommunityRating" not in item_info: + item_info["CommunityRating"] = "0" + original_score = item_info["CommunityRating"] + if self.update_title: + if not self.__is_chinese__(string=item["Name"]): + if not tmdbid and not imdbid: + log().logger.info( + "[{}]媒体[{}]ID[{}]Tmdb|Imdb不存在".format( + self.media_server_type, item["Name"], item["Id"] + ) + ) + return False, item["Name"] + name = None + if tmdbid: + if "Series" in item["Type"]: + ret, name = self.__get_tmdb_media_name__( + mediatype=1, datatype=1, name=item["Name"], id=tmdbid + ) + else: + ret, name = self.__get_tmdb_media_name__( + mediatype=2, datatype=1, name=item["Name"], id=tmdbid + ) + if imdbid and not name: + if "Series" in item["Type"]: + ret, douban_media_info = self.__get_douban_media_info__( + mediatype=1, name=item["Name"], id=imdbid + ) + else: + ret, douban_media_info = self.__get_douban_media_info__( + mediatype=2, name=item["Name"], id=imdbid + ) + if ret: + if self.__is_chinese__( + string=douban_media_info["title"], mode=2 + ): + name = douban_media_info["title"] - if "Imdb" in iteminfo["ProviderIds"]: - imdbid = iteminfo["ProviderIds"]["Imdb"] - elif "imdb" in iteminfo["ProviderIds"]: - imdbid = iteminfo["ProviderIds"]["imdb"] + if name: - doubanmediainfo = None + item_info["Name"] = name + if "Name" not in item_info["LockedFields"]: + item_info["LockedFields"].append("Name") + update_name = True - if not self.__is_chinese__(string=item["Name"]): - if not tmdbid and not imdbid: - log().logger.info( - "[{}]媒体[{}]ID[{}]Tmdb|Imdb不存在".format( - self.mediaservertype, item["Name"], item["Id"] - ) - ) - return False, item["Name"] - name = None - if tmdbid: - if "Series" in item["Type"]: - ret, name = self.__get_tmdb_media_name__( - mediatype=1, datatype=1, name=item["Name"], id=tmdbid - ) - else: - ret, name = self.__get_tmdb_media_name__( - mediatype=2, datatype=1, name=item["Name"], id=tmdbid - ) - if imdbid and not name: - if "Series" in item["Type"]: - ret, doubanmediainfo = self.__get_douban_media_info__( - mediatype=1, name=item["Name"], id=imdbid - ) - else: - ret, doubanmediainfo = self.__get_douban_media_info__( - mediatype=2, name=item["Name"], id=imdbid - ) - if ret: - if self.__is_chinese__(string=doubanmediainfo["title"], mode=2): - name = doubanmediainfo["title"] - - if name: - originalname = iteminfo["Name"] - iteminfo["Name"] = name - if "Name" not in iteminfo["LockedFields"]: - iteminfo["LockedFields"].append("Name") - updatename = True - - if self.updatepeople and "People" in iteminfo: - updatepeople = self.__update_people__( - item=item, iteminfo=iteminfo, imdbid=imdbid + if self.update_score: + if imdbid: + if not douban_media_info: + if "Series" in item["Type"]: + ret, douban_media_info = self.__get_douban_media_info__( + mediatype=1, name=item["Name"], id=imdbid + ) + else: + ret, douban_media_info = self.__get_douban_media_info__( + mediatype=2, name=item["Name"], id=imdbid + ) + if douban_media_info: + score = douban_media_info["rating"]["value"] + if "CommunityRating" not in item_info["LockedFields"]: + item_info["LockedFields"].append("CommunityRating") + if score != original_score: + update_score = True + item_info["CommunityRating"] = score + + if self.update_people and "People" in item_info: + update_people = self.__update_people__( + item=item, iteminfo=item_info, imdbid=imdbid ) if "Series" in item["Type"]: - ret, seasons = self.meidiaserverclient.get_items( + ret, seasons = self.meidia_server_client.get_items( parentid=item["Id"], type="Season" ) if not ret: log().logger.info( "获取[{}]媒体[{}]ID[{}]信息失败, {}".format( - self.mediaservertype, + self.media_server_type, item["Name"], item["Id"], - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) else: for season in seasons["Items"]: - if "Jellyfin" in self.mediaservertype: - ret, seasoninfo = self.meidiaserverclient.get_item_info( - itemid=season["Id"] + if "Jellyfin" in self.media_server_type: + ret, seasoninfo = ( + self.meidia_server_client.get_item_info( + itemid=season["Id"] + ) ) if not ret: log().logger.info( "获取[{}]媒体[{}]ID[{}]信息失败, {}".format( - self.mediaservertype, + self.media_server_type, season["Name"], season["Id"], - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) continue @@ -469,22 +521,22 @@ def __to_deal_with_item__(self, item): ) log().logger.info( "原始媒体名称[{}] 第[{}]季更新人物".format( - iteminfo["Name"], season["IndexNumber"] + item_info["Name"], season["IndexNumber"] ) ) - ret = self.meidiaserverclient.set_item_info( + ret = self.meidia_server_client.set_item_info( itemid=seasoninfo["Id"], iteminfo=seasoninfo ) - ret, episodes = self.meidiaserverclient.get_items( + ret, episodes = self.meidia_server_client.get_items( parentid=season["Id"], type="Episode" ) if not ret: log().logger.info( "获取[{}]媒体[{}]ID[{}]信息失败, {}".format( - self.mediaservertype, + self.media_server_type, season["Name"], season["Id"], - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) continue @@ -492,16 +544,16 @@ def __to_deal_with_item__(self, item): ( ret, episodeinfo, - ) = self.meidiaserverclient.get_item_info( + ) = self.meidia_server_client.get_item_info( itemid=episode["Id"] ) if not ret: log().logger.info( "获取[{}]媒体[{}]ID[{}]信息失败, {}".format( - self.mediaservertype, + self.media_server_type, episode["Name"], episode["Id"], - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) else: @@ -512,30 +564,30 @@ def __to_deal_with_item__(self, item): ) if not ret: continue - ret = self.meidiaserverclient.set_item_info( + ret = self.meidia_server_client.set_item_info( itemid=episodeinfo["Id"], iteminfo=episodeinfo ) if ret: - if "Jellyfin" in self.mediaservertype: + if "Jellyfin" in self.media_server_type: _ = self.__refresh_people__( item=episode, iteminfo=episodeinfo ) log().logger.info( "原始媒体名称[{}] 第[{}]季 第[{}]集更新人物".format( - iteminfo["Name"], + item_info["Name"], season["IndexNumber"], episode["IndexNumber"], ) ) - if self.updateoverview: - if "Overview" not in iteminfo or not self.__is_chinese__( - string=iteminfo["Overview"] + if self.update_overview: + if "Overview" not in item_info or not self.__is_chinese__( + string=item_info["Overview"] ): if not tmdbid and not imdbid: log().logger.info( "[{}]媒体[{}]ID[{}]Tmdb|Imdb不存在".format( - self.mediaservertype, item["Name"], item["Id"] + self.media_server_type, item["Name"], item["Id"] ) ) return False, item["Name"] @@ -550,45 +602,45 @@ def __to_deal_with_item__(self, item): mediatype=2, datatype=2, name=item["Name"], id=tmdbid ) if ret: - iteminfo["Overview"] = overview - if "Overview" not in iteminfo["LockedFields"]: - iteminfo["LockedFields"].append("Overview") - updateoverview = True + item_info["Overview"] = overview + if "Overview" not in item_info["LockedFields"]: + item_info["LockedFields"].append("Overview") + update_overview = True elif imdbid: - if not doubanmediainfo: + if not douban_media_info: if "Series" in item["Type"]: - ret, doubanmediainfo = self.__get_douban_media_info__( + ret, douban_media_info = self.__get_douban_media_info__( mediatype=1, name=item["Name"], id=imdbid ) else: - ret, doubanmediainfo = self.__get_douban_media_info__( + ret, douban_media_info = self.__get_douban_media_info__( mediatype=2, name=item["Name"], id=imdbid ) - if doubanmediainfo: - if doubanmediainfo["intro"]: - iteminfo["Overview"] = doubanmediainfo["intro"] - if "Overview" not in iteminfo["LockedFields"]: - iteminfo["LockedFields"].append("Overview") - updateoverview = True + if douban_media_info: + if douban_media_info["intro"]: + item_info["Overview"] = douban_media_info["intro"] + if "Overview" not in item_info["LockedFields"]: + item_info["LockedFields"].append("Overview") + update_overview = True if "Series" in item["Type"]: - ret, seasons = self.meidiaserverclient.get_items( + ret, seasons = self.meidia_server_client.get_items( parentid=item["Id"], type="Season" ) if not ret: log().logger.info( "获取[{}]媒体[{}]ID[{}]信息失败, {}".format( - self.mediaservertype, + self.media_server_type, item["Name"], item["Id"], - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) else: seasongroupinfo = None groupid = None - if self.updateseasongroup: - for groupinfo in self.configinfo.systemdata["seasongroup"]: + if self.update_season_group: + for groupinfo in self.config_info["system"]["seasongroup"]: infolist = groupinfo.split("|") if len(infolist) < 2: continue @@ -596,16 +648,16 @@ def __to_deal_with_item__(self, item): groupid = infolist[1] break for season in seasons["Items"]: - ret, episodes = self.meidiaserverclient.get_items( + ret, episodes = self.meidia_server_client.get_items( parentid=season["Id"], type="Episode" ) if not ret: log().logger.info( "获取[{}]媒体[{}]ID[{}]信息失败, {}".format( - self.mediaservertype, + self.media_server_type, season["Name"], season["Id"], - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) continue @@ -613,16 +665,16 @@ def __to_deal_with_item__(self, item): ( ret, episodeinfo, - ) = self.meidiaserverclient.get_item_info( + ) = self.meidia_server_client.get_item_info( itemid=episode["Id"] ) if not ret: log().logger.info( "获取[{}]媒体[{}]ID[{}]信息失败, {}".format( - self.mediaservertype, + self.media_server_type, episode["Name"], episode["Id"], - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) else: @@ -681,7 +733,7 @@ def __to_deal_with_item__(self, item): if not tmdbepisodeinfo: log().logger.info( "原始媒体名称[{}] 第[{}]季 第[{}]集未匹配到TMDB剧集组数据".format( - iteminfo["Name"], + item_info["Name"], season["IndexNumber"], episode["IndexNumber"], ) @@ -728,14 +780,14 @@ def __to_deal_with_item__(self, item): episodeinfo["LockedFields"].append("Name") if "Overview" not in episodeinfo["LockedFields"]: episodeinfo["LockedFields"].append("Overview") - ret = self.meidiaserverclient.set_item_info( + ret = self.meidia_server_client.set_item_info( itemid=episodeinfo["Id"], iteminfo=episodeinfo ) if ret: if overview: log().logger.info( "原始媒体名称[{}] 第[{}]季 第[{}]集更新概述".format( - iteminfo["Name"], + item_info["Name"], season["IndexNumber"], episode["IndexNumber"], ) @@ -743,47 +795,58 @@ def __to_deal_with_item__(self, item): if ommunityrating: log().logger.info( "原始媒体名称[{}] 第[{}]季 第[{}]集更新评分".format( - iteminfo["Name"], + item_info["Name"], season["IndexNumber"], episode["IndexNumber"], ) ) if imageurl: - ret = self.meidiaserverclient.set_item_image( + ret = self.meidia_server_client.set_item_image( itemid=episodeinfo["Id"], imageurl=imageurl ) if ret: log().logger.info( "原始媒体名称[{}] 第[{}]季 第[{}]集更新图片".format( - iteminfo["Name"], + item_info["Name"], season["IndexNumber"], episode["IndexNumber"], ) ) - if not updatename and not updatepeople and not updateoverview: + if ( + not update_name + and not update_people + and not update_overview + and not update_score + ): return True, item["Name"] - ret = self.meidiaserverclient.set_item_info( - itemid=iteminfo["Id"], iteminfo=iteminfo + ret = self.meidia_server_client.set_item_info( + itemid=item_info["Id"], iteminfo=item_info ) if ret: - if updatename: + if update_name: log().logger.info( "原始媒体名称[{}]更新为[{}]".format( - originalname, iteminfo["Name"] + original_name, item_info["Name"] + ) + ) + if update_score: + log().logger.info( + "原始媒体名称[{}]更新豆瓣评分[{}]".format( + item_info["Name"], item_info["CommunityRating"] ) ) - if updatepeople: - if "Jellyfin" in self.mediaservertype: - _ = self.__refresh_people__(item=item, iteminfo=iteminfo) + if update_people: + if "Jellyfin" in self.media_server_type: + _ = self.__refresh_people__(item=item, iteminfo=item_info) log().logger.info( - "原始媒体名称[{}]更新人物".format(iteminfo["Name"]) + "原始媒体名称[{}]更新人物".format(item_info["Name"]) ) - if updateoverview: + if update_overview: log().logger.info( - "原始媒体名称[{}]更新概述".format(iteminfo["Name"]) + "原始媒体名称[{}]更新概述".format(item_info["Name"]) ) - time.sleep(self.taskdonespace) + time.sleep(self.task_done_space) return True, item["Name"] except Exception as result: @@ -801,14 +864,16 @@ def __refresh_people__(self, item, iteminfo): :return True of False """ try: - ret, newiteminfo = self.meidiaserverclient.get_item_info(itemid=item["Id"]) + ret, newiteminfo = self.meidia_server_client.get_item_info( + itemid=item["Id"] + ) if not ret: log().logger.info( "获取[{}]媒体[{}]ID[{}]信息失败, {}".format( - self.mediaservertype, + self.media_server_type, item["Name"], item["Id"], - self.meidiaserverclient.err, + self.meidia_server_client.err, ) ) return False @@ -818,44 +883,44 @@ def __refresh_people__(self, item, iteminfo): or people["Name"] not in newpeople["Name"] ): continue - ret, peopleinfo = self.meidiaserverclient.get_item_info( + ret, peopleinfo = self.meidia_server_client.get_item_info( itemid=people["Id"] ) if not ret: log().logger.info( "获取[{}]人物信息失败, {}".format( - self.mediaservertype, self.meidiaserverclient.err + self.media_server_type, self.meidia_server_client.err ) ) continue - ret, newpeopleinfo = self.meidiaserverclient.get_item_info( + ret, newpeopleinfo = self.meidia_server_client.get_item_info( itemid=newpeople["Id"] ) if not ret: log().logger.info( "获取[{}]人物信息失败, {}".format( - self.mediaservertype, self.meidiaserverclient.err + self.media_server_type, self.meidia_server_client.err ) ) continue newpeopleinfo["ProviderIds"] = peopleinfo["ProviderIds"] - ret = self.meidiaserverclient.set_item_info( + ret = self.meidia_server_client.set_item_info( itemid=newpeopleinfo["Id"], iteminfo=newpeopleinfo ) if not ret: log().logger.info( "更新[{}]人物信息失败, {}".format( - self.mediaservertype, self.meidiaserverclient.err + self.media_server_type, self.meidia_server_client.err ) ) continue - ret, newpeopleinfo = self.meidiaserverclient.get_item_info( + ret, newpeopleinfo = self.meidia_server_client.get_item_info( itemid=newpeople["Id"] ) if not ret: log().logger.info( "获取[{}]人物信息失败, {}".format( - self.mediaservertype, self.meidiaserverclient.err + self.media_server_type, self.meidia_server_client.err ) ) continue @@ -864,7 +929,7 @@ def __refresh_people__(self, item, iteminfo): if not ret: log().logger.info('刷新[{}]人物信息失败, {}'.format(self.mediaservertype, self.meidiaserverclient.err)) """ - time.sleep(self.taskdonespace) + time.sleep(self.task_done_space) return True except Exception as result: @@ -885,13 +950,13 @@ def __update_people__(self, item, iteminfo, imdbid): doubancelebritiesinfo = None needdelpeople = [] for people in iteminfo["People"]: - ret, peopleinfo = self.meidiaserverclient.get_item_info( + ret, peopleinfo = self.meidia_server_client.get_item_info( itemid=people["Id"] ) if not ret: log().logger.info( "获取[{}]人物信息失败, {}".format( - self.mediaservertype, self.meidiaserverclient.err + self.media_server_type, self.meidia_server_client.err ) ) continue @@ -916,7 +981,7 @@ def __update_people__(self, item, iteminfo, imdbid): if not peopletmdbid and not peopleimdbid: log().logger.info( "[{}]人物[{}]ID[{}]Tmdb|Imdb不存在".format( - self.mediaservertype, + self.media_server_type, peopleinfo["Name"], peopleinfo["Id"], ) @@ -982,8 +1047,8 @@ def __update_people__(self, item, iteminfo, imdbid): if "Name" not in peopleinfo["LockedFields"]: peopleinfo["LockedFields"].append("Name") people["Name"] = peoplename - if "Emby" in self.mediaservertype: - ret = self.meidiaserverclient.set_item_info( + if "Emby" in self.media_server_type: + ret = self.meidia_server_client.set_item_info( itemid=peopleinfo["Id"], iteminfo=peopleinfo ) else: @@ -1052,14 +1117,16 @@ def __update_people__(self, item, iteminfo, imdbid): doubanname = re.sub( pattern="\\s+", repl="", string=celebrities["name"] ) + if doubanname.find("演员") != -1: + continue if people["Name"] != doubanname and self.__is_chinese__( string=doubanname ): originalpeoplename = people["Name"] peopleinfo["Name"] = doubanname people["Name"] = doubanname - if "Emby" in self.mediaservertype: - ret = self.meidiaserverclient.set_item_info( + if "Emby" in self.media_server_type: + ret = self.meidia_server_client.set_item_info( itemid=peopleinfo["Id"], iteminfo=peopleinfo ) else: @@ -1072,11 +1139,11 @@ def __update_people__(self, item, iteminfo, imdbid): ) updatepeople = True - if "Emby" in self.mediaservertype: + if "Emby" in self.media_server_type: peoplelist = [] peoples = [] for people in iteminfo["People"]: - if self.delnotimagepeople: + if self.del_not_image_people: if "PrimaryImageTag" not in people: updatepeople = True continue @@ -1167,61 +1234,61 @@ def __get_douban_media_celebrities_info__(self, mediatype: int, name: str, id: s :return True or False, celebritiesinfo """ try: - ret, celebritiesinfo = self.sqlclient.get_douban_celebrities_info( + ret, celebritiesinfo = self.sql_client.get_douban_celebrities_info( mediatype=mediatype, id=id ) if not ret: if mediatype == 1: - ret, celebritiesinfo = self.doubanclient.get_tv_celebrities_info( + ret, celebritiesinfo = self.douban_client.get_tv_celebrities_info( tvid=id ) else: - ret, celebritiesinfo = self.doubanclient.get_movie_celebrities_info( - movieid=id + ret, celebritiesinfo = ( + self.douban_client.get_movie_celebrities_info(movieid=id) ) if not ret: log().logger.info( "获取豆瓣媒体[{}]ID[{}]演员信息失败, {}".format( - name, id, self.doubanclient.err + name, id, self.douban_client.err ) ) return False, None - ret = self.sqlclient.write_douban_celebrities_info( + ret = self.sql_client.write_douban_celebrities_info( mediatype=mediatype, id=id, iteminfo=celebritiesinfo ) if not ret: log().logger.info( "保存豆瓣媒体[{}]ID[{}]演员信息失败, {}".format( - name, id, self.doubanclient.err + name, id, self.douban_client.err ) ) for celebrities in celebritiesinfo["directors"]: - ret, info = self.sqlclient.get_douban_people_info(id=celebrities["id"]) + ret, info = self.sql_client.get_douban_people_info(id=celebrities["id"]) if not ret: - ret, info = self.doubanclient.get_celebrity_info( + ret, info = self.douban_client.get_celebrity_info( celebrityid=celebrities["id"] ) if not ret: log().logger.info( "获取豆瓣媒体[{}]ID[{}]演员信息失败, {}".format( - name, id, self.doubanclient.err + name, id, self.douban_client.err ) ) continue - ret = self.sqlclient.write_douban_people_info( + ret = self.sql_client.write_douban_people_info( id=celebrities["id"], iteminfo=info ) if not ret: log().logger.info( "获取豆瓣媒体[{}]ID[{}]演员信息失败, {}".format( - name, id, self.doubanclient.err + name, id, self.douban_client.err ) ) celebrities["info"] = info for celebrities in celebritiesinfo["actors"]: - ret, info = self.sqlclient.get_douban_people_info(id=celebrities["id"]) + ret, info = self.sql_client.get_douban_people_info(id=celebrities["id"]) if not ret: - ret, info = self.doubanclient.get_celebrity_info( + ret, info = self.douban_client.get_celebrity_info( celebrityid=celebrities["id"] ) if not ret: @@ -1231,11 +1298,11 @@ def __get_douban_media_celebrities_info__(self, mediatype: int, name: str, id: s id, celebrities["name"], celebrities["id"], - self.doubanclient.err, + self.douban_client.err, ) ) continue - ret = self.sqlclient.write_douban_people_info( + ret = self.sql_client.write_douban_people_info( id=celebrities["id"], iteminfo=info ) if not ret: @@ -1245,7 +1312,7 @@ def __get_douban_media_celebrities_info__(self, mediatype: int, name: str, id: s id, celebrities["name"], celebrities["id"], - self.doubanclient.err, + self.douban_client.err, ) ) celebrities["info"] = info @@ -1267,19 +1334,19 @@ def __get_douban_media_info__(self, mediatype: int, name: str, id: str): :return True or False, mediainfo """ try: - ret, items = self.sqlclient.search_douban_media( + ret, items = self.sql_client.search_douban_media( mediatype=mediatype, title=name ) if not ret: - ret, items = self.doubanclient.search_media_pc(name) - if not ret: - ret, items = self.doubanclient.search_media(name) - if not ret: - ret, items = self.doubanclient.search_media_weixin(name) - if not ret: + ret, items = self.douban_client.search_media_pc(name) + if not ret or not items["items"]: + ret, items = self.douban_client.search_media(name) + if not ret or not items["items"]: + ret, items = self.douban_client.search_media_weixin(name) + if not ret or not items["items"]: log().logger.info( "豆瓣搜索媒体[{}]失败, {}".format( - name, str(self.doubanclient.err) + name, str(self.douban_client.err) ) ) return False, None @@ -1291,44 +1358,44 @@ def __get_douban_media_info__(self, mediatype: int, name: str, id: str): elif mediatype == 2: if "target_type" in item and item["target_type"] != "movie": continue - ret, mediainfo = self.sqlclient.get_douban_media_info( + ret, mediainfo = self.sql_client.get_douban_media_info( mediatype=mediatype, id=item["target_id"] ) if not ret: if mediatype == 2: - ret, mediainfo = self.doubanclient.get_movie_info( + ret, mediainfo = self.douban_client.get_movie_info( movieid=item["target_id"] ) else: - ret, mediainfo = self.doubanclient.get_tv_info( + ret, mediainfo = self.douban_client.get_tv_info( tvid=item["target_id"] ) if not ret: log().logger.info( "获取豆瓣媒体[{}]ID[{}]信息失败, {}".format( - item["title"], item["target_id"], self.doubanclient.err + item["title"], item["target_id"], self.douban_client.err ) ) return False, None - ret = self.sqlclient.write_douban_media_info( + ret = self.sql_client.write_douban_media_info( mediatype=mediatype, id=item["target_id"], iteminfo=mediainfo ) if not ret: log().logger.info( "保存豆瓣媒体[{}]ID[{}]信息失败, {}".format( - item["title"], item["target_id"], self.doubanclient.err + item["title"], item["target_id"], self.douban_client.err ) ) if "IMDb" not in mediainfo["info"]: continue if mediainfo["info"]["IMDb"] == id: - ret = self.sqlclient.write_douban_media( + ret = self.sql_client.write_douban_media( mediatype=mediatype, id=item["target_id"], iteminfo=item ) if not ret: log().logger.info( "保存豆瓣媒体[{}]ID[{}]信息失败, {}".format( - item["title"], item["target_id"], self.doubanclient.err + item["title"], item["target_id"], self.douban_client.err ) ) return True, mediainfo @@ -1354,52 +1421,52 @@ def __get_tmdb_media_info__( try: iteminfo = None if mediatype == 1: - ret, iteminfo = self.sqlclient.get_tmdb_media_info( + ret, iteminfo = self.sql_client.get_tmdb_media_info( mediatype=mediatype, id=id, language=language ) if not ret: - ret, iteminfo = self.tmdbclient.get_tv_info( + ret, iteminfo = self.tmdb_client.get_tv_info( tvid=id, language=language ) if not ret: log().logger.info( "获取TMDB媒体[{}]ID[{}]信息失败, {}".format( - name, id, self.tmdbclient.err + name, id, self.tmdb_client.err ) ) return False, None - ret = self.sqlclient.write_tmdb_media_info( + ret = self.sql_client.write_tmdb_media_info( mediatype=mediatype, id=id, language=language, iteminfo=iteminfo ) if not ret: log().logger.info( "保存TMDB媒体[{}]ID[{}]信息失败, {}".format( - name, id, self.tmdbclient.err + name, id, self.tmdb_client.err ) ) return True, iteminfo else: - ret, iteminfo = self.sqlclient.get_tmdb_media_info( + ret, iteminfo = self.sql_client.get_tmdb_media_info( mediatype=mediatype, id=id, language=language ) if not ret: - ret, iteminfo = self.tmdbclient.get_movie_info( + ret, iteminfo = self.tmdb_client.get_movie_info( movieid=id, language=language ) if not ret: log().logger.info( "获取TMDB媒体[{}]ID[{}]信息失败, {}".format( - name, id, self.tmdbclient.err + name, id, self.tmdb_client.err ) ) return False, None - ret = self.sqlclient.write_tmdb_media_info( + ret = self.sql_client.write_tmdb_media_info( mediatype=mediatype, id=id, language=language, iteminfo=iteminfo ) if not ret: log().logger.info( "保存TMDB媒体[{}]ID[{}]信息失败, {}".format( - name, id, self.tmdbclient.err + name, id, self.tmdb_client.err ) ) return True, iteminfo @@ -1424,7 +1491,7 @@ def __get_tmdb_media_name__( :return True or False, name """ try: - for language in self.languagelist: + for language in self.language_list: if mediatype == 1: ret, tvinfo = self.__get_tmdb_media_info__( mediatype=mediatype, name=name, id=id, language=language @@ -1492,14 +1559,14 @@ def __get_tmdb_tv_season_group_info__(self, name, groupid): :return True or False, iteminfo """ try: - for language in self.languagelist: - ret, iteminfo = self.tmdbclient.get_tv_season_group( + for language in self.language_list: + ret, iteminfo = self.tmdb_client.get_tv_season_group( groupid=groupid, language=language ) if not ret: log().logger.info( "获取TMDB剧集[{}]组ID[{}]信息失败, {}".format( - name, groupid, self.tmdbclient.err + name, groupid, self.tmdb_client.err ) ) continue @@ -1525,22 +1592,22 @@ def __get_tmdb_tv_season_info__( :return True or False, name, overview, ommunityrating, imageurl """ try: - for language in self.languagelist: - ret, seasoninfo = self.sqlclient.get_tmdb_season_info( + for language in self.language_list: + ret, seasoninfo = self.sql_client.get_tmdb_season_info( id=tvid, seasonid=seasonid, language=language ) if not ret: - ret, seasoninfo = self.tmdbclient.get_tv_season_info( + ret, seasoninfo = self.tmdb_client.get_tv_season_info( tvid=tvid, seasonid=seasonid, language=language ) if not ret: log().logger.info( "获取TMDB媒体[{}]ID[{}]季ID[{}]信息失败, {}".format( - name, tvid, seasonid, self.tmdbclient.err + name, tvid, seasonid, self.tmdb_client.err ) ) continue - ret = self.sqlclient.write_tmdb_season_info( + ret = self.sql_client.write_tmdb_season_info( id=tvid, seasonid=seasonid, language=language, @@ -1549,7 +1616,7 @@ def __get_tmdb_tv_season_info__( if not ret: log().logger.info( "保存TMDB媒体[{}]ID[{}]季ID[{}]信息失败, {}".format( - name, tvid, seasonid, self.tmdbclient.err + name, tvid, seasonid, self.tmdb_client.err ) ) for episodes in seasoninfo["episodes"]: @@ -1596,28 +1663,28 @@ def __get_tmdb_person_name(self, name, personid): :return True or False, name """ try: - for language in self.languagelist: - ret, personinfo = self.sqlclient.get_tmdb_people_info( + for language in self.language_list: + ret, personinfo = self.sql_client.get_tmdb_people_info( id=personid, language=language ) if not ret: - ret, personinfo = self.tmdbclient.get_person_info( + ret, personinfo = self.tmdb_client.get_person_info( personid=personid, language=language ) if not ret: log().logger.info( "获取TMDB人物[{}]ID[{}]信息失败, {}".format( - name, personid, self.tmdbclient.err + name, personid, self.tmdb_client.err ) ) continue - ret = self.sqlclient.write_tmdb_people_info( + ret = self.sql_client.write_tmdb_people_info( id=personid, language=language, iteminfo=personinfo ) if not ret: log().logger.info( "保存TMDB人物[{}]ID[{}]信息失败, {}".format( - name, personid, self.tmdbclient.err + name, personid, self.tmdb_client.err ) ) for name in personinfo["also_known_as"]: diff --git a/system/config.py b/api/media_config.py similarity index 80% rename from system/config.py rename to api/media_config.py index eb3c490..d37fade 100644 --- a/system/config.py +++ b/api/media_config.py @@ -3,32 +3,27 @@ import os -class config: - path = None - configdata = None - defaultconfig = None - apidata = None - systemdata = None - - def __init__(self, path: str) -> None: +class MediaConfig: + def __init__(self) -> None: try: # 打开文件 self.defaultconfig = yaml.load( open("config.default.yaml", "r", encoding="utf-8"), Loader=yaml.FullLoader, ) - self.path = path self.configdata = yaml.load( - open(self.path, "r", encoding="utf-8"), Loader=yaml.FullLoader + open( + os.path.join(os.getcwd(), "config", "config.yaml"), + "r", + encoding="utf-8", + ), + Loader=yaml.FullLoader, ) # 递归检查配置文件是否包含默认配置文件的所有配置项 for key in self.defaultconfig: self.__config_check__(self.configdata, key, self.defaultconfig[key]) - self.systemdata = self.configdata["system"] - self.apidata = self.configdata["api"] - except Exception as result: log().logger.info("配置异常错误, {}".format(result)) diff --git a/api/mediasql.py b/api/mediasql.py index 991b398..e4ceb86 100644 --- a/api/mediasql.py +++ b/api/mediasql.py @@ -1,8 +1,7 @@ import datetime import json - from api.sql import sql -from system.config import config +from system.config import Config class mediasql(sql): @@ -10,43 +9,56 @@ class mediasql(sql): tmdbpeoplecachefailtime = None doubanmediacachefailtime = None doubanpeoplecachefailtime = None - - def __init__(self, configinfo : config) -> None: + + def __init__(self) -> None: super().__init__() - self.tmdbmediacachefailtime = configinfo.apidata['tmdb']['mediacachefailtime'] - self.tmdbpeoplecachefailtime = configinfo.apidata['tmdb']['peoplecachefailtime'] - self.doubanmediacachefailtime = configinfo.apidata['douban']['mediacachefailtime'] - self.doubanpeoplecachefailtime = configinfo.apidata['douban']['peoplecachefailtime'] + config = Config().get_config() + self.tmdbmediacachefailtime = config["api"]["tmdb"]["mediacachefailtime"] + self.tmdbpeoplecachefailtime = config["api"]["tmdb"]["peoplecachefailtime"] + self.doubanmediacachefailtime = config["api"]["douban"]["mediacachefailtime"] + self.doubanpeoplecachefailtime = config["api"]["douban"]["peoplecachefailtime"] - def search_douban_media(self, mediatype : int, title : str): + def search_douban_media(self, mediatype: int, title: str): """ 搜索豆瓣媒体 :param mediatype 媒体类型 1TV 2电影 :param titel 媒体标题 :return True or False, iteminfo """ - iteminfo = {'items':[]} + iteminfo = {"items": []} try: if mediatype == 1: - ret, datalist = self.query(sql="select * from douban_tv where media_name like '%{}%';".format(title)) + ret, datalist = self.query( + sql="select * from douban_tv where media_name like '%{}%';".format( + title + ) + ) else: - ret, datalist = self.query(sql="select * from douban_movie where media_name like '%{}%';".format(title)) + ret, datalist = self.query( + sql="select * from douban_movie where media_name like '%{}%';".format( + title + ) + ) if not ret or not len(datalist): return False, None for data in datalist: - datatime = datetime.datetime.strptime(data[-1], '%Y-%m-%d %H:%M:%S') + datatime = datetime.datetime.strptime(data[-1], "%Y-%m-%d %H:%M:%S") nowtime = datetime.datetime.now() day = (nowtime - datatime).days if day > self.doubanmediacachefailtime: continue - iteminfo['items'].append(json.loads(data[3])) + iteminfo["items"].append(json.loads(data[3])) return True, iteminfo return False, None except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False, iteminfo - def write_douban_media(self, mediatype : int, id : str, iteminfo): + def write_douban_media(self, mediatype: int, id: str, iteminfo): """ 写入豆瓣媒体 :param mediatype 媒体类型 1TV 2电影 @@ -56,28 +68,66 @@ def write_douban_media(self, mediatype : int, id : str, iteminfo): """ try: if mediatype == 1: - ret, datalist = self.query(sql="select * from douban_tv where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from douban_tv where media_id = '{}';".format(id) + ) else: - ret, datalist = self.query(sql="select * from douban_movie where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from douban_movie where media_id = '{}';".format(id) + ) if not ret: return False for data in datalist: if mediatype == 1: - ret = self.execution(sql="update douban_tv set media_brief = ?, update_time = ? where media_id = ?;", data=(json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data[1])) + ret = self.execution( + sql="update douban_tv set media_brief = ?, update_time = ? where media_id = ?;", + data=( + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + data[1], + ), + ) else: - ret = self.execution(sql="update douban_movie set media_brief = ?, update_time = ? where media_id = ?;", data=(json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data[1])) + ret = self.execution( + sql="update douban_movie set media_brief = ?, update_time = ? where media_id = ?;", + data=( + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + data[1], + ), + ) return ret - + if mediatype == 1: - ret = self.execution(sql="insert into douban_tv(id, media_id, media_name, media_brief, media_data, media_celebrities, update_time) values(null, ?, ?, ?, '', '', ?);", data=(id, iteminfo['title'], json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) + ret = self.execution( + sql="insert into douban_tv(id, media_id, media_name, media_brief, media_data, media_celebrities, update_time) values(null, ?, ?, ?, '', '', ?);", + data=( + id, + iteminfo["title"], + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + ), + ) else: - ret = self.execution(sql="insert into douban_movie(id, media_id, media_name, media_brief, media_data, media_celebrities, update_time) values(null, ?, ?, ?, '', '', ?);", data=(id, iteminfo['title'], json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) + ret = self.execution( + sql="insert into douban_movie(id, media_id, media_name, media_brief, media_data, media_celebrities, update_time) values(null, ?, ?, ?, '', '', ?);", + data=( + id, + iteminfo["title"], + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + ), + ) return ret except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False - def get_douban_media_info(self, mediatype : int, id : str): + def get_douban_media_info(self, mediatype: int, id: str): """ 获取豆瓣媒体信息 :param mediatype 媒体类型 1TV 2电影 @@ -87,13 +137,17 @@ def get_douban_media_info(self, mediatype : int, id : str): iteminfo = {} try: if mediatype == 1: - ret, datalist = self.query(sql="select * from douban_tv where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from douban_tv where media_id = '{}';".format(id) + ) else: - ret, datalist = self.query(sql="select * from douban_movie where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from douban_movie where media_id = '{}';".format(id) + ) if not ret or not len(datalist): return False, None for data in datalist: - datatime = datetime.datetime.strptime(data[-1], '%Y-%m-%d %H:%M:%S') + datatime = datetime.datetime.strptime(data[-1], "%Y-%m-%d %H:%M:%S") nowtime = datetime.datetime.now() day = (nowtime - datatime).days if day > self.doubanmediacachefailtime: @@ -102,10 +156,14 @@ def get_douban_media_info(self, mediatype : int, id : str): return True, iteminfo return False, None except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False, iteminfo - def write_douban_media_info(self, mediatype : int, id : str, iteminfo): + def write_douban_media_info(self, mediatype: int, id: str, iteminfo): """ 写入豆瓣媒体信息 :param mediatype 媒体类型 1TV 2电影 @@ -115,28 +173,66 @@ def write_douban_media_info(self, mediatype : int, id : str, iteminfo): """ try: if mediatype == 1: - ret, datalist = self.query(sql="select * from douban_tv where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from douban_tv where media_id = '{}';".format(id) + ) else: - ret, datalist = self.query(sql="select * from douban_movie where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from douban_movie where media_id = '{}';".format(id) + ) if not ret: return False for data in datalist: if mediatype == 1: - ret = self.execution(sql="update douban_tv set media_data = ?, update_time = ? where media_id = ?;", data=(json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data[1])) + ret = self.execution( + sql="update douban_tv set media_data = ?, update_time = ? where media_id = ?;", + data=( + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + data[1], + ), + ) else: - ret = self.execution(sql="update douban_movie set media_data = ?, update_time = ? where media_id = ?;", data=(json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data[1])) + ret = self.execution( + sql="update douban_movie set media_data = ?, update_time = ? where media_id = ?;", + data=( + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + data[1], + ), + ) return ret - + if mediatype == 1: - ret = self.execution(sql="insert into douban_tv(id, media_id, media_name, media_brief, media_data, media_celebrities, update_time) values(null, ?, ?, '', ?, '', ?);", data=(id, iteminfo['title'], json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) + ret = self.execution( + sql="insert into douban_tv(id, media_id, media_name, media_brief, media_data, media_celebrities, update_time) values(null, ?, ?, '', ?, '', ?);", + data=( + id, + iteminfo["title"], + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + ), + ) else: - ret = self.execution(sql="insert into douban_movie(id, media_id, media_name, media_brief, media_data, media_celebrities, update_time) values(null, ?, ?, '', ?, '', ?);", data=(id, iteminfo['title'], json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) + ret = self.execution( + sql="insert into douban_movie(id, media_id, media_name, media_brief, media_data, media_celebrities, update_time) values(null, ?, ?, '', ?, '', ?);", + data=( + id, + iteminfo["title"], + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + ), + ) return ret except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False - def get_douban_celebrities_info(self, mediatype : int, id : str): + def get_douban_celebrities_info(self, mediatype: int, id: str): """ 获取豆瓣媒体演员信息 :param mediatype 媒体类型 1TV 2电影 @@ -146,13 +242,17 @@ def get_douban_celebrities_info(self, mediatype : int, id : str): iteminfo = {} try: if mediatype == 1: - ret, datalist = self.query(sql="select * from douban_tv where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from douban_tv where media_id = '{}';".format(id) + ) else: - ret, datalist = self.query(sql="select * from douban_movie where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from douban_movie where media_id = '{}';".format(id) + ) if not ret or not len(datalist): return False, None for data in datalist: - datatime = datetime.datetime.strptime(data[-1], '%Y-%m-%d %H:%M:%S') + datatime = datetime.datetime.strptime(data[-1], "%Y-%m-%d %H:%M:%S") nowtime = datetime.datetime.now() day = (nowtime - datatime).days if day > self.doubanmediacachefailtime: @@ -161,10 +261,14 @@ def get_douban_celebrities_info(self, mediatype : int, id : str): return True, iteminfo return False, None except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False, iteminfo - def write_douban_celebrities_info(self, mediatype : int, id : str, iteminfo): + def write_douban_celebrities_info(self, mediatype: int, id: str, iteminfo): """ 写入豆瓣媒体演员信息 :param mediatype 媒体类型 1TV 2电影 @@ -174,28 +278,64 @@ def write_douban_celebrities_info(self, mediatype : int, id : str, iteminfo): """ try: if mediatype == 1: - ret, datalist = self.query(sql="select * from douban_tv where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from douban_tv where media_id = '{}';".format(id) + ) else: - ret, datalist = self.query(sql="select * from douban_movie where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from douban_movie where media_id = '{}';".format(id) + ) if not ret: return False for data in datalist: if mediatype == 1: - ret = self.execution(sql="update douban_tv set media_celebrities = ?, update_time = ? where media_id = ?;", data=(json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data[1])) + ret = self.execution( + sql="update douban_tv set media_celebrities = ?, update_time = ? where media_id = ?;", + data=( + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + data[1], + ), + ) else: - ret = self.execution(sql="update douban_movie set media_celebrities = ?, update_time = ? where media_id = ?;", data=(json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data[1])) + ret = self.execution( + sql="update douban_movie set media_celebrities = ?, update_time = ? where media_id = ?;", + data=( + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + data[1], + ), + ) return ret - + if mediatype == 1: - ret = self.execution(sql="insert into douban_tv(id, media_id, media_name, media_brief, media_data, media_celebrities, update_time) values(null, ?, '', '', '', ?, ?);", data=(id, json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) + ret = self.execution( + sql="insert into douban_tv(id, media_id, media_name, media_brief, media_data, media_celebrities, update_time) values(null, ?, '', '', '', ?, ?);", + data=( + id, + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + ), + ) else: - ret = self.execution(sql="insert into douban_movie(id, media_id, media_name, media_brief, media_data, media_celebrities, update_time) values(null, ?, '', '', '', ?, ?);", data=(id, json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) + ret = self.execution( + sql="insert into douban_movie(id, media_id, media_name, media_brief, media_data, media_celebrities, update_time) values(null, ?, '', '', '', ?, ?);", + data=( + id, + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + ), + ) return ret except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False - def get_douban_people_info(self, id : str): + def get_douban_people_info(self, id: str): """ 获取豆瓣演员信息 :param mediatype 媒体类型 1TV 2电影 @@ -204,11 +344,13 @@ def get_douban_people_info(self, id : str): """ iteminfo = {} try: - ret, datalist = self.query(sql="select * from douban_people where people_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from douban_people where people_id = '{}';".format(id) + ) if not ret or not len(datalist): return False, None for data in datalist: - datatime = datetime.datetime.strptime(data[-1], '%Y-%m-%d %H:%M:%S') + datatime = datetime.datetime.strptime(data[-1], "%Y-%m-%d %H:%M:%S") nowtime = datetime.datetime.now() day = (nowtime - datatime).days if day > self.doubanpeoplecachefailtime: @@ -217,10 +359,14 @@ def get_douban_people_info(self, id : str): return True, iteminfo return False, None except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False, iteminfo - def write_douban_people_info(self, id : str, iteminfo): + def write_douban_people_info(self, id: str, iteminfo): """ 写入豆瓣演员信息 :param id 人物ID @@ -229,20 +375,41 @@ def write_douban_people_info(self, id : str, iteminfo): :return True or False """ try: - ret, datalist = self.query(sql="select * from douban_people where people_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from douban_people where people_id = '{}';".format(id) + ) if not ret: return False for data in datalist: - ret = self.execution(sql="update douban_people set people_name = ?, people_data = ?, update_time = ? where people_id = ?;", data=(iteminfo['title'], json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data[2])) + ret = self.execution( + sql="update douban_people set people_name = ?, people_data = ?, update_time = ? where people_id = ?;", + data=( + iteminfo["title"], + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + data[2], + ), + ) return ret - ret = self.execution(sql="insert into douban_people(id, people_id, people_name, people_data, update_time) values(null, ?, ?, ?, ?)", data=(id, iteminfo['title'], json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) + ret = self.execution( + sql="insert into douban_people(id, people_id, people_name, people_data, update_time) values(null, ?, ?, ?, ?)", + data=( + id, + iteminfo["title"], + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + ), + ) return ret except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False - - def get_tmdb_media_info(self, mediatype : int, id : str, language): + def get_tmdb_media_info(self, mediatype: int, id: str, language): """ 读取TMDB媒体信息 :param mediatype 媒体类型 1TV 2电影 @@ -252,35 +419,43 @@ def get_tmdb_media_info(self, mediatype : int, id : str, language): """ try: if mediatype == 1: - ret, datalist = self.query(sql="select * from tmdb_tv where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from tmdb_tv where media_id = '{}';".format(id) + ) else: - ret, datalist = self.query(sql="select * from tmdb_movie where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from tmdb_movie where media_id = '{}';".format(id) + ) if not ret or not len(datalist): return False, None for data in datalist: item = None - if language == 'zh-CN': + if language == "zh-CN": item = data[3] - elif language == 'zh-SG': + elif language == "zh-SG": item = data[4] - elif language == 'zh-TW': + elif language == "zh-TW": item = data[5] - elif language == 'zh-HK': - item = data[6] + elif language == "zh-HK": + item = data[6] if not item: return False, None - datatime = datetime.datetime.strptime(data[-1], '%Y-%m-%d %H:%M:%S') + datatime = datetime.datetime.strptime(data[-1], "%Y-%m-%d %H:%M:%S") nowtime = datetime.datetime.now() day = (nowtime - datatime).days if day > self.tmdbmediacachefailtime: continue return True, json.loads(item) - return False, None + return False, None except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False, None - def write_tmdb_media_info(self, mediatype : int, id : str, language, iteminfo): + def write_tmdb_media_info(self, mediatype: int, id: str, language, iteminfo): """ 写入TMDB媒体信息 :param mediatype 媒体类型 1TV 2电影 @@ -291,39 +466,87 @@ def write_tmdb_media_info(self, mediatype : int, id : str, language, iteminfo): """ try: if mediatype == 1: - ret, datalist = self.query(sql="select * from tmdb_tv where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from tmdb_tv where media_id = '{}';".format(id) + ) else: - ret, datalist = self.query(sql="select * from tmdb_movie where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from tmdb_movie where media_id = '{}';".format(id) + ) if not ret: return False key = None - if language == 'zh-CN': - key = 'media_data_zh_cn' - elif language == 'zh-SG': - key = 'media_data_zh_sg' - elif language == 'zh-TW': - key = 'media_data_zh_tw' - elif language == 'zh-HK': - key = 'media_data_zh_hk' + if language == "zh-CN": + key = "media_data_zh_cn" + elif language == "zh-SG": + key = "media_data_zh_sg" + elif language == "zh-TW": + key = "media_data_zh_tw" + elif language == "zh-HK": + key = "media_data_zh_hk" if not key: - self.err = '当前语言[{}]不支持'.format(language) + self.err = "当前语言[{}]不支持".format(language) return False for data in datalist: if mediatype == 1: - ret = self.execution(sql="update tmdb_tv set {} = ?, media_name = ?, update_time = ? where media_id = ?;".format(key), data=(json.dumps(iteminfo), iteminfo['name'], datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data[1])) + ret = self.execution( + sql="update tmdb_tv set {} = ?, media_name = ?, update_time = ? where media_id = ?;".format( + key + ), + data=( + json.dumps(iteminfo), + iteminfo["name"], + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + data[1], + ), + ) else: - ret = self.execution(sql="update tmdb_movie set {} = ?, media_name = ?, update_time = ? where media_id = ?;".format(key), data=(json.dumps(iteminfo), iteminfo['title'], datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data[1])) + ret = self.execution( + sql="update tmdb_movie set {} = ?, media_name = ?, update_time = ? where media_id = ?;".format( + key + ), + data=( + json.dumps(iteminfo), + iteminfo["title"], + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + data[1], + ), + ) return ret if mediatype == 1: - ret = self.execution(sql="insert into tmdb_tv(id, media_id, media_name, {}, update_time) values(null, ?, ?, ?, ?);".format(key), data=(id, iteminfo['name'], json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) + ret = self.execution( + sql="insert into tmdb_tv(id, media_id, media_name, {}, update_time) values(null, ?, ?, ?, ?);".format( + key + ), + data=( + id, + iteminfo["name"], + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + ), + ) else: - ret = self.execution(sql="insert into tmdb_movie(id, media_id, media_name, {}, update_time) values(null, ?, ?, ?, ?);".format(key), data=(id, iteminfo['title'], json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) + ret = self.execution( + sql="insert into tmdb_movie(id, media_id, media_name, {}, update_time) values(null, ?, ?, ?, ?);".format( + key + ), + data=( + id, + iteminfo["title"], + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + ), + ) return ret except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False - def get_tmdb_season_info(self, id : str, seasonid, language): + def get_tmdb_season_info(self, id: str, seasonid, language): """ 读取TMDB季信息 :param id 媒体ID @@ -331,41 +554,47 @@ def get_tmdb_season_info(self, id : str, seasonid, language): :returen True or False, iteminfo """ try: - ret, datalist = self.query(sql="select * from tmdb_tv where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from tmdb_tv where media_id = '{}';".format(id) + ) if not ret or not len(datalist): return False, None for data in datalist: item = None - if language == 'zh-CN': + if language == "zh-CN": item = data[7] - elif language == 'zh-SG': + elif language == "zh-SG": item = data[8] - elif language == 'zh-TW': + elif language == "zh-TW": item = data[9] - elif language == 'zh-HK': - item = data[10] + elif language == "zh-HK": + item = data[10] if not item: return False, None - datatime = datetime.datetime.strptime(data[-1], '%Y-%m-%d %H:%M:%S') + datatime = datetime.datetime.strptime(data[-1], "%Y-%m-%d %H:%M:%S") nowtime = datetime.datetime.now() day = (nowtime - datatime).days if day > self.tmdbmediacachefailtime: continue root_object = json.loads(item) if type(root_object) == dict: - if root_object['season_number'] == int(seasonid): + if root_object["season_number"] == int(seasonid): return True, root_object else: for season in root_object: - if season['season_number'] == int(seasonid): + if season["season_number"] == int(seasonid): return True, season - - return False, None + + return False, None except Exception as result: - self.err = "文件[{}][{}]异常错误:".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}][{}]异常错误:".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False, None - def write_tmdb_season_info(self, id : str, seasonid, language, iteminfo): + def write_tmdb_season_info(self, id: str, seasonid, language, iteminfo): """ 写入TMDB季信息 :param id 媒体ID @@ -374,55 +603,88 @@ def write_tmdb_season_info(self, id : str, seasonid, language, iteminfo): :returen True or False """ try: - ret, datalist = self.query(sql="select * from tmdb_tv where media_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from tmdb_tv where media_id = '{}';".format(id) + ) if not ret: return False key = None - if language == 'zh-CN': - key = 'season_data_zh_cn' - elif language == 'zh-SG': - key = 'season_data_zh_sg' - elif language == 'zh-TW': - key = 'season_data_zh_tw' - elif language == 'zh-HK': - key = 'season_data_zh_hk' + if language == "zh-CN": + key = "season_data_zh_cn" + elif language == "zh-SG": + key = "season_data_zh_sg" + elif language == "zh-TW": + key = "season_data_zh_tw" + elif language == "zh-HK": + key = "season_data_zh_hk" if not key: - self.err = '当前语言[{}]不支持'.format(language) + self.err = "当前语言[{}]不支持".format(language) return False data_array = [] data_array.append(iteminfo) for data in datalist: item = None - if language == 'zh-CN': + if language == "zh-CN": item = data[7] - elif language == 'zh-SG': + elif language == "zh-SG": item = data[8] - elif language == 'zh-TW': + elif language == "zh-TW": item = data[9] - elif language == 'zh-HK': - item = data[10] + elif language == "zh-HK": + item = data[10] if not item: - ret = self.execution(sql="update tmdb_tv set {} = ?, update_time = ? where media_id = ?;".format(key), data=(json.dumps(data_array), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data[1])) + ret = self.execution( + sql="update tmdb_tv set {} = ?, update_time = ? where media_id = ?;".format( + key + ), + data=( + json.dumps(data_array), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + data[1], + ), + ) else: root_object = json.loads(item) if type(root_object) == dict: - if root_object['season_number'] == int(seasonid): + if root_object["season_number"] == int(seasonid): continue data_array.append(root_object) else: for season in root_object: - if season['season_number'] == int(seasonid): + if season["season_number"] == int(seasonid): continue data_array.append(season) - ret = self.execution(sql="update tmdb_tv set {} = ?, update_time = ? where media_id = ?;".format(key), data=(json.dumps(data_array), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data[1])) + ret = self.execution( + sql="update tmdb_tv set {} = ?, update_time = ? where media_id = ?;".format( + key + ), + data=( + json.dumps(data_array), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + data[1], + ), + ) return ret - ret = self.execution(sql="insert into tmdb_tv(id, media_id, {}, update_time) values(null, ?, ?, ?);".format(key), data=(id, json.dumps(data_array), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) + ret = self.execution( + sql="insert into tmdb_tv(id, media_id, {}, update_time) values(null, ?, ?, ?);".format( + key + ), + data=( + id, + json.dumps(data_array), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + ), + ) return ret except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False - def get_tmdb_people_info(self, id : str, language): + def get_tmdb_people_info(self, id: str, language): """ 获取TMDB人物信息 :param id 人物ID @@ -430,33 +692,39 @@ def get_tmdb_people_info(self, id : str, language): :returen True or False, iteminfo """ try: - ret, datalist = self.query(sql="select * from tmdb_people where people_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from tmdb_people where people_id = '{}';".format(id) + ) if not ret or not len(datalist): return False, None for data in datalist: item = None - if language == 'zh-CN': + if language == "zh-CN": item = data[3] - elif language == 'zh-SG': + elif language == "zh-SG": item = data[4] - elif language == 'zh-TW': + elif language == "zh-TW": item = data[5] - elif language == 'zh-HK': - item = data[6] + elif language == "zh-HK": + item = data[6] if not item: return False, None - datatime = datetime.datetime.strptime(data[-1], '%Y-%m-%d %H:%M:%S') + datatime = datetime.datetime.strptime(data[-1], "%Y-%m-%d %H:%M:%S") nowtime = datetime.datetime.now() day = (nowtime - datatime).days if day > self.tmdbpeoplecachefailtime: continue return True, json.loads(item) - return False, None + return False, None except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) return False, None - def write_tmdb_people_info(self, id : str, language, iteminfo): + def write_tmdb_people_info(self, id: str, language, iteminfo): """ 写入TMDB人物信息 :param id 人物ID @@ -465,26 +733,51 @@ def write_tmdb_people_info(self, id : str, language, iteminfo): :returen True or False """ try: - ret, datalist = self.query(sql="select * from tmdb_people where people_id = '{}';".format(id)) + ret, datalist = self.query( + sql="select * from tmdb_people where people_id = '{}';".format(id) + ) if not ret: return False key = None - if language == 'zh-CN': - key = 'people_data_zh_cn' - elif language == 'zh-SG': - key = 'people_data_zh_sg' - elif language == 'zh-TW': - key = 'people_data_zh_tw' - elif language == 'zh-HK': - key = 'people_data_zh_hk' + if language == "zh-CN": + key = "people_data_zh_cn" + elif language == "zh-SG": + key = "people_data_zh_sg" + elif language == "zh-TW": + key = "people_data_zh_tw" + elif language == "zh-HK": + key = "people_data_zh_hk" if not key: - self.err = '当前语言[{}]不支持'.format(language) + self.err = "当前语言[{}]不支持".format(language) return False for data in datalist: - ret = self.execution(sql="update tmdb_people set {} = ?, update_time = ? where people_id = ?;".format(key), data=(json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), data[1])) + ret = self.execution( + sql="update tmdb_people set {} = ?, update_time = ? where people_id = ?;".format( + key + ), + data=( + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + data[1], + ), + ) return ret - ret = self.execution(sql="insert into tmdb_people(id, people_id, people_name, {}, update_time) values(null, ?, ?, ?, ?);".format(key), data=(id, iteminfo['name'], json.dumps(iteminfo), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) + ret = self.execution( + sql="insert into tmdb_people(id, people_id, people_name, {}, update_time) values(null, ?, ?, ?, ?);".format( + key + ), + data=( + id, + iteminfo["name"], + json.dumps(iteminfo), + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + ), + ) return ret except Exception as result: - self.err = "文件[{}]行[{}]异常错误:{}".format(result.__traceback__.tb_frame.f_globals["__file__"], result.__traceback__.tb_lineno, result) - return False \ No newline at end of file + self.err = "文件[{}]行[{}]异常错误:{}".format( + result.__traceback__.tb_frame.f_globals["__file__"], + result.__traceback__.tb_lineno, + result, + ) + return False diff --git a/config.default.yaml b/config.default.yaml index e2403e2..1d89d5a 100644 --- a/config.default.yaml +++ b/config.default.yaml @@ -60,10 +60,20 @@ system: # - "xxx" excludepath: - "合集" + #排除媒体 + #例如 + #excludemedia: + # - "xxx" + excludemedia: + - "进击的巨人" #使用媒体服务器 Emby Jellyfin Plex mediaserver: "Emby" #线程数量 豆瓣API有请求频率限制建议线程数量不要过多 threadnum: 10 + #是否更新豆瓣评分 + updatescore: False + #是否更新标题 + updatetitle: True #是否刷新人名 updatepeople: False #是否更新概述 diff --git a/config/config.default.yaml b/config/config.default.yaml index f08c868..1d89d5a 100644 --- a/config/config.default.yaml +++ b/config/config.default.yaml @@ -1,5 +1,7 @@ api: nastools: + #授权码 + authorization: "" #域名 包含http(s)和端口号后面不带/ #例如http://xxx.xxx.xxx:3000 host: "http://127.0.0.1:3000" @@ -51,10 +53,27 @@ api: peoplecachefailtime: 120 system: + #排除媒体文件夹 + #例如 + #excludepath: + # - "AV" + # - "xxx" + excludepath: + - "合集" + #排除媒体 + #例如 + #excludemedia: + # - "xxx" + excludemedia: + - "进击的巨人" #使用媒体服务器 Emby Jellyfin Plex mediaserver: "Emby" #线程数量 豆瓣API有请求频率限制建议线程数量不要过多 threadnum: 10 + #是否更新豆瓣评分 + updatescore: False + #是否更新标题 + updatetitle: True #是否刷新人名 updatepeople: False #是否更新概述 diff --git a/config/config.yaml b/config/config.yaml index 967e09b..422b2fe 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -60,10 +60,22 @@ system: # - "xxx" excludepath: - "合集" + excludepath: + - "合集" + #排除媒体 + #例如 + #excludemedia: + # - "xxx" + excludemedia: + - "进击的巨人" #使用媒体服务器 Emby Jellyfin Plex mediaserver: "Emby" #线程数量 豆瓣API有请求频率限制建议线程数量不要过多 - threadnum: 10 + threadnum: 1 + #是否更新豆瓣评分 + updatescore: True + #是否更新标题 + updatetitle: True #是否刷新人名 updatepeople: False #是否更新概述 diff --git a/main.py b/main.py index e734633..5c089b1 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,10 @@ import os import time import shutil -from media import media -from system.config import config +from api.media import media from system.log import log +from api.media_config import MediaConfig +from system.config import Config if __name__ == "__main__": try: @@ -20,16 +21,15 @@ "默认配置文件不存在, 拷贝默认配置文件[config.default.yaml]->[/config/config.default.yaml]" ) shutil.copy("config.default.yaml", "config/config.default.yaml") - - path = os.path.join(os.getcwd(), "config", "config.yaml") - configinfo = config(path=path) - mediaclient = media(configinfo=configinfo) + _ = MediaConfig() + config = Config().get_config() + mediaclient = media() while True: try: log().logger.info("开始刷新媒体库元数据") mediaclient.start_scan_media() log().logger.info("刷新媒体库元数据完成") - time.sleep(configinfo.systemdata["updatetime"] * 3600) + time.sleep(config["system"]["updatetime"] * 3600) except Exception as result: log().logger.info(result) except Exception as result: diff --git a/network/__init__.py b/network/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/network/client_data.py b/network/client_data.py deleted file mode 100644 index 8945f78..0000000 --- a/network/client_data.py +++ /dev/null @@ -1,51 +0,0 @@ -import requests -import threading - - -class ClientData(object): - client = None - allotnum = None - allotmaxnum = None - lock = None - - def __init__(self, allotnum: int = 0, allotmaxnum: int = 3, proxy: str = None): - self.lock = threading.Lock() - self.allotnum = allotnum - self.allotmaxnum = allotmaxnum - self.client = requests.Session() - - # 设置代理 - if proxy: - self.client.proxies = {"http": proxy, "https": proxy} - - def get(self, url, **kwargs): - """ - GET - :param url - :return p, err - """ - self.lock.acquire() - p = None - err = None - try: - p = self.client.get(url=url, **kwargs) - except Exception as result: - err = "异常错误:{}".format(result) - self.lock.release() - return p, err - - def post(self, url, **kwargs): - """ - GET - :param url - :return p, err - """ - self.lock.acquire() - p = None - err = None - try: - p = self.client.post(url=url, **kwargs) - except Exception as result: - err = "异常错误:{}".format(result) - self.lock.release() - return p, err diff --git a/network/network.py b/network/network.py deleted file mode 100644 index 4264f48..0000000 --- a/network/network.py +++ /dev/null @@ -1,101 +0,0 @@ -from network.client_data import ClientData -import threading - - -class Network: - maxnumconnect = None - maxnumcache = None - clientlist = None - lock = None - - def __init__( - self, maxnumconnect: int = 1, maxnumcache: int = 5, proxy: str = None - ) -> None: - """ - 构造函数 - :param maxnumconnect 最大连接数 - :param maxnumcache 最大缓存数 - """ - self.lock = threading.Lock() - self.maxnumcache = maxnumcache - self.maxnumconnect = maxnumconnect - self.clientlist = [] - if self.maxnumcache < 0: - self.maxnumcache = 1 - if self.maxnumconnect < 0: - self.maxnumconnect = 1 - for i in range(self.maxnumconnect): - self.clientlist.append( - ClientData(allotmaxnum=self.maxnumcache, proxy=proxy) - ) - - def get(self, url, **kwargs): - """ - GET - :param url - :return p, err - """ - p = None - ret, num, err = self.__getclient__() - if ret: - p, err = self.clientlist[num].get(url=url, **kwargs) - self.__releasecache__(num=num) - return p, err - - def post(self, url, **kwargs): - """ - POST - :param url - :param data - :param headers - :return p, err - """ - p = None - ret, num, err = self.__getclient__() - if ret: - p, err = self.clientlist[num].post(url=url, **kwargs) - self.__releasecache__(num=num) - return p, err - - def __getclient__(self): - """ - 获取客户端 - :return True, num, err 成功返回True, 编号, None 失败返回False, None, 错误 - """ - with self.lock: - # 优先空闲连接 - for i in range(len(self.clientlist)): - if ( - self.clientlist[i].allotnum < self.clientlist[i].allotmaxnum - and self.clientlist[i].allotnum == 0 - ): - self.clientlist[i].allotnum += 1 - return True, i, None - - # 优先缓存数少的 - minallotnum = -1 - num = -1 - - for i in range(len(self.clientlist)): - if self.clientlist[i].allotnum < self.clientlist[i].allotmaxnum: - if minallotnum == -1: - minallotnum = self.clientlist[i].allotnum - num = i - elif minallotnum > self.clientlist[i].allotnum: - minallotnum = self.clientlist[i].allotnum - num = i - - if num > -1: - self.clientlist[num].allotnum += 1 - return True, num, None - - return False, None, "连接缓存已满" - - def __releasecache__(self, num: int): - """ - 释放连接 - :param num 连接编号 - """ - self.lock.acquire() - self.clientlist[num].allotnum -= 1 - self.lock.release() diff --git a/system/log.py b/system/log.py deleted file mode 100644 index 7412c9d..0000000 --- a/system/log.py +++ /dev/null @@ -1,36 +0,0 @@ -import logging -from logging.handlers import RotatingFileHandler -import os -from system.singleton import singleton - - -@singleton -class log(object): - logger = None - - def __init__(self) -> None: - try: - path = os.path.join(os.getcwd(), "log", "log.txt") - self.logger = logging.getLogger(__name__) - self.logger.setLevel(logging.INFO) - handler = RotatingFileHandler( - filename=path, - maxBytes=10 * 1024 * 1024, - backupCount=3, - encoding="utf-8", - ) - handler.setFormatter( - logging.Formatter( - "[%(asctime)s][%(levelname)s][%(filename)s:%(lineno)d]: %(message)s" - ) - ) - self.logger.addHandler(handler) - streamhandler = logging.StreamHandler() - streamhandler.setFormatter( - logging.Formatter( - "[%(asctime)s][%(levelname)s][%(filename)s:%(lineno)d]: %(message)s" - ) - ) - self.logger.addHandler(streamhandler) - except Exception as result: - print("异常错误: %s" % result) diff --git a/system/singleton.py b/system/singleton.py deleted file mode 100644 index 0c04f61..0000000 --- a/system/singleton.py +++ /dev/null @@ -1,22 +0,0 @@ -import threading - -lock = threading.RLock() -_instance = {} - - -# 单例模式 -def singleton(cls): - # 创建字典用来保存类的实例对象 - global _instance - - def _singleton(*args, **kwargs): - # 先判断这个类有没有对象 - if cls not in _instance: - with lock: - if cls not in _instance: - _instance[cls] = cls(*args, **kwargs) - pass - # 将实例对象返回 - return _instance[cls] - - return _singleton \ No newline at end of file