Skip to content

Commit

Permalink
Merge pull request #2727 from qcgzxw/mediaserver
Browse files Browse the repository at this point in the history
  • Loading branch information
jxxghp authored Sep 16, 2024
2 parents 1920dc0 + 8306aa9 commit 3c43055
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 102 deletions.
163 changes: 111 additions & 52 deletions app/modules/emby/emby.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ def get_emby_folders(self) -> List[dict]:
"""
if not self._host or not self._apikey:
return []
req_url = "%semby/Library/SelectableMediaFolders?api_key=%s" % (self._host, self._apikey)
url = f"{self._host}emby/Library/SelectableMediaFolders"
params = {
'api_key': self._apikey
}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res:
return res.json()
else:
Expand All @@ -77,9 +80,12 @@ def get_emby_virtual_folders(self) -> List[dict]:
"""
if not self._host or not self._apikey:
return []
req_url = "%semby/Library/VirtualFolders/Query?api_key=%s" % (self._host, self._apikey)
url = f"{self._host}emby/Library/VirtualFolders/Query"
params = {
'api_key': self._apikey
}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res:
library_items = res.json().get("Items")
librarys = []
Expand Down Expand Up @@ -116,9 +122,10 @@ def __get_emby_librarys(self, username: str = None) -> List[dict]:
user = self.get_user(username)
else:
user = self.user
req_url = f"{self._host}emby/Users/{user}/Views?api_key={self._apikey}"
url = f"{self._host}emby/Users/{user}/Views"
params = {"api_key": self._apikey}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res:
return res.json().get("Items")
else:
Expand Down Expand Up @@ -166,9 +173,12 @@ def get_user(self, user_name: str = None) -> Optional[Union[str, int]]:
"""
if not self._host or not self._apikey:
return None
req_url = "%sUsers?api_key=%s" % (self._host, self._apikey)
url = f"{self._host}Users"
params = {
"api_key": self._apikey
}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res:
users = res.json()
# 先查询是否有与当前用户名称匹配的
Expand All @@ -195,7 +205,7 @@ def authenticate(self, username: str, password: str) -> Optional[str]:
"""
if not self._host or not self._apikey:
return None
req_url = "%semby/Users/AuthenticateByName" % self._host
url = f"{self._host}emby/Users/AuthenticateByName"
try:
res = RequestUtils(headers={
'X-Emby-Authorization': f'MediaBrowser Client="MoviePilot", '
Expand All @@ -206,7 +216,7 @@ def authenticate(self, username: str, password: str) -> Optional[str]:
'Content-Type': 'application/json',
"Accept": "application/json"
}).post_res(
url=req_url,
url=url,
data=json.dumps({
"Username": username,
"Pw": password
Expand All @@ -229,9 +239,12 @@ def get_server_id(self) -> Optional[str]:
"""
if not self._host or not self._apikey:
return None
req_url = "%sSystem/Info?api_key=%s" % (self._host, self._apikey)
url = f"{self._host}System/Info"
params = {
'api_key': self._apikey
}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res:
return res.json().get("Id")
else:
Expand All @@ -247,9 +260,12 @@ def get_user_count(self) -> int:
"""
if not self._host or not self._apikey:
return 0
req_url = "%semby/Users/Query?api_key=%s" % (self._host, self._apikey)
url = f"{self._host}emby/Users/Query"
params = {
'api_key': self._apikey
}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res:
return res.json().get("TotalRecordCount")
else:
Expand All @@ -266,9 +282,12 @@ def get_medias_count(self) -> schemas.Statistic:
"""
if not self._host or not self._apikey:
return schemas.Statistic()
req_url = "%semby/Items/Counts?api_key=%s" % (self._host, self._apikey)
url = f"{self._host}emby/Items/Counts"
params = {
'api_key': self._apikey
}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res:
result = res.json()
return schemas.Statistic(
Expand All @@ -292,18 +311,19 @@ def __get_emby_series_id_by_name(self, name: str, year: str) -> Optional[str]:
"""
if not self._host or not self._apikey:
return None
req_url = ("%semby/Items?"
"IncludeItemTypes=Series"
"&Fields=ProductionYear"
"&StartIndex=0"
"&Recursive=true"
"&SearchTerm=%s"
"&Limit=10"
"&IncludeSearchTypes=false"
"&api_key=%s") % (
self._host, name, self._apikey)
url = f"{self._host}emby/Items"
params={
"IncludeItemTypes": "Series",
"Fields": "ProductionYear",
"StartIndex": 0,
"Recursive": "true",
"SearchTerm": name,
"Limit": 10,
"IncludeSearchTypes": "false",
"api_key": self._apikey
}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res:
res_items = res.json().get("Items")
if res_items:
Expand All @@ -329,11 +349,19 @@ def get_movies(self,
"""
if not self._host or not self._apikey:
return None
req_url = "%semby/Items?IncludeItemTypes=Movie&Fields=ProductionYear&StartIndex=0" \
"&Recursive=true&SearchTerm=%s&Limit=10&IncludeSearchTypes=false&api_key=%s" % (
self._host, title, self._apikey)
url = f"{self._host}emby/Items"
params = {
"IncludeItemTypes": "Movie",
"Fields": "ProductionYear",
"StartIndex": 0,
"Recursive": "true",
"SearchTerm": title,
"Limit": 10,
"IncludeSearchTypes": "false",
"api_key": self._apikey
}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res:
res_items = res.json().get("Items")
if res_items:
Expand Down Expand Up @@ -403,9 +431,13 @@ def get_tv_episodes(self,
if not season:
season = ""
try:
req_url = "%semby/Shows/%s/Episodes?Season=%s&IsMissing=false&api_key=%s" % (
self._host, item_id, season, self._apikey)
res_json = RequestUtils().get_res(req_url)
url = f"{self._host}emby/Shows/{item_id}/Episodes"
params = {
"Season": season,
"IsMissing": "false",
"api_key": self._apikey
}
res_json = RequestUtils().get_res(url, params)
if res_json:
tv_item = res_json.json()
res_items = tv_item.get("Items")
Expand Down Expand Up @@ -438,9 +470,12 @@ def get_remote_image_by_id(self, item_id: str, image_type: str) -> Optional[str]
"""
if not self._host or not self._apikey:
return None
req_url = "%semby/Items/%s/RemoteImages?api_key=%s" % (self._host, item_id, self._apikey)
url = f"{self._host}emby/Items/{item_id}/RemoteImages"
params = {
"api_key": self._apikey
}
try:
res = RequestUtils(timeout=10).get_res(req_url)
res = RequestUtils(timeout=10).get_res(url, params)
if res:
images = res.json().get("Images")
if images:
Expand All @@ -465,9 +500,9 @@ def generate_external_image_link(self, item_id: str, image_type: str) -> Optiona
logger.error("Emby外网播放地址未能获取或为空")
return None

req_url = "%sItems/%s/Images/%s" % (self._playhost, item_id, image_type)
url = f"{self._playhost}Items/{item_id}/Images/{image_type}"
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url)
if res and res.status_code != 404:
logger.info(f"影片图片链接:{res.url}")
return res.url
Expand All @@ -484,9 +519,13 @@ def __refresh_emby_library_by_id(self, item_id: str) -> bool:
"""
if not self._host or not self._apikey:
return False
req_url = "%semby/Items/%s/Refresh?Recursive=true&api_key=%s" % (self._host, item_id, self._apikey)
url = f"{self._host}emby/Items/{item_id}/Refresh"
params = {
"Recursive": "true",
"api_key": self._apikey
}
try:
res = RequestUtils().post_res(req_url)
res = RequestUtils().post_res(url, params)
if res:
return True
else:
Expand All @@ -502,9 +541,12 @@ def refresh_root_library(self) -> bool:
"""
if not self._host or not self._apikey:
return False
req_url = "%semby/Library/Refresh?api_key=%s" % (self._host, self._apikey)
url = f"{self._host}emby/Library/Refresh"
params = {
"api_key": self._apikey
}
try:
res = RequestUtils().post_res(req_url)
res = RequestUtils().post_res(url, params)
if res:
return True
else:
Expand Down Expand Up @@ -581,9 +623,12 @@ def get_iteminfo(self, itemid: str) -> Optional[schemas.MediaServerItem]:
return None
if not self._host or not self._apikey:
return None
req_url = "%semby/Users/%s/Items/%s?api_key=%s" % (self._host, self.user, itemid, self._apikey)
url = f"{self._host}emby/Users/{self.user}/Items/{itemid}"
params = {
"api_key": self._apikey
}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res and res.status_code == 200:
item = res.json()
tmdbid = item.get("ProviderIds", {}).get("Tmdb")
Expand Down Expand Up @@ -612,9 +657,13 @@ def get_items(self, parent: str) -> Generator:
yield None
if not self._host or not self._apikey:
yield None
req_url = "%semby/Users/%s/Items?ParentId=%s&api_key=%s" % (self._host, self.user, parent, self._apikey)
url = f"{self._host}emby/Users/{self.user}/Items"
params = {
"ParentId": parent,
"api_key": self._apikey
}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res and res.status_code == 200:
results = res.json().get("Items") or []
for result in results:
Expand Down Expand Up @@ -1033,10 +1082,15 @@ def get_resume(self, num: int = 12, username: str = None) -> Optional[List[schem
user = self.get_user(username)
else:
user = self.user
req_url = (f"{self._host}Users/{user}/Items/Resume?"
f"Limit=100&MediaTypes=Video&api_key={self._apikey}&Fields=ProductionYear,Path")
url = f"{self._host}Users/{user}/Items/Resume"
params = {
"Limit": 100,
"MediaTypes": "Video",
"Fields": "ProductionYear,Path",
"api_key": self._apikey,
}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res:
result = res.json().get("Items") or []
ret_resume = []
Expand Down Expand Up @@ -1096,10 +1150,15 @@ def get_latest(self, num: int = 20, username: str = None) -> Optional[List[schem
user = self.get_user(username)
else:
user = self.user
req_url = (f"{self._host}Users/{user}/Items/Latest?"
f"Limit=100&MediaTypes=Video&api_key={self._apikey}&Fields=ProductionYear,Path")
url = f"{self._host}Users/{user}/Items/Latest"
params = {
"Limit": 100,
"MediaTypes": "Video",
"Fields": "ProductionYear,Path",
"api_key": self._apikey
}
try:
res = RequestUtils().get_res(req_url)
res = RequestUtils().get_res(url, params)
if res:
result = res.json() or []
ret_latest = []
Expand Down
Loading

0 comments on commit 3c43055

Please sign in to comment.