From c2a40876e2c493328d3263f911a0aa1a1595d080 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Mon, 29 Apr 2024 20:19:46 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8Dm-team=E6=96=B0=E9=89=B4?= =?UTF-8?q?=E6=9D=83=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/site.py | 12 ++++--- app/db/models/site.py | 4 +++ app/modules/indexer/mtorrent.py | 41 +++--------------------- app/schemas/site.py | 4 +++ database/versions/735c01e0453d_1_0_18.py | 31 ++++++++++++++++++ 5 files changed, 51 insertions(+), 41 deletions(-) create mode 100644 database/versions/735c01e0453d_1_0_18.py diff --git a/app/chain/site.py b/app/chain/site.py index de111059..dc53ae9b 100644 --- a/app/chain/site.py +++ b/app/chain/site.py @@ -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" diff --git a/app/db/models/site.py b/app/db/models/site.py index 297e8813..3f21e10e 100644 --- a/app/db/models/site.py +++ b/app/db/models/site.py @@ -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) # 过滤规则 diff --git a/app/modules/indexer/mtorrent.py b/app/modules/indexer/mtorrent.py index cbd25825..1f1194e3 100644 --- a/app/modules/indexer/mtorrent.py +++ b/app/modules/indexer/mtorrent.py @@ -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, [] diff --git a/app/schemas/site.py b/app/schemas/site.py index 0c0fe800..50885cc0 100644 --- a/app/schemas/site.py +++ b/app/schemas/site.py @@ -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 # 过滤规则 diff --git a/database/versions/735c01e0453d_1_0_18.py b/database/versions/735c01e0453d_1_0_18.py new file mode 100644 index 00000000..999974f7 --- /dev/null +++ b/database/versions/735c01e0453d_1_0_18.py @@ -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