def get(_): today = datetime.datetime.now().date().strftime('%Y-%m-%d') update_date = Config.get_value_by_key(CI.UPDATE_DATE, '1900-01-01') # update_date = datetime.datetime.strptime(update_date, '%Y-%m-%d').date() if today == update_date: return 0 news = SPIDER.grab_today_news() economist = Economist.create( date=today, **news.d(), ) for content, section in zip(news.body, news.section): Paragraph.create( content=content, section=section, economist=economist, ) Config.update_value(CI.UPDATE_DATE, today) return 0
def get(_): crt_time = datetime.datetime.now().timestamp() if tidx.allow_key_maps_constructor(): # python >= 3.6 client = tidx(client='fetch_access_token', expire=0, access_token=0) else: client = tidx('client', 'expire', 'access_token').map(fetch_access_token=0) clients = [ client(wechat_client, CI.WX_ACCESS_TOKEN_EXPIRE, CI.WX_ACCESS_TOKEN), client(qiX_client, CI.QIX_ACCESS_TOKEN_EXPIRE, CI.QIX_ACCESS_TOKEN), ] for client in clients: expire_time = float(Config.get_value_by_key(client.expire) or '0') if crt_time + 10 * 60 > expire_time: data = client.fetch_access_token() Config.update_value(client.access_token, data['access_token']) Config.update_value(client.expire, str(crt_time + data['expires_in'])) return 0
def refresh_frequent_score(cls): from Config.models import Config crt_date = datetime.datetime.now().date() crt_time = datetime.datetime.now().timestamp() last_date = Config.get_value_by_key(CI.LAST_RE_FREQ_SCORE_DATE) last_date = datetime.datetime.strptime(last_date, '%Y-%m-%d').date() if last_date >= crt_date: raise AppError.SCORE_REFRESHED from OAuth.views import OAUTH_TOKEN_EXPIRE_TIME Config.update_value(CI.LAST_RE_FREQ_SCORE_DATE, crt_date.strftime('%Y-%m-%d')) for user_app in cls.objects.all(): if crt_time - float( user_app.last_auth_code_time) > OAUTH_TOKEN_EXPIRE_TIME + 24 * 60 * 60: if crt_time - float(user_app.last_score_changed_time) > OAUTH_TOKEN_EXPIRE_TIME: user_app.frequent_score /= 2 user_app.last_score_changed_time = crt_time user_app.save()
def update_access_token(): crt_time = int(datetime.datetime.now().timestamp()) last_update = int(Config.get_value_by_key(CI.WEIXIN_LAST_UPDATE, '0')) if crt_time - last_update < 60 * 80: # 80 mins raise WeixinError.UPDATE_WEIXIN_TIME_NOT_EXPIRED resp = requests.get( 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (APP_ID, APP_SECRET)) data = resp.json() resp.close() if ('errcode' in data and data['errcode'] != 0) or 'access_token' not in data: raise WeixinError.UPDATE_WEIXIN_ACCESS_TOKEN_ERROR Config.update_value(CI.WEIXIN_ACCESS_TOKEN, data['access_token']) resp = requests.get( 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi' % data['access_token']) data = resp.json() resp.close() if ('errcode' in data and data['errcode'] != 0) or 'ticket' not in data: raise WeixinError.UPDATE_WEIXIN_JSAPI_TICKET_ERROR Config.update_value(CI.WEIXIN_JSAPI_TICKET, data['ticket']) Config.update_value(CI.WEIXIN_LAST_UPDATE, str(crt_time))
def push(cls, o_music): pushing_date_str = Config.get_value_by_key('next-recommend-date', '2018-10-17').body pushing_date = datetime.datetime.strptime(pushing_date_str, '%Y-%m-%d').date() try: o_dr = cls( dat_e=pushing_date, re_music=o_music, ) o_dr.save() except Exception as err: deprint(str(err)) return Ret(Error.ERROR_CREATE_DAILYRECOMMEND) next_date = pushing_date + datetime.timedelta(days=1) next_date_str = next_date.strftime('%Y-%m-%d') Config.update_value('next-recommend-date', next_date_str) return Ret(o_dr)