适配m-team新鉴权机制

This commit is contained in:
jxxghp 2024-04-29 20:19:46 +08:00
parent c06bdf0491
commit c2a40876e2
5 changed files with 51 additions and 41 deletions

View File

@ -105,9 +105,14 @@ class SiteChain(ChainBase):
"""
user_agent = site.ua or settings.USER_AGENT
url = f"{site.url}api/member/profile"
headers = {
"Content-Type": "application/json",
"User-Agent": user_agent,
"Accept": "application/json, text/plain, */*",
"Authorization": site.token
}
res = RequestUtils(
ua=user_agent,
cookies=site.cookie,
headers=headers,
proxies=settings.PROXY if site.proxy else None,
timeout=15
).post_res(url=url)
@ -115,8 +120,7 @@ class SiteChain(ChainBase):
user_info = res.json()
if user_info and user_info.get("data"):
# 更新最后访问时间
res = RequestUtils(cookies=site.cookie,
ua=user_agent,
res = RequestUtils(headers=headers,
timeout=60,
proxies=settings.PROXY if site.proxy else None,
referer=f"{site.url}index"

View File

@ -25,6 +25,10 @@ class Site(Base):
cookie = Column(String)
# User-Agent
ua = Column(String)
# ApiKey
apikey = Column(String)
# Token
token = Column(String)
# 是否使用代理 0-否1-是
proxy = Column(Integer)
# 过滤规则

View File

@ -34,6 +34,8 @@ class MTorrentSpider:
# API KEY
_apikey = None
# JWT Token
_token = None
# 标签
_labels = {
@ -58,49 +60,14 @@ class MTorrentSpider:
self._proxy = settings.PROXY
self._cookie = indexer.get('cookie')
self._ua = indexer.get('ua')
def __get_apikey(self) -> str:
"""
获取ApiKey
"""
domain_host = StringUtils.get_url_host(self._domain)
self._apikey = self.systemconfig.get(f"site.{domain_host}.apikey")
if not self._apikey:
try:
res = RequestUtils(
headers={
"Content-Type": "application/json",
"User-Agent": f"{self._ua}"
},
cookies=self._cookie,
ua=self._ua,
proxies=self._proxy,
referer=f"{self._domain}usercp?tab=laboratory",
timeout=15
).post_res(url=f"{self._domain}api/apikey/getKeyList")
if res and res.status_code == 200:
api_keys = res.json().get('data')
if api_keys:
logger.info(f"{self._name} 获取ApiKey成功")
# 按lastModifiedDate倒序排序
api_keys.sort(key=lambda x: x.get('lastModifiedDate'), reverse=True)
self._apikey = api_keys[0].get('apiKey')
self.systemconfig.set(f"site.{domain_host}.apikey", self._apikey)
else:
logger.warn(f"{self._name} 获取ApiKey失败请先在`控制台`->`实验室`建立存取令牌")
else:
logger.warn(f"{self._name} 获取ApiKey失败请检查Cookie是否有效")
except Exception as e:
logger.error(f"{self._name} 获取ApiKey出错{e}")
return self._apikey
self._apikey = indexer.get('apikey')
self._token = indexer.get('token')
def search(self, keyword: str, mtype: MediaType = None, page: int = 0) -> Tuple[bool, List[dict]]:
"""
搜索
"""
# 检查ApiKey
self.__get_apikey()
if not self._apikey:
return True, []

View File

@ -20,6 +20,10 @@ class Site(BaseModel):
cookie: Optional[str] = None
# User-Agent
ua: Optional[str] = None
# ApiKey
apikey: Optional[str] = None
# Token
token: Optional[str] = None
# 是否使用代理
proxy: Optional[int] = 0
# 过滤规则

View File

@ -0,0 +1,31 @@
"""1.0.18
Revision ID: 735c01e0453d
Revises: 9cb3993e340e
Create Date: 2024-04-29 19:40:38.375072
"""
import contextlib
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '735c01e0453d'
down_revision = '9cb3993e340e'
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with contextlib.suppress(Exception):
with op.batch_alter_table("site") as batch_op:
batch_op.add_column(sa.Column('apikey', sa.VARCHAR))
batch_op.add_column(sa.Column('token', sa.VARCHAR))
# ### end Alembic commands ###
def downgrade() -> None:
pass