Skip to content

Commit

Permalink
fix: 合并冲突
Browse files Browse the repository at this point in the history
  • Loading branch information
linyuan0213 committed Apr 28, 2023
2 parents 3f97c1b + 8bada58 commit 56be5c9
Show file tree
Hide file tree
Showing 21 changed files with 1,040 additions and 327 deletions.
123 changes: 123 additions & 0 deletions app/media/doubanapi/apiv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ class DoubanApi(object):
"music_interests": "/music/%s/interests",
"music_reviews": "/music/%s/reviews",
"music_recommendations": "/music/%s/recommendations",

# doulist
"doulist": "/doulist/",
"doulist_items": "/doulist/%s/items",
}

_user_agents = [
Expand Down Expand Up @@ -239,3 +243,122 @@ def tv_chinese_best_weekly(self, start=0, count=20, ts=datetime.strftime(datetim

def tv_global_best_weekly(self, start=0, count=20, ts=datetime.strftime(datetime.now(), '%Y%m%d')):
return self.__invoke(self._urls["tv_global_best_weekly"], start=start, count=count, _ts=ts)

def doulist_detail(self, subject_id):
"""
豆列详情
:param subject_id: 豆列id
:return:
{
"is_follow": false,
"screenshot_title": "分享海报",
"playable_count": 1226,
"screenshot_url": "douban:\/\/partial.douban.com\/screenshot\/doulist\/13712178\/_content",
"create_time": "2014-10-05 10:41:22",
"owner": {
"kind": "user",
"name": "依然饭特稀",
"url": "https:\/\/www.douban.com\/people\/56698183\/",
"uri": "douban:\/\/douban.com\/user\/56698183",
"avatar": "https://img2.doubanio.com\/icon\/up56698183-12.jpg",
"is_club": false,
"type": "user",
"id": "56698183",
"uid": "yrftx"
},
"screenshot_type": "rexxar",
"id": "13712178",
"category": "movie",
"is_merged_cover": false,
"title": "评价人数超过十万的电影",
"is_subject_selection": false,
"followers_count": 53081,
"is_private": false,
"item_abstracts": [],
"type": "doulist",
"update_time": "2023-04-22 22:19:48",
"list_type": "ugc_doulist",
"tags": [],
"syncing_note": null,
"cover_url": "https://img9.doubanio.com\/view\/elanor_image\/raw\/public\/91314905.jpg",
"header_bg_image": "",
"doulist_type": "",
"done_count": 0,
"desc": "谢谢大家的关注和点赞,不过我更希望大家能在留言板上补充遗漏。\r\n看腻了豆瓣的评分排序,不如试试评价人数排序。评价人数并不代表作品的优劣,但是它起码说明了作品的存在感。这不一定是选电影最好的方法,却一定是选电影风险最小的方法。\r\n欢迎关注我关于读书的两个豆列: \r\n豆瓣评价人数超过一万的外文书籍 \r\nhttp:\/\/www.douban.com\/doulist\/37912871\/ \r\n豆瓣评价人数超过一万的中文书籍\r\nhttp:\/\/www.douban.com\/doulist\/36708212\/",
"items_count": 1453,
"wechat_timeline_share": "url",
"url": "https:\/\/www.douban.com\/doulist\/13712178\/",
"is_sys_private": false,
"uri": "douban:\/\/douban.com\/doulist\/13712178",
"sharing_url": "https:\/\/www.douban.com\/doulist\/13712178\/"
}
"""
return self.__invoke(self._urls["doulist"] + subject_id)

def doulist_items(self, subject_id, start=0, count=20, ts=datetime.strftime(datetime.now(), '%Y%m%d')):
"""
豆列列表
:param subject_id: 豆列id
:param start: 开始
:param count: 数量
:param ts: 时间戳
:return:
{
"count": 3,
"start": 0,
"total": 1453,
"items": [{
"comment": "",
"rating": {
"count": 2834097,
"max": 10,
"star_count": 5.0,
"value": 9.7
},
"subtitle": "1994 \/ 美国 \/ 剧情 犯罪 \/ 弗兰克·德拉邦特 \/ 蒂姆·罗宾斯 摩根·弗里曼",
"title": "肖申克的救赎",
"url": "https:\/\/movie.douban.com\/subject\/1292052\/",
"target_id": "1292052",
"uri": "douban:\/\/douban.com\/movie\/1292052",
"cover_url": "https:\/\/qnmob3.doubanio.com\/view\/photo\/m_ratio_poster\/public\/p480747492.jpg?imageView2\/2\/q\/80\/w\/300\/h\/300\/format\/jpg",
"create_time": "2014-10-05 10:41:51",
"type": "movie",
"id": "19877287"
}, {
"comment": "",
"rating": {
"count": 2255839,
"max": 10,
"star_count": 4.5,
"value": 9.4
},
"subtitle": "1994 \/ 法国 美国 \/ 剧情 动作 犯罪 \/ 吕克·贝松 \/ 让·雷诺 娜塔莉·波特曼",
"title": "这个杀手不太冷",
"url": "https:\/\/movie.douban.com\/subject\/1295644\/",
"target_id": "1295644",
"uri": "douban:\/\/douban.com\/movie\/1295644",
"cover_url": "https:\/\/qnmob3.doubanio.com\/view\/photo\/m_ratio_poster\/public\/p511118051.jpg?imageView2\/2\/q\/80\/w\/300\/h\/300\/format\/jpg",
"create_time": "2014-10-05 10:42:34",
"type": "movie",
"id": "19877286"
}, {
"comment": "",
"rating": {
"count": 2198702,
"max": 10,
"star_count": 4.5,
"value": 9.4
},
"subtitle": "2001 \/ 日本 \/ 剧情 动画 奇幻 \/ 宫崎骏 \/ 柊瑠美 入野自由",
"title": "千与千寻",
"url": "https:\/\/movie.douban.com\/subject\/1291561\/",
"target_id": "1291561",
"uri": "douban:\/\/douban.com\/movie\/1291561",
"cover_url": "https:\/\/qnmob3.doubanio.com\/view\/photo\/m_ratio_poster\/public\/p2557573348.jpg?imageView2\/2\/q\/80\/w\/300\/h\/300\/format\/jpg",
"create_time": "2014-10-05 10:47:12",
"type": "movie",
"id": "19877280"
}]
}
"""
return self.__invoke(self._urls["doulist_items"] % subject_id, start=start, count=count, _ts=ts)
78 changes: 60 additions & 18 deletions app/mediaserver/client/plex.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from functools import lru_cache

from urllib.parse import quote_plus
import log
from app.mediaserver.client._base import _IMediaClient
from app.utils import ExceptionUtils
Expand Down Expand Up @@ -103,7 +104,7 @@ def get_activity_log(self, num):
return []
ret_array = []
try:
# type的含义: 1 电影 4 剧集单集
# type的含义: 1 电影 4 剧集单集 详见 plexapi/utils.py中SEARCHTYPES的定义
# 根据最后播放时间倒序获取数据
historys = self._plex.library.search(sort='lastViewedAt:desc', limit=num, type='1,4')
for his in historys:
Expand Down Expand Up @@ -291,11 +292,51 @@ def refresh_root_library(self):

def refresh_library_by_items(self, items):
"""
按类型、名称、年份来刷新媒体库,未找到对应的API,直接刷整个库
按路径刷新媒体库
"""
if not self._plex:
return False
return self._plex.library.update()
# _libraries可能未初始化,初始化一下
if not self._libraries:
try:
self._libraries = self._plex.library.sections()
except Exception as err:
ExceptionUtils.exception_traceback(err)
result_dict = {}
for item in items:
file_path = item.get("file_path")
lib_key, path = self.__find_librarie(file_path, self._libraries)
# 如果存在同一剧集的多集,key(path)相同会合并
result_dict[path] = lib_key
if "" in result_dict:
# 如果有匹配失败的,刷新整个库
self._plex.library.update()
else:
# 否则一个一个刷新
for path, lib_key in result_dict.items():
log.info(f"【{self.client_name}】刷新媒体库:{lib_key} : {path}")
self._plex.query(f'/library/sections/{lib_key}/refresh?path={quote_plus(path)}')

@staticmethod
def __find_librarie(path, libraries):
"""
判断这个path属于哪个媒体库
多个媒体库配置的目录不应有重复和嵌套,
使用os.path.commonprefix([path, location]) == location应该没问题
"""
if path is None:
return "", ""
# 只要路径,不要文件名
dir_path = os.path.dirname(path)
try:
for lib in libraries:
if hasattr(lib, "locations") and lib.locations:
for location in lib.locations:
if os.path.commonprefix([dir_path, location]) == location:
return lib.key, dir_path
except Exception as err:
ExceptionUtils.exception_traceback(err)
return "", ""

def get_libraries(self):
"""
Expand All @@ -313,10 +354,10 @@ def get_libraries(self):
match library.type:
case "movie":
library_type = MediaType.MOVIE.value
image_list_str = self.get_libraries_image(library.key)
image_list_str = self.get_libraries_image(library.key, 1)
case "show":
library_type = MediaType.TV.value
image_list_str = self.get_libraries_image(library.key)
image_list_str = self.get_libraries_image(library.key, 2)
case _:
continue
libraries.append({
Expand All @@ -331,12 +372,18 @@ def get_libraries(self):
return libraries

@lru_cache(maxsize=10)
def get_libraries_image(self, library_key):
def get_libraries_image(self, library_key, type):
"""
获取媒体服务器最近添加的媒体的图片列表
param: library_key
param: type type的含义: 1 电影 2 剧集 详见 plexapi/utils.py中SEARCHTYPES的定义
"""
if not self._plex:
return ""
library = self._plex.library.sectionByID(library_key)
# 担心有些没图片,多获取几个
items = library.recentlyAdded(maxresults=8)
items = self._plex.fetchItems(f"/hubs/home/recentlyAdded?type={type}&sectionID={library_key}",
container_size=8,
container_start=0)
poster_urls = []
for item in items:
if item.posterUrl is not None:
Expand Down Expand Up @@ -491,12 +538,7 @@ def get_resume(self, num=12):
"""
if not self._plex:
return []
items = self._plex.library.search(**{
'sort': 'lastViewedAt:desc', # 按最后观看时间排序
'type': '1,4', # 1 电影 4 剧集单集
'viewOffset!': '0', # 播放进度不等于0的
'limit': num # 限制结果数量
})
items = self._plex.fetchItems('/hubs/continueWatching/items', container_start=0, container_size=num)
ret_resume = []
for item in items:
item_type = MediaType.MOVIE.value if item.TYPE == "movie" else MediaType.TV.value
Expand Down Expand Up @@ -525,14 +567,14 @@ def get_latest(self, num=20):
"""
if not self._plex:
return []
items = self._plex.library.recentlyAdded()
items = self._plex.fetchItems('/library/recentlyAdded', container_start=0, container_size=num)
ret_resume = []
for item in items[:num]:
for item in items:
item_type = MediaType.MOVIE.value if item.TYPE == "movie" else MediaType.TV.value
link = self.get_play_url(item.key)
title = item.title if item_type == MediaType.MOVIE.value else \
"%s 第%s季" % (item.parentTitle, item.index)
image = self.get_nt_image_url(item.artUrl)
image = self.get_nt_image_url(item.posterUrl)
ret_resume.append({
"id": item.key,
"name": title,
Expand Down
Loading

0 comments on commit 56be5c9

Please sign in to comment.