Skip to content

Commit

Permalink
fix 增加播放限速
Browse files Browse the repository at this point in the history
  • Loading branch information
Shurelol committed Feb 7, 2023
1 parent 4933298 commit a82ba2f
Show file tree
Hide file tree
Showing 20 changed files with 456 additions and 28 deletions.
4 changes: 3 additions & 1 deletion app/conf/systemconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class SystemConfig:
# 自动获取Cookie的用户信息
"CookieUserInfo": {},
# 用户自定义CSS/JavsScript
"CustomScript": {}
"CustomScript": {},
# 播放限速设置
"SpeedLimit": {}
}

def __init__(self):
Expand Down
7 changes: 7 additions & 0 deletions app/downloader/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,10 @@ def get_downloading_progress(self):
获取下载进度
"""
pass

@abstractmethod
def set_speed_limit(self, **kwargs):
"""
设置速度限制
"""
pass
6 changes: 6 additions & 0 deletions app/downloader/client/aria2.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,9 @@ def get_downloading_progress(self, **kwargs):
'progress': progress
})
return DispTorrents

def set_speed_limit(self, **kwargs):
"""
设置速度限制
"""
pass
6 changes: 6 additions & 0 deletions app/downloader/client/client115.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,9 @@ def get_downloading_progress(self, **kwargs):
'progress': progress
})
return DispTorrents

def set_speed_limit(self, **kwargs):
"""
设置速度限制
"""
pass
6 changes: 6 additions & 0 deletions app/downloader/client/pikpak.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,9 @@ def get_downloading_progress(self, **kwargs):
'noprogress': True
})
return DispTorrents

def set_speed_limit(self, **kwargs):
"""
设置速度限制
"""
pass
15 changes: 15 additions & 0 deletions app/downloader/client/qbittorrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,18 @@ def get_downloading_progress(self, tag=None):
'progress': progress
})
return DispTorrents

def set_speed_limit(self, download_limit=None, upload_limit=None):
"""
设置速度限制
"""
if not self.qbc:
return
try:
if self.qbc.transfer.upload_limit != upload_limit:
self.qbc.transfer.upload_limit = upload_limit
if self.qbc.transfer.download_limit != download_limit:
self.qbc.transfer.download_limit = download_limit
except Exception as err:
ExceptionUtils.exception_traceback(err)
return False
27 changes: 27 additions & 0 deletions app/downloader/client/transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,30 @@ def get_downloading_progress(self, tag=None):
'progress': progress
})
return DispTorrents

def set_speed_limit(self, download_limit=None, upload_limit=None):
"""
设置速度限制
"""
if not self.trc:
return
try:
session = self.trc.get_session()
download_limit_enabled = True if download_limit else False
upload_limit_enabled = True if upload_limit else False
if download_limit_enabled == session.speed_limit_down_enabled and \
upload_limit_enabled == session.speed_limit_up_enabled and \
download_limit == session.speed_limit_down and \
upload_limit == session.speed_limit_up:
return
self.trc.set_session(
speed_limit_down=download_limit if download_limit != session.speed_limit_down
else session.speed_limit_down,
speed_limit_up=upload_limit if upload_limit != session.speed_limit_up
else session.speed_limit_up,
speed_limit_down_enabled=download_limit_enabled,
speed_limit_up_enabled=upload_limit_enabled
)
except Exception as err:
ExceptionUtils.exception_traceback(err)
return False
19 changes: 19 additions & 0 deletions app/downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,3 +1062,22 @@ def get_default_download_setting(self):
if not self._download_setting.get(default_download_setting):
default_download_setting = "-1"
return default_download_setting

def set_speed_limit(self, downloader, download_limit=None, upload_limit=None):
"""
设置速度限制
"""
if not downloader:
return []
_client = self.__get_client(downloader)
try:
download_limit = int(download_limit) if download_limit else 0
except Exception as err:
ExceptionUtils.exception_traceback(err)
download_limit = 0
try:
upload_limit = int(upload_limit) if upload_limit else 0
except Exception as err:
ExceptionUtils.exception_traceback(err)
upload_limit = 0
_client.set_speed_limit(download_limit=download_limit, upload_limit=upload_limit)
21 changes: 13 additions & 8 deletions app/helper/security_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ def __init__(self):
self.synology_webhook_allow_ip = security.get('synology_webhook_allow_ip') or {}

def check_mediaserver_ip(self, ip):
return self.webhook_allow_access(self.media_server_webhook_allow_ip, ip)
return self.allow_access(self.media_server_webhook_allow_ip, ip)

def check_telegram_ip(self, ip):
return self.webhook_allow_access(self.telegram_webhook_allow_ip, ip)
return self.allow_access(self.telegram_webhook_allow_ip, ip)

def check_synology_ip(self, ip):
return self.webhook_allow_access(self.synology_webhook_allow_ip, ip)
return self.allow_access(self.synology_webhook_allow_ip, ip)

def check_slack_ip(self, ip):
return self.webhook_allow_access({"ipve": "127.0.0.1"}, ip)
return self.allow_access({"ipve": "127.0.0.1"}, ip)

@staticmethod
def webhook_allow_access(allow_ips, ip):
def allow_access(allow_ips, ip):
"""
判断IP是否合法
:param allow_ips: 充许的IP范围 {"ipv4":, "ipv6":}
Expand All @@ -39,14 +39,19 @@ def webhook_allow_access(allow_ips, ip):
return True
try:
ipaddr = ipaddress.ip_address(ip)
if ipaddr.version == 4 or ipaddr.ipv4_mapped:
if ipaddr.version == 4:
if not allow_ips.get('ipv4'):
return True
allow_ipv4s = allow_ips.get('ipv4').split(",")
for allow_ipv4 in allow_ipv4s:
if ipaddr.version == 4 and ipaddr in ipaddress.ip_network(allow_ipv4):
if ipaddr in ipaddress.ip_network(allow_ipv4):
return True
if ipaddr.ipv4_mapped and ipaddr.ipv4_mapped in ipaddress.ip_network(allow_ipv4):
elif ipaddr.ipv4_mapped:
if not allow_ips.get('ipv4'):
return True
allow_ipv4s = allow_ips.get('ipv4').split(",")
for allow_ipv4 in allow_ipv4s:
if ipaddr.ipv4_mapped in ipaddress.ip_network(allow_ipv4):
return True
else:
if not allow_ips.get('ipv6'):
Expand Down
7 changes: 7 additions & 0 deletions app/mediaserver/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,10 @@ def get_items(self, parent):
:param parent: 上一级的ID
"""
pass

@abstractmethod
def get_playing_sessions(self):
"""
获取正在播放的会话
"""
pass
20 changes: 20 additions & 0 deletions app/mediaserver/client/emby.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,23 @@ def get_items(self, parent):
ExceptionUtils.exception_traceback(e)
log.error(f"【{self.server_type}】连接Users/Items出错:" + str(e))
yield {}

def get_playing_sessions(self):
"""
获取正在播放的会话
"""
if not self._host or not self._apikey:
return []
playing_sessions = []
req_url = "%semby/Sessions?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
if res and res.status_code == 200:
sessions = res.json()
for session in sessions:
if session.get("NowPlayingItem"):
playing_sessions.append(session)
return playing_sessions
except Exception as e:
ExceptionUtils.exception_traceback(e)
return []
6 changes: 6 additions & 0 deletions app/mediaserver/client/jellyfin.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,9 @@ def get_items(self, parent):
ExceptionUtils.exception_traceback(e)
log.error(f"【{self.server_type}】连接Users/Items出错:" + str(e))
yield {}

def get_playing_sessions(self):
"""
获取正在播放的会话
"""
pass
6 changes: 6 additions & 0 deletions app/mediaserver/client/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,9 @@ def get_items(self, parent):
except Exception as err:
ExceptionUtils.exception_traceback(err)
yield {}

def get_playing_sessions(self):
"""
获取正在播放的会话
"""
pass
16 changes: 12 additions & 4 deletions app/mediaserver/media_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,18 @@ def get_mediasync_status(self):

def get_iteminfo(self, itemid):
"""
根据ItemId从媒体服务器查询项目详情
:param itemid: 在Emby中的ID
:return: 图片对应在TMDB中的URL
"""
根据ItemId从媒体服务器查询项目详情
:param itemid: 在Emby中的ID
:return: 图片对应在TMDB中的URL
"""
if not self.server:
return None
return self.server.get_iteminfo(itemid)

def get_playing_sessions(self):
"""
获取正在播放的会话
"""
if not self.server:
return None
return self.server.get_playing_sessions()
Loading

0 comments on commit a82ba2f

Please sign in to comment.