async def get_by_uid(cls, user_id, available=None): if user_id == "*": objs = await cls.execute( LTUserCookie.select().where(LTUserCookie.available == True)) return objs[0] if user_id == "DD": user_id = 20932326 elif user_id == "LP": user_id = 39748080 elif user_id == "TZ": user_id = 312186483 elif user_id == "CZ": user_id = 87301592 if available is None: query = LTUserCookie.select().where( LTUserCookie.DedeUserID == user_id) else: query = LTUserCookie.select().where( (LTUserCookie.DedeUserID == user_id) & (LTUserCookie.available == available)) objs = await cls.execute(query) if objs: return objs[0] return None
async def get_objs(cls, available=None, is_vip=None, non_blocked=None, separate=False): query = LTUserCookie.select() if available is not None: query = query.where(LTUserCookie.available == available) if is_vip is not None: query = query.where(LTUserCookie.is_vip == is_vip) if non_blocked is not None: three_hour_ago = datetime.datetime.now() - datetime.timedelta( hours=BLOCK_FRESH_TIME) query = query.where(LTUserCookie.blocked_time < three_hour_ago) important_objs = [] objs = [] for o in await cls.execute(query): if o.DedeUserID in cls.IMPORTANT_UID_LIST: important_objs.append(o) else: objs.append(o) if separate: return important_objs, objs else: return important_objs + objs
async def set_invalid(cls, obj_or_user_id): if isinstance(obj_or_user_id, LTUserCookie): cookie_obj = obj_or_user_id else: objs = await cls.execute(LTUserCookie.select().where( LTUserCookie.DedeUserID == obj_or_user_id)) if not objs: return False, "Cannot get LTUserCookie obj." cookie_obj = objs[0] cookie_obj.available = False await cls._objects.update(cookie_obj, only=("available", )) user_in_period = await LtUserLoginPeriodOfValidity.in_period( user_id=cookie_obj.DedeUserID) user_in_iptt_list = cookie_obj.DedeUserID in cls.IMPORTANT_UID_LIST if (user_in_iptt_list or user_in_period) and cookie_obj.account and cookie_obj.password: for try_times in range(3): flag, data = await cls.add_cookie_by_account( account=cookie_obj.account, password=cookie_obj.password) if flag: # send_cookie_relogin_notice(cookie_obj) return True, "" else: logging.error( f"Failed to login user: {cookie_obj.name}(uid: {cookie_obj.user_id}), " f"try times: {try_times}, error msg: {data}") await asyncio.sleep(1) send_cookie_invalid_notice(cookie_obj) return True, ""
async def add_cookie_by_account(cls, account, password, notice_email=None): objs = await cls.execute( LTUserCookie.select().where(LTUserCookie.account == account)) if not objs: return False, "你不在白名单里。提前联系站长经过允许才可以使用哦。" lt_user = objs[0] if lt_user.available: return True, lt_user flag, data = await CookieFetcher.login(account, password) if not flag: return False, data lt_user.password = password lt_user.cookie_expire_time = datetime.datetime.now( ) + datetime.timedelta(days=30) lt_user.available = True attrs = ["password", "cookie_expire_time", "available"] for k, v in data.items(): setattr(lt_user, k, v) attrs.append(k) flag, data, uname = await BiliApi.get_if_user_is_live_vip( cookie=lt_user.cookie, user_id=lt_user.uid, return_uname=True) if not flag: return False, "无法获取你的个人信息,请稍后再试。" lt_user.is_vip = data lt_user.name = uname attrs.extend(["is_vip", "name"]) if notice_email is not None: lt_user.notice_email = notice_email attrs.append("notice_email") for obj in await cls._objects.execute(LTUserCookie.select().where( (LTUserCookie.DedeUserID == lt_user.DedeUserID) & (LTUserCookie.id != lt_user.id))): await cls._objects.delete(obj) await cls._objects.update(lt_user, only=attrs) if lt_user.uid in (g.BILI_UID_CZ, g.BILI_UID_TZ): await ReqFreLimitApi.set_available_cookie_for_xnode() return True, lt_user
async def del_uid_or_account_from_white_list(cls, uid=None, account=None): count = 0 if uid: objs = await cls._objects.execute( LTUserCookie.select().where(LTUserCookie.DedeUserID == uid)) for obj in objs: count += 1 await cls._objects.delete(obj) if account: objs = await cls._objects.execute( LTUserCookie.select().where(LTUserCookie.account == account)) for obj in objs: count += 1 await cls._objects.delete(obj) return count
async def set_blocked(cls, obj_or_user_id): if isinstance(obj_or_user_id, LTUserCookie): cookie_obj = obj_or_user_id else: objs = await cls.execute(LTUserCookie.select().where( LTUserCookie.DedeUserID == obj_or_user_id)) if not objs: return False, "Cannot get LTUserCookie obj." cookie_obj = objs[0] cookie_obj.blocked_time = datetime.datetime.now() await cls._objects.update(cookie_obj, only=("blocked_time", )) return True, cookie_obj
async def set_vip(cls, obj_or_user_id, is_vip): if isinstance(obj_or_user_id, LTUserCookie): cookie_obj = obj_or_user_id else: objs = await cls.execute(LTUserCookie.select().where( LTUserCookie.DedeUserID == obj_or_user_id)) if not objs: return False, "Cannot get LTUserCookie obj." cookie_obj = objs[0] cookie_obj.is_vip = bool(is_vip) await cls._objects.update(cookie_obj, only=("is_vip", )) return True, cookie_obj
async def refresh_token(cls, obj_or_user_id): if isinstance(obj_or_user_id, LTUserCookie): cookie_obj = obj_or_user_id else: objs = await cls.execute(LTUserCookie.select().where( LTUserCookie.DedeUserID == obj_or_user_id)) if not objs: return False, "Cannot get LTUserCookie obj." cookie_obj = objs[0] if not cookie_obj.available: return False, "User not available!" if not cookie_obj.account or not cookie_obj.password: return False, "No account or password." user_in_period = await LtUserLoginPeriodOfValidity.in_period( user_id=cookie_obj.DedeUserID) user_in_iptt_list = cookie_obj.DedeUserID in cls.IMPORTANT_UID_LIST if not user_in_period and not user_in_iptt_list: return False, "User not in period." if not cookie_obj.refresh_token or not cookie_obj.access_token: # 重新登录 return await cls.set_invalid(cookie_obj) flag, r = await CookieFetcher.fresh_token(cookie_obj.cookie, cookie_obj.access_token, cookie_obj.refresh_token) if not flag: return False, f"User {cookie_obj.name}(uid: {cookie_obj.uid}) cannot fresh_token: {r}" attrs = [] for k, v in r.items(): setattr(cookie_obj, k, v) attrs.append(k) await cls._objects.update(cookie_obj, only=attrs) logging.info( f"User {cookie_obj.name}(uid: {cookie_obj.uid}) access token refresh success!" ) return True, ""