def subject(cls, subject_id: int, url: str): db_session = Session() title = "" with requests.Session() as http_client: bangumi_id = get_bangumi_id_from_url(url) album_id = http_client.get(url).text s = cls.alb_regex.search(album_id) if not s or not s.groups(): logger.info("can't find albumId in {}", url) return soup = BeautifulSoup(album_id, "lxml") title_el = soup.find("a", class_="info-intro-title") if title_el: title = title_el.attrs["title"] album_id = s.groups()[0] r = http_client.get( "https://pcw-api.iqiyi.com/albums/album/avlistinfo", params={ "aid": album_id, "page": 1, "size": 10000, }, headers={ "user-agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) " "AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/74.0.3729.169 Safari/537.36") }, ) list_info = r.json() try: insert_stmt = sa.insert( sa.BangumiIqiyi, values={ "subject_id": subject_id, "bangumi_id": bangumi_id, "title": title, }, ) query = insert_stmt.on_duplicate_key_update( **preserve_fields(insert_stmt, "title", "bangumi_id")) db_session.execute(query) if list_info["data"] == "参数错误": logger.error("参数错误 with album id {}", album_id) return else: eps = [ ApiResult.parse_obj(x) for x in list_info["data"]["epsodelist"] ] bgm_eps = bgm_tv.mirror.subject_eps(subject_id).eps bgm_ep_start = min(x.sort for x in bgm_eps) ep_start = min(x.order for x in eps) for ep in eps: for bgm_ep in bgm_eps: if (bgm_ep.sort - bgm_ep_start) == (ep.order - ep_start): upsert_ep_iqiyi( db_session, { "ep_id": bgm_ep.id, "source_ep_id": ep.ep_id, "subject_id": subject_id, "title": ep.title, }, ) break db_session.commit() except Exception: db_session.rollback() raise finally: db_session.close()
async def oauth_callback( code: str = None, db: Database = Depends(get_db), redis: PickleRedis = Depends(get_redis), aio_client: aiohttp.ClientSession = Depends(aiohttp_session), ): redirect_response = RedirectResponse("./bgm.tv_auth") if code is None: return redirect_response try: async with aio_client.post( "https://bgm.tv/oauth/access_token", data={ "code": code, "client_id": config.BgmTvAutoTracker.APP_ID, "grant_type": "authorization_code", "redirect_uri": CALLBACK_URL, "client_secret": config.BgmTvAutoTracker.APP_SECRET, }, ) as auth_resp: auth_time = dateutil.parser.parse( auth_resp.headers["Date"]).timestamp() auth_response = AuthResponse.parse_obj(await auth_resp.json()) async with await aio_client.get( f"https://mirror.api.bgm.rin.cat/user/{auth_response.user_id}" ) as user_info_resp: user_info = UserInfo.parse_obj(await user_info_resp.json()) user = Me(auth_time=auth_time, **auth_response.dict()) insert_stmt = sa.insert(sa.UserToken) query = insert_stmt.on_duplicate_key_update( **preserve_fields( insert_stmt, "token_type", "expires_in", "auth_time", "access_token", "refresh_token", "scope", "username", "nickname", "usergroup", ), ) await db.execute( query, { "user_id": user.user_id, "token_type": user.token_type, "expires_in": user.expires_in, "auth_time": user.auth_time, "access_token": user.access_token, "refresh_token": user.refresh_token, "scope": auth_response.scope or "", "username": user_info.username, "nickname": user_info.nickname, "usergroup": user_info.usergroup.value, }, ) session = await new_session(user_id=auth_response.user_id, redis=redis) response = JSONResponse({"api_key": session.api_key}) response.set_cookie(cookie_scheme.model.name, session.api_key) return response except aiohttp.ServerTimeoutError: return JSONResponse( content={"detail": f"connect to bgm.tv timeout"}, status_code=HTTP_503_SERVICE_UNAVAILABLE, ) except ( json.decoder.JSONDecodeError, ValidationError, ConnectionError, aiohttp.ServerConnectionError, ): return redirect_response
def upsert_ep_iqiyi(db_session: Session, values): insert_stmt = sa.insert(sa.EpIqiyi, values=values) query = insert_stmt.on_duplicate_key_update( **preserve_fields(insert_stmt, "title", "subject_id", "ep_id")) db_session.execute(query)
def upsert_bilibili_bangumi(db_session: Session, values): insert_stmt = sa.insert(sa.BangumiBilibili, values=values) query = insert_stmt.on_duplicate_key_update( **preserve_fields(insert_stmt, "title", "media_id")) db_session.execute(query)