fix BestFilmVersion 短时间内连续触发插件 造成的重复订阅
This commit is contained in:
parent
271ab494a9
commit
9e37068b75
@ -1,6 +1,7 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from threading import RLock
|
||||||
from typing import Optional, Any, List, Dict, Tuple
|
from typing import Optional, Any, List, Dict, Tuple
|
||||||
from xml.dom.minidom import parseString
|
from xml.dom.minidom import parseString
|
||||||
|
|
||||||
@ -22,6 +23,8 @@ from app.schemas import WebhookEventInfo
|
|||||||
from app.schemas.types import MediaType, EventType
|
from app.schemas.types import MediaType, EventType
|
||||||
from app.utils.http import RequestUtils
|
from app.utils.http import RequestUtils
|
||||||
|
|
||||||
|
lock = RLock()
|
||||||
|
|
||||||
|
|
||||||
class BestFilmVersion(_PluginBase):
|
class BestFilmVersion(_PluginBase):
|
||||||
# 插件名称
|
# 插件名称
|
||||||
@ -373,7 +376,11 @@ class BestFilmVersion(_PluginBase):
|
|||||||
"""
|
"""
|
||||||
通过流媒体管理工具收藏,自动洗版
|
通过流媒体管理工具收藏,自动洗版
|
||||||
"""
|
"""
|
||||||
|
# 获取锁
|
||||||
|
_is_lock: bool = lock.acquire(timeout=60)
|
||||||
|
if not _is_lock:
|
||||||
|
return
|
||||||
|
try:
|
||||||
# 读取缓存
|
# 读取缓存
|
||||||
caches = self._cache_path.read_text().split("\n") if self._cache_path.exists() else []
|
caches = self._cache_path.read_text().split("\n") if self._cache_path.exists() else []
|
||||||
# 读取历史记录
|
# 读取历史记录
|
||||||
@ -382,49 +389,9 @@ class BestFilmVersion(_PluginBase):
|
|||||||
all_item = []
|
all_item = []
|
||||||
# 读取收藏
|
# 读取收藏
|
||||||
if settings.MEDIASERVER == 'jellyfin':
|
if settings.MEDIASERVER == 'jellyfin':
|
||||||
# 获取所有user
|
self.jellyfin_get_items(all_item)
|
||||||
users_url = "{HOST}Users?&apikey={APIKEY}"
|
|
||||||
users = self.get_users(Jellyfin().get_data(users_url))
|
|
||||||
if not users:
|
|
||||||
logger.info(f"bestfilmversion/users_url: {users_url}")
|
|
||||||
return
|
|
||||||
for user in users:
|
|
||||||
# 根据加入日期 降序排序
|
|
||||||
url = "{HOST}Users/" + user + "/Items?SortBy=DateCreated%2CSortName" \
|
|
||||||
"&SortOrder=Descending" \
|
|
||||||
"&Filters=IsFavorite" \
|
|
||||||
"&Recursive=true" \
|
|
||||||
"&Fields=PrimaryImageAspectRatio%2CBasicSyncInfo" \
|
|
||||||
"&CollapseBoxSetItems=false" \
|
|
||||||
"&ExcludeLocationTypes=Virtual" \
|
|
||||||
"&EnableTotalRecordCount=false" \
|
|
||||||
"&Limit=20" \
|
|
||||||
"&apikey={APIKEY}"
|
|
||||||
resp = self.get_items(Jellyfin().get_data(url))
|
|
||||||
if not resp:
|
|
||||||
continue
|
|
||||||
all_item.extend(resp)
|
|
||||||
elif settings.MEDIASERVER == 'emby':
|
elif settings.MEDIASERVER == 'emby':
|
||||||
# 获取所有user
|
self.emby_get_items(all_item)
|
||||||
get_users_url = "{HOST}Users?&api_key={APIKEY}"
|
|
||||||
users = self.get_users(Emby().get_data(get_users_url))
|
|
||||||
if not users:
|
|
||||||
return
|
|
||||||
for user in users:
|
|
||||||
# 根据加入日期 降序排序
|
|
||||||
url = "{HOST}emby/Users/" + user + "/Items?SortBy=DateCreated%2CSortName" \
|
|
||||||
"&SortOrder=Descending" \
|
|
||||||
"&Filters=IsFavorite" \
|
|
||||||
"&Recursive=true" \
|
|
||||||
"&Fields=PrimaryImageAspectRatio%2CBasicSyncInfo" \
|
|
||||||
"&CollapseBoxSetItems=false" \
|
|
||||||
"&ExcludeLocationTypes=Virtual" \
|
|
||||||
"&EnableTotalRecordCount=false" \
|
|
||||||
"&Limit=20&api_key={APIKEY}"
|
|
||||||
resp = self.get_items(Emby().get_data(url))
|
|
||||||
if not resp:
|
|
||||||
continue
|
|
||||||
all_item.extend(resp)
|
|
||||||
else:
|
else:
|
||||||
resp = self.plex_get_watchlist()
|
resp = self.plex_get_watchlist()
|
||||||
if not resp:
|
if not resp:
|
||||||
@ -433,6 +400,7 @@ class BestFilmVersion(_PluginBase):
|
|||||||
|
|
||||||
def function(y, x):
|
def function(y, x):
|
||||||
return y if (x['Name'] in [i['Name'] for i in y]) else (lambda z, u: (z.append(u), z))(y, x)[1]
|
return y if (x['Name'] in [i['Name'] for i in y]) else (lambda z, u: (z.append(u), z))(y, x)[1]
|
||||||
|
|
||||||
# all_item 根据电影名去重
|
# all_item 根据电影名去重
|
||||||
result = reduce(function, all_item, [])
|
result = reduce(function, all_item, [])
|
||||||
|
|
||||||
@ -459,6 +427,8 @@ class BestFilmVersion(_PluginBase):
|
|||||||
|
|
||||||
# 获取tmdb_id
|
# 获取tmdb_id
|
||||||
media_info_ids = item_info_resp.get('ExternalUrls')
|
media_info_ids = item_info_resp.get('ExternalUrls')
|
||||||
|
if not media_info_ids:
|
||||||
|
continue
|
||||||
for media_info_id in media_info_ids:
|
for media_info_id in media_info_ids:
|
||||||
if 'TheMovieDb' != media_info_id.get('Name'):
|
if 'TheMovieDb' != media_info_id.get('Name'):
|
||||||
continue
|
continue
|
||||||
@ -495,6 +465,54 @@ class BestFilmVersion(_PluginBase):
|
|||||||
self.save_data('history', history)
|
self.save_data('history', history)
|
||||||
# 保存缓存
|
# 保存缓存
|
||||||
self._cache_path.write_text("\n".join(caches))
|
self._cache_path.write_text("\n".join(caches))
|
||||||
|
finally:
|
||||||
|
lock.release()
|
||||||
|
|
||||||
|
def jellyfin_get_items(self, all_item):
|
||||||
|
# 获取所有user
|
||||||
|
users_url = "{HOST}Users?&apikey={APIKEY}"
|
||||||
|
users = self.get_users(Jellyfin().get_data(users_url))
|
||||||
|
if not users:
|
||||||
|
logger.info(f"bestfilmversion/users_url: {users_url}")
|
||||||
|
return
|
||||||
|
for user in users:
|
||||||
|
# 根据加入日期 降序排序
|
||||||
|
url = "{HOST}Users/" + user + "/Items?SortBy=DateCreated%2CSortName" \
|
||||||
|
"&SortOrder=Descending" \
|
||||||
|
"&Filters=IsFavorite" \
|
||||||
|
"&Recursive=true" \
|
||||||
|
"&Fields=PrimaryImageAspectRatio%2CBasicSyncInfo" \
|
||||||
|
"&CollapseBoxSetItems=false" \
|
||||||
|
"&ExcludeLocationTypes=Virtual" \
|
||||||
|
"&EnableTotalRecordCount=false" \
|
||||||
|
"&Limit=20" \
|
||||||
|
"&apikey={APIKEY}"
|
||||||
|
resp = self.get_items(Jellyfin().get_data(url))
|
||||||
|
if not resp:
|
||||||
|
continue
|
||||||
|
all_item.extend(resp)
|
||||||
|
|
||||||
|
def emby_get_items(self, all_item):
|
||||||
|
# 获取所有user
|
||||||
|
get_users_url = "{HOST}Users?&api_key={APIKEY}"
|
||||||
|
users = self.get_users(Emby().get_data(get_users_url))
|
||||||
|
if not users:
|
||||||
|
return
|
||||||
|
for user in users:
|
||||||
|
# 根据加入日期 降序排序
|
||||||
|
url = "{HOST}emby/Users/" + user + "/Items?SortBy=DateCreated%2CSortName" \
|
||||||
|
"&SortOrder=Descending" \
|
||||||
|
"&Filters=IsFavorite" \
|
||||||
|
"&Recursive=true" \
|
||||||
|
"&Fields=PrimaryImageAspectRatio%2CBasicSyncInfo" \
|
||||||
|
"&CollapseBoxSetItems=false" \
|
||||||
|
"&ExcludeLocationTypes=Virtual" \
|
||||||
|
"&EnableTotalRecordCount=false" \
|
||||||
|
"&Limit=20&api_key={APIKEY}"
|
||||||
|
resp = self.get_items(Emby().get_data(url))
|
||||||
|
if not resp:
|
||||||
|
continue
|
||||||
|
all_item.extend(resp)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_items(resp: Response):
|
def get_items(resp: Response):
|
||||||
@ -594,16 +612,29 @@ class BestFilmVersion(_PluginBase):
|
|||||||
return
|
return
|
||||||
|
|
||||||
data: WebhookEventInfo = event.event_data
|
data: WebhookEventInfo = event.event_data
|
||||||
|
# 排除不是收藏调用
|
||||||
|
if data.channel not in ['jellyfin', 'emby', 'plex']:
|
||||||
|
return
|
||||||
|
if data.channel in ['emby', 'plex'] and data.event != 'item.rate':
|
||||||
|
return
|
||||||
|
if data.channel == 'jellyfin' and data.save_reason != 'UpdateUserRating':
|
||||||
|
return
|
||||||
logger.info(f'BestFilmVersion/webhook_message_action WebhookEventInfo打印:{data}')
|
logger.info(f'BestFilmVersion/webhook_message_action WebhookEventInfo打印:{data}')
|
||||||
|
|
||||||
|
# 获取锁
|
||||||
|
_is_lock: bool = lock.acquire(timeout=60)
|
||||||
|
if not _is_lock:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
|
||||||
mediainfo: Optional[MediaInfo] = None
|
mediainfo: Optional[MediaInfo] = None
|
||||||
if not data.tmdb_id:
|
if not data.tmdb_id:
|
||||||
info = None
|
info = None
|
||||||
if data.channel == 'jellyfin' and data.save_reason == 'UpdateUserRating' and data.item_favorite:
|
if data.channel == 'jellyfin' and data.save_reason == 'UpdateUserRating' and data.item_favorite:
|
||||||
info = Jellyfin().get_iteminfo(itemid=data.item_id)
|
info = Jellyfin().get_iteminfo(itemid=data.item_id)
|
||||||
if data.channel == 'emby' and data.event == 'item.rate':
|
elif data.channel == 'emby' and data.event == 'item.rate':
|
||||||
info = Emby().get_iteminfo(itemid=data.item_id)
|
info = Emby().get_iteminfo(itemid=data.item_id)
|
||||||
if data.channel == 'plex' and data.event == 'item.rate':
|
elif data.channel == 'plex' and data.event == 'item.rate':
|
||||||
info = Plex().get_iteminfo(itemid=data.item_id)
|
info = Plex().get_iteminfo(itemid=data.item_id)
|
||||||
logger.info(f'BestFilmVersion/webhook_message_action item打印:{info}')
|
logger.info(f'BestFilmVersion/webhook_message_action item打印:{info}')
|
||||||
|
|
||||||
@ -614,6 +645,8 @@ class BestFilmVersion(_PluginBase):
|
|||||||
|
|
||||||
# 获取tmdb_id
|
# 获取tmdb_id
|
||||||
media_info_ids = info.get('ExternalUrls')
|
media_info_ids = info.get('ExternalUrls')
|
||||||
|
if not media_info_ids:
|
||||||
|
return
|
||||||
for media_info_id in media_info_ids:
|
for media_info_id in media_info_ids:
|
||||||
|
|
||||||
if 'TheMovieDb' != media_info_id.get('Name'):
|
if 'TheMovieDb' != media_info_id.get('Name'):
|
||||||
@ -630,10 +663,6 @@ class BestFilmVersion(_PluginBase):
|
|||||||
else:
|
else:
|
||||||
if data.channel == 'jellyfin' and (data.save_reason != 'UpdateUserRating' or not data.item_favorite):
|
if data.channel == 'jellyfin' and (data.save_reason != 'UpdateUserRating' or not data.item_favorite):
|
||||||
return
|
return
|
||||||
if data.channel == 'emby' and data.event != 'item.rate':
|
|
||||||
return
|
|
||||||
if data.channel == 'plex' and data.event != 'item.rate':
|
|
||||||
return
|
|
||||||
if data.item_type not in ['Movie', 'MOV', 'movie']:
|
if data.item_type not in ['Movie', 'MOV', 'movie']:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -642,6 +671,8 @@ class BestFilmVersion(_PluginBase):
|
|||||||
logger.warn(f'未识别到媒体信息,标题:{data.item_name},tmdbID:{data.tmdb_id}')
|
logger.warn(f'未识别到媒体信息,标题:{data.item_name},tmdbID:{data.tmdb_id}')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if not mediainfo:
|
||||||
|
return
|
||||||
# 读取缓存
|
# 读取缓存
|
||||||
caches = self._cache_path.read_text().split("\n") if self._cache_path.exists() else []
|
caches = self._cache_path.read_text().split("\n") if self._cache_path.exists() else []
|
||||||
# 检查缓存
|
# 检查缓存
|
||||||
@ -674,3 +705,5 @@ class BestFilmVersion(_PluginBase):
|
|||||||
self.save_data('history', history)
|
self.save_data('history', history)
|
||||||
# 保存缓存
|
# 保存缓存
|
||||||
self._cache_path.write_text("\n".join(caches))
|
self._cache_path.write_text("\n".join(caches))
|
||||||
|
finally:
|
||||||
|
lock.release()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user