def load_pan_acc_list_by_user(self, user_id): acc: Accounts = DataDao.account_by_id(user_id) pan_acc_list = DataDao.pan_account_list(user_id) l = len(pan_acc_list) result = {} need_renew_pan_acc = [] for pan_acc in pan_acc_list: if pan_acc.client_id != self.client_id or pan_acc.client_secret != self.client_secret: need_renew_access_token = True need_renew_pan_acc.append({ "id": pan_acc.id, "name": pan_acc.name, "use_cnt": pan_acc.use_count, "refresh": False, 'auth': self.pan_auth }) elif pan_acc.access_token and pan_acc.token_updated_at: tud = arrow.get( pan_acc.token_updated_at).replace(tzinfo=self.default_tz) if (arrow.now(self.default_tz) - tud).total_seconds() > PAN_ACCESS_TOKEN_TIMEOUT: need_renew_access_token = True need_renew_pan_acc.append({ "id": pan_acc.id, "name": pan_acc.name, "use_cnt": pan_acc.use_count, "refresh": True, 'auth': self.pan_auth }) else: need_renew_access_token = True need_renew_pan_acc.append({ "id": pan_acc.id, "name": pan_acc.name, "use_cnt": pan_acc.use_count, "refresh": True, 'auth': self.pan_auth }) return need_renew_pan_acc
def recheck_shared_d_link(self, shared_log_id): share_log: ShareLogs = DataDao.query_shared_log_by_pk_id(shared_log_id) if share_log: data_item: DataItem = DataDao.get_data_item_by_fs_id( share_log.fs_id) need_sync = False # print("query_file dlink:", data_item.dlink) if not data_item.dlink_updated_at or not data_item.dlink: need_sync = True elif data_item.dlink_updated_at: dt = arrow.get( data_item.dlink_updated_at).replace(tzinfo=self.default_tz) if dt.shift(hours=+DLINK_TIMEOUT) < arrow.now(): need_sync = True if need_sync: account_id = data_item.account_id acc: Accounts = DataDao.account_by_id(account_id) pan_acc: PanAccounts = self.get_pan_account( data_item.panacc, data_item.account_id) # sync_list = restapi.sync_file(self.pan_acc.access_token, [int(data_item.fs_id)]) sync_dlink, thumbs = restapi.get_dlink_by_sync_file( pan_acc.access_token, int(data_item.fs_id)) if sync_dlink: data_item.dlink = sync_dlink data_item.dlink_updated_at = get_now_datetime() DataDao.update_data_item( data_item.id, { "dlink": data_item.dlink, "dlink_updated_at": data_item.dlink_updated_at }) share_log.dlink = data_item.dlink DataDao.update_share_log_by_pk(share_log.id, {'dlink': data_item.dlink}) return ShareLogs.to_dict(share_log) return None
def bd_sync_login(self, params): acc_name = params.get('acc_name') refresh_token = params.get('refresh_token') access_token = params.get('access_token') expires_in = params.get('expires_in') userid = int(params.get('userid')) portrait = params.get('portrait') username = params.get('username') openid = params.get('openid') is_realname = int(params.get('is_realname', '1')) realname = '' userdetail = '' birthday = '' marriage = '' sex = '' blood = '' figure = '' constellation = '' education = '' trade = '' job = '' expires_in = expires_in - 20 * 60 # seconds expires_at = arrow.now( self.default_tz).shift(seconds=+expires_in).datetime acc_ext: AccountExt = DataDao.account_ext_by_bd_user_id(userid) # print("find acc_ext:", acc_ext) log.info("bd_sync_login bd userid:{}".format(userid)) now_tm = get_now_datetime() result = {} if not acc_ext: # new user # print("not find acc_ext userid:", userid) log.info("bd_sync_login not find acc_ext :{}".format(userid)) user_token, user_ext_dict = self._new_user( acc_name, '654321', username, access_token, refresh_token, expires_at, dict(realname=realname, portrait=portrait, userdetail=userdetail, birthday=birthday, marriage=marriage, sex=sex, blood=blood, figure=figure, constellation=constellation, education=education, trade=trade, job=job, username=username, is_realname=is_realname, user_id=userid)) # acc_id = user_ext_dict['_id'] # DataDao.new_accounts_ext(userid, username, realname, portrait, userdetail, birthday, marriage, sex, # blood, figure, constellation, education, trade, job, is_realname, # account_id=acc_id) login_updated_at = user_ext_dict['login_updated_at'] lud = arrow.get(login_updated_at).replace(tzinfo=self.default_tz) result['token'] = user_token result['login_at'] = int(arrow.get(lud).timestamp * 1000) # print('login_at:', result['login_at']) result['pan_acc_list'] = [] result['username'] = username result['portrait'] = portrait result['id'] = user_ext_dict['id'] else: # print("find acc_ext:", acc_ext.username) log.info("bd_sync_login find acc_ext :{}".format(userid)) acc_id = acc_ext.account_id account: Accounts = DataDao.account_by_id(acc_id) DataDao.update_account_ext_by_user_id( userid, dict(username=username, portrait=portrait, account_id=acc_id)) pan_acc: PanAccounts = DataDao.pan_account_by_bd_uid( acc_id, acc_ext.user_id) if not pan_acc: pan_acc = DataDao.pan_account_by_name(acc_id, acc_name) if pan_acc: if pan_acc.pin != 1: n = DataDao.query_pan_acc_count_by_acc_id(acc_id) if n > 1: DataDao.update_pan_account_by_acc_id( acc_id, {'pin': 0}) DataDao.update_pan_account_by_pk( pan_acc.id, { "access_token": access_token, "refresh_token": refresh_token, "expires_at": expires_at, "token_updated_at": now_tm, "pin": 1 }) else: client_id = PAN_SERVICE['client_id'] client_secret = PAN_SERVICE['client_secret'] pan_acc_id = DataDao.new_pan_account(acc_id, acc_name, client_id, client_secret, access_token, refresh_token, expires_at, get_now_datetime(), pin=1, bd_uid=acc_ext.user_id) pan_acc = self.get_pan_account(pan_acc_id, acc_id) result = self.login_check_user(account) return result
def query_file(self, item_id): data_item: DataItem = DataDao.get_data_item_by_id(item_id) need_sync = False logger.info("query_file dlink:{}".format(data_item.dlink)) if not data_item.dlink_updated_at or not data_item.dlink: need_sync = True elif data_item.dlink_updated_at: dt = arrow.get( data_item.dlink_updated_at).replace(tzinfo=self.default_tz) if dt.shift(hours=+DLINK_TIMEOUT) < arrow.now(): need_sync = True account_id = data_item.account_id acc: Accounts = DataDao.account_by_id(account_id) flv_json = None need_thumbs = False # data_item_ext = None # if data_item.category == 1 and is_video_media(data_item.filename): # data_item_ext = DataDao.get_data_item_ext_by_id(data_item.id) if is_image_media(data_item.filename) and data_item.category == 3: need_thumbs = True if need_sync: pan_acc: PanAccounts = self.get_pan_account( data_item.panacc, data_item.account_id) # sync_list = restapi.sync_file(self.pan_acc.access_token, [int(data_item.fs_id)]) sync_dlink, thumbs = restapi.get_dlink_by_sync_file( pan_acc.access_token, int(data_item.fs_id), need_thumbs) if sync_dlink: data_item.dlink = "{}&access_token={}".format( sync_dlink, pan_acc.access_token) data_item.dlink_updated_at = get_now_datetime() data_item_params = { "dlink": data_item.dlink, "dlink_updated_at": data_item.dlink_updated_at } if need_thumbs: if "url3" in thumbs: data_item_params["thumb"] = thumbs["url3"] data_item.thumb = data_item_params["thumb"] elif "url2" in thumbs: data_item_params["thumb"] = thumbs["url2"] data_item.thumb = data_item_params["thumb"] elif "url1" in thumbs: data_item_params["thumb"] = thumbs["url1"] data_item.thumb = data_item_params["thumb"] elif "icon" in thumbs: data_item_params["thumb"] = thumbs["icon"] data_item.thumb = data_item_params["thumb"] DataDao.update_data_item(data_item.id, data_item_params) # not authorized # if data_item.category == 1 and is_video_media(data_item.filename): # flv_json = restapi.get_media_flv_info(pan_acc.access_token, data_item.path) # if flv_json and "mlink" in flv_json: # flv_params = {"fs_id": data_item.fs_id, "mlink": flv_json["mlink"], # "start_at_time": flv_json["mlink_start_at"]} # if data_item_ext: # data_item_ext.mlink = flv_params["mlink"] # data_item_ext.start_at_time = flv_params["start_at_time"] # DataDao.update_data_item_ext(data_item.id, flv_params) # else: # data_item_ext = DataDao.new_data_item_ext(data_item.id, flv_params) used_pan_acc_id = data_item.panacc if data_item: data_item.size = int(data_item.size / 1024) f_type = guess_file_type(data_item.filename) params = {"item": DataItem.to_dict(data_item, ['id', 'parent'])} params["item"]["id"] = obfuscate_id(data_item.id) params["item"]["type"] = f_type params["item"]["media_type"] = self.check_data_item_media_type( data_item.category, data_item.filename) params["item"]["dlink_tokens"] = [used_pan_acc_id] # if data_item.category == 1 and is_video_media(data_item.filename) and data_item_ext: # params["item"]["mlink"] = data_item_ext.mlink # params["item"]["start_at_time"] = data_item_ext.start_at_time return params
def account_by_id(cls, account_id) -> Accounts: return DataDao.account_by_id(account_id)