feat: 增加指定的仓库Github token

This commit is contained in:
InfinityPacer 2024-07-06 16:07:46 +08:00
parent ffc72ba6fe
commit cafa4d217c
2 changed files with 43 additions and 4 deletions

View File

@ -224,6 +224,8 @@ class Settings(BaseSettings):
PLUGIN_MARKET: str = "https://github.com/jxxghp/MoviePilot-Plugins,https://github.com/thsrite/MoviePilot-Plugins,https://github.com/honue/MoviePilot-Plugins,https://github.com/InfinityPacer/MoviePilot-Plugins" PLUGIN_MARKET: str = "https://github.com/jxxghp/MoviePilot-Plugins,https://github.com/thsrite/MoviePilot-Plugins,https://github.com/honue/MoviePilot-Plugins,https://github.com/InfinityPacer/MoviePilot-Plugins"
# Github token提高请求api限流阈值 ghp_**** # Github token提高请求api限流阈值 ghp_****
GITHUB_TOKEN: Optional[str] = None GITHUB_TOKEN: Optional[str] = None
# 指定的仓库Github token多个仓库使用,分隔,格式:{user1}/{repo1}:ghp_****,{user2}/{repo2}:github_pat_****
GITHUB_TOKEN_FOR_REPO: Optional[str] = None
# Github代理服务器格式https://mirror.ghproxy.com/ # Github代理服务器格式https://mirror.ghproxy.com/
GITHUB_PROXY: Optional[str] = '' GITHUB_PROXY: Optional[str] = ''
# 自动检查和更新站点资源包(站点索引、认证等) # 自动检查和更新站点资源包(站点索引、认证等)
@ -362,6 +364,37 @@ class Settings(BaseSettings):
} }
return {} return {}
def GITHUB_HEADERS_FOR_REPO(self, repo: str = None):
"""
Github指定的仓库请求头
:param repo: 指定的仓库名称格式为 "user/repo"如果为空或者没有找到指定仓库请求头则返回默认的请求头信息
:return: Github请求头
"""
# 如果没有传入指定的仓库名称或没有配置指定的仓库Token则返回默认的请求头信息
if not repo or not self.GITHUB_TOKEN_FOR_REPO:
return self.GITHUB_HEADERS
headers = {}
# 格式:{user1}/{repo1}:ghp_****,{user2}/{repo2}:github_pat_****
token_pairs = self.GITHUB_TOKEN_FOR_REPO.split(",")
for token_pair in token_pairs:
try:
parts = token_pair.split(":")
if len(parts) != 2:
print(f"无效的令牌格式: {token_pair}")
continue
repo_info = parts[0].strip()
token = parts[1].strip()
if not repo_info or not token:
print(f"无效的令牌或仓库信息: {token_pair}")
continue
headers[repo_info] = {
"Authorization": f"Bearer {token}"
}
except Exception as e:
print(f"处理令牌对 '{token_pair}' 时出错: {e}")
# 如果传入了指定的仓库名称,则返回该仓库的请求头信息,否则返回默认请求头
return headers.get(repo, self.GITHUB_HEADERS)
@property @property
def DEFAULT_DOWNLOADER(self): def DEFAULT_DOWNLOADER(self):
""" """

View File

@ -51,7 +51,8 @@ class PluginHelper(metaclass=Singleton):
if not user or not repo: if not user or not repo:
return {} return {}
raw_url = self._base_url % (user, repo) raw_url = self._base_url % (user, repo)
res = RequestUtils(proxies=self.proxies, headers=settings.GITHUB_HEADERS, res = RequestUtils(proxies=self.proxies,
headers=settings.GITHUB_HEADERS_FOR_REPO(repo=f"{user}/{repo}"),
timeout=10).get_res(f"{raw_url}package.json") timeout=10).get_res(f"{raw_url}package.json")
if res: if res:
try: try:
@ -137,12 +138,16 @@ class PluginHelper(metaclass=Singleton):
if not user or not repo: if not user or not repo:
return False, "不支持的插件仓库地址格式" return False, "不支持的插件仓库地址格式"
user_repo = f"{user}/{repo}"
def __get_filelist(_p: str) -> Tuple[Optional[list], Optional[str]]: def __get_filelist(_p: str) -> Tuple[Optional[list], Optional[str]]:
""" """
获取插件的文件列表 获取插件的文件列表
""" """
file_api = f"https://api.github.com/repos/{user}/{repo}/contents/plugins/{_p}" file_api = f"https://api.github.com/repos/{user_repo}/contents/plugins/{_p}"
r = RequestUtils(proxies=settings.PROXY, headers=settings.GITHUB_HEADERS, timeout=30).get_res(file_api) r = RequestUtils(proxies=settings.PROXY,
headers=settings.GITHUB_HEADERS_FOR_REPO(repo=user_repo),
timeout=30).get_res(file_api)
if r is None: if r is None:
return None, "连接仓库失败" return None, "连接仓库失败"
elif r.status_code != 200: elif r.status_code != 200:
@ -164,7 +169,8 @@ class PluginHelper(metaclass=Singleton):
download_url = f"{settings.GITHUB_PROXY}{item.get('download_url')}" download_url = f"{settings.GITHUB_PROXY}{item.get('download_url')}"
# 下载插件文件 # 下载插件文件
res = RequestUtils(proxies=self.proxies, res = RequestUtils(proxies=self.proxies,
headers=settings.GITHUB_HEADERS, timeout=60).get_res(download_url) headers=settings.GITHUB_HEADERS_FOR_REPO(repo=user_repo),
timeout=60).get_res(download_url)
if not res: if not res:
return False, f"文件 {item.get('name')} 下载失败!" return False, f"文件 {item.get('name')} 下载失败!"
elif res.status_code != 200: elif res.status_code != 200: