适配m-team新鉴权机制
This commit is contained in:
parent
c06bdf0491
commit
c2a40876e2
@ -105,9 +105,14 @@ class SiteChain(ChainBase):
|
|||||||
"""
|
"""
|
||||||
user_agent = site.ua or settings.USER_AGENT
|
user_agent = site.ua or settings.USER_AGENT
|
||||||
url = f"{site.url}api/member/profile"
|
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(
|
res = RequestUtils(
|
||||||
ua=user_agent,
|
headers=headers,
|
||||||
cookies=site.cookie,
|
|
||||||
proxies=settings.PROXY if site.proxy else None,
|
proxies=settings.PROXY if site.proxy else None,
|
||||||
timeout=15
|
timeout=15
|
||||||
).post_res(url=url)
|
).post_res(url=url)
|
||||||
@ -115,8 +120,7 @@ class SiteChain(ChainBase):
|
|||||||
user_info = res.json()
|
user_info = res.json()
|
||||||
if user_info and user_info.get("data"):
|
if user_info and user_info.get("data"):
|
||||||
# 更新最后访问时间
|
# 更新最后访问时间
|
||||||
res = RequestUtils(cookies=site.cookie,
|
res = RequestUtils(headers=headers,
|
||||||
ua=user_agent,
|
|
||||||
timeout=60,
|
timeout=60,
|
||||||
proxies=settings.PROXY if site.proxy else None,
|
proxies=settings.PROXY if site.proxy else None,
|
||||||
referer=f"{site.url}index"
|
referer=f"{site.url}index"
|
||||||
|
@ -25,6 +25,10 @@ class Site(Base):
|
|||||||
cookie = Column(String)
|
cookie = Column(String)
|
||||||
# User-Agent
|
# User-Agent
|
||||||
ua = Column(String)
|
ua = Column(String)
|
||||||
|
# ApiKey
|
||||||
|
apikey = Column(String)
|
||||||
|
# Token
|
||||||
|
token = Column(String)
|
||||||
# 是否使用代理 0-否,1-是
|
# 是否使用代理 0-否,1-是
|
||||||
proxy = Column(Integer)
|
proxy = Column(Integer)
|
||||||
# 过滤规则
|
# 过滤规则
|
||||||
|
@ -34,6 +34,8 @@ class MTorrentSpider:
|
|||||||
|
|
||||||
# API KEY
|
# API KEY
|
||||||
_apikey = None
|
_apikey = None
|
||||||
|
# JWT Token
|
||||||
|
_token = None
|
||||||
|
|
||||||
# 标签
|
# 标签
|
||||||
_labels = {
|
_labels = {
|
||||||
@ -58,49 +60,14 @@ class MTorrentSpider:
|
|||||||
self._proxy = settings.PROXY
|
self._proxy = settings.PROXY
|
||||||
self._cookie = indexer.get('cookie')
|
self._cookie = indexer.get('cookie')
|
||||||
self._ua = indexer.get('ua')
|
self._ua = indexer.get('ua')
|
||||||
|
self._apikey = indexer.get('apikey')
|
||||||
def __get_apikey(self) -> str:
|
self._token = indexer.get('token')
|
||||||
"""
|
|
||||||
获取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
|
|
||||||
|
|
||||||
def search(self, keyword: str, mtype: MediaType = None, page: int = 0) -> Tuple[bool, List[dict]]:
|
def search(self, keyword: str, mtype: MediaType = None, page: int = 0) -> Tuple[bool, List[dict]]:
|
||||||
"""
|
"""
|
||||||
搜索
|
搜索
|
||||||
"""
|
"""
|
||||||
# 检查ApiKey
|
# 检查ApiKey
|
||||||
self.__get_apikey()
|
|
||||||
|
|
||||||
if not self._apikey:
|
if not self._apikey:
|
||||||
return True, []
|
return True, []
|
||||||
|
|
||||||
|
@ -20,6 +20,10 @@ class Site(BaseModel):
|
|||||||
cookie: Optional[str] = None
|
cookie: Optional[str] = None
|
||||||
# User-Agent
|
# User-Agent
|
||||||
ua: Optional[str] = None
|
ua: Optional[str] = None
|
||||||
|
# ApiKey
|
||||||
|
apikey: Optional[str] = None
|
||||||
|
# Token
|
||||||
|
token: Optional[str] = None
|
||||||
# 是否使用代理
|
# 是否使用代理
|
||||||
proxy: Optional[int] = 0
|
proxy: Optional[int] = 0
|
||||||
# 过滤规则
|
# 过滤规则
|
||||||
|
31
database/versions/735c01e0453d_1_0_18.py
Normal file
31
database/versions/735c01e0453d_1_0_18.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user