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 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 init(request): """ 初始化网站数据 """ Config.create(GLOBAL_SIGNAL, '0') Config.create(GLOBAL_INTERVAL, '20') Config.create('lasting', '10') # 默认新闻频率统计范围 Config.create('interval', '10') # 默认统计数据时间间隔 Config.create('analyse-signal', '0') # 默认统计数据时间 # Admin.create('cp', 'Chaping321') return response()
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 news_dealer(request): """ 网站统一抓取 """ crt_time = int(datetime.datetime.now().timestamp()) last_time = int(Config.objects.get(key=GLOBAL_SIGNAL).value) if crt_time - last_time < int(Config.objects.get(key=GLOBAL_INTERVAL).value): return response() crt_time = datetime.datetime.now() old_time = crt_time - datetime.timedelta(days=1) # 抓取函数列表 funcs = [ qdaily_grab, cnbeta_grab, techweb_grab, sspai_grab, leiphone_grab, dgtle_grab, ithome_grab, kr_grab, ninetofivemac_grab, ninetofivegoogle_grab, solidot_grab, chouti_grab, TCEN_grab, TCCN_grab, sina_grab, engadgetcn_grab, engadgeten_grab, applenewsroom_grab ] for func in funcs: ret = func() # 执行抓取 if ret is None: continue news_list, source = ret for news in news_list: # 存储到数据库 if news['publish_time'] > old_time: News.create(news, source) Config.create(GLOBAL_SIGNAL, str(int(datetime.datetime.now().timestamp()))) # 更新上次抓取时间 return response()
class Weixin: APP_ID = Config.get_value_by_key('weixin-app-id', 'YOUR-APP-ID').body APP_SECRET = Config.get_value_by_key('weixin-app-secret', 'YOUR-APP-secret').body CODE2SESSION_URL = 'https://api.weixin.qq.com/sns/jscode2session' \ '?appid=' + APP_ID + \ '&secret=' + APP_SECRET + \ '&js_code=%s' \ '&grant_type=authorization_code' @staticmethod def code2session(code): url = Weixin.CODE2SESSION_URL % code data = requests.get(url).json() if 'openid' not in data: return Ret(Error.ERROR_JSCODE) return Ret(data) @classmethod def decrypt(cls, encrypted_data, iv, session_key): try: encrypted_data = base64.b64decode(encrypted_data) iv = base64.b64decode(iv) session_key = base64.b64decode(session_key) cipher = AES.new(session_key, AES.MODE_CBC, iv) decrypted = json.loads(cls._unpad(cipher.decrypt(encrypted_data)).decode()) if decrypted['watermark']['appid'] != cls.APP_ID: # raise Exception('Invalid Buffer') return Ret(Error.ERROR_APP_ID) except Exception as err: deprint(str(err)) return Ret(Error.DECRYPT_ERROR) return Ret(decrypted) @staticmethod def _unpad(s): return s[:-ord(s[len(s)-1:])]
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 get_config(url): jsapi_ticket = Config.get_config_by_key(CI.WEIXIN_JSAPI_TICKET) noncestr = get_random_string(length=16) timestamp = int(datetime.datetime.now().timestamp()) raw_string = 'jsapi_ticket=%s&noncestr=%s×tamp=%s&url=%s' % ( jsapi_ticket, noncestr, timestamp, url) signature = sha1(raw_string) return dict( noncestr=noncestr, signature=signature, timestamp=timestamp, appid=APP_ID, )
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)
def msg_sec_check(cls, content): access_token = Config.get_value_by_key(CI.QIX_ACCESS_TOKEN) try: data = json.dumps(dict(content=content), ensure_ascii=False) data = data.encode() resp = requests.post(cls.MSG_SEC_CHECK_URL % access_token, data=data) data = resp.json() if data['errcode'] == 87014: raise WeixinError.CONTENT_UNSAFE if data['errcode'] != 0: raise WeixinError.SAFE_CHECK_FAIL except E as e: raise e except Exception: raise WeixinError.SAFE_CHECK_FAIL
def post(r): tags = r.d.tags start = Config.get_config_by_key('start').body if tags == 'BACK': start.value = str(int(start.value) - 1) elif tags == 'DELETE': start.value = str(int(start.value) + 1) else: Phrase.create(**r.d.dict()) start.value = str(int(start.value) + 1) start.save() return 0
def post(request): phrase = request.d.phrase tags = request.d.tags tags = str(tags) start = Config.get_config_by_key('start').body if tags == 'BACK': start.value = str(int(start.value) - 1) elif tags == 'DELETE': start.value = str(int(start.value) + 1) else: ret = Phrase.create(phrase, tags) if ret.error is not Error.OK: return error_response(ret) start.value = str(int(start.value) + 1) start.save() return response()
def get_daily_music_list(cls, end_date=None, count=10): if count > 10 or count <= 0: count = 10 crt_date = datetime.datetime.now().date() print(crt_date, end_date) if end_date: end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d').date() if not end_date or end_date > crt_date: end_date = crt_date first_date_str = Config.get_value_by_key('first-date', '2018-10-17').body first_date = datetime.datetime.strptime(first_date_str, '%Y-%m-%d').date() daily_music_list = [] is_over = False for _ in range(count): if end_date < first_date: break ret = cls.get_dr_by_date(end_date) if ret.error is Error.OK: o_dr = ret.body daily_music_list.append(o_dr.to_dict()) end_date -= datetime.timedelta(days=1) if end_date < first_date: is_over = True return Ret( dict( daily_music_list=daily_music_list, next_date=end_date.strftime('%Y-%m-%d'), is_over=is_over, ))
import requests from Config.models import Config, CI G_RECAPTCHA_SECRET = Config.get_value_by_key(CI.G_RECAPTCHA_SECRET) class Recaptcha: API_URL = 'https://recaptcha.net/recaptcha/api/siteverify' @staticmethod def verify(response): try: resp = requests.post(Recaptcha.API_URL, { 'secret': G_RECAPTCHA_SECRET, 'response': response, }) success = resp.json()['success'] except Exception: return False return success
import datetime import requests from SmartDjango import E from django.utils.crypto import get_random_string from Base.common import sha1 from Config.models import Config, CI APP_ID = Config.get_value_by_key(CI.WEIXIN_APP_ID) APP_SECRET = Config.get_value_by_key(CI.WEIXIN_APP_SECRET) @E.register() class WeixinError: UPDATE_WEIXIN_ACCESS_TOKEN_ERROR = E("更新微信Access Token错误") UPDATE_WEIXIN_JSAPI_TICKET_ERROR = E("更新微信JsApi Ticket错误") UPDATE_WEIXIN_TIME_NOT_EXPIRED = E("更新间隔太短") class Weixin: @staticmethod 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))
"""171203 Adel Liu 即将使用web前端直接上传到七牛 而无需通过服务器 减小服务器压力 """ import qiniu import requests from SmartDjango import E from django.http import HttpRequest from qiniu import urlsafe_base64_encode from Base.common import HOST from Config.models import Config, CI ACCESS_KEY = Config.get_value_by_key(CI.QINIU_ACCESS_KEY) SECRET_KEY = Config.get_value_by_key(CI.QINIU_SECRET_KEY) RES_BUCKET = Config.get_value_by_key(CI.RES_BUCKET) PUBLIC_BUCKET = Config.get_value_by_key(CI.PUBLIC_BUCKET) RES_CDN_HOST = Config.get_value_by_key(CI.RES_CDN_HOST) PUBLIC_CDN_HOST = Config.get_value_by_key(CI.PUBLIC_CDN_HOST) qn_auth = qiniu.Auth(access_key=ACCESS_KEY, secret_key=SECRET_KEY) host = HOST key_prefix = 'disk/' QINIU_MANAGE_HOST = "https://rs.qiniu.com" @E.register(id_processor=E.idp_cls_prefix()) class QNError: REQUEST_QINIU = E("七牛请求错误", hc=500) QINIU_UNAUTHORIZED = E("七牛端身份验证错误", hc=403)
"""171203 Adel Liu 即将使用web前端直接上传到七牛 而无需通过服务器 减小服务器压力 """ import qiniu import requests from django.http import HttpRequest from qiniu import urlsafe_base64_encode from Base.common import deprint from Base.error import Error from Base.response import Ret from Config.models import Config # from engif.settings import HOST ACCESS_KEY = Config.get_value_by_key('qiniu-access-key', 'YOUR-ACCESS-KEY').body SECRET_KEY = Config.get_value_by_key('qiniu-secret-key', 'YOUR-SECRET-KEY').body PUBLIC_BUCKET = Config.get_value_by_key('qiniu-public-bucket', 'YOUR-PUBLIC-BUCKET').body _AUTH = qiniu.Auth(access_key=ACCESS_KEY, secret_key=SECRET_KEY) # _HOST = HOST _KEY_PREFIX = 'engif/' QINIU_MANAGE_HOST = "https://rs.qiniu.com" PUBLIC_CDN_HOST = 'https://image.6-79.cn' class QN: def __init__(self, auth, bucket, cdn_host, public):
import json from urllib.parse import urlencode import requests from django.utils.crypto import get_random_string from Base.session import Session from Config.models import Config, CI yunpian_appkey = Config.get_value_by_key(CI.YUNPIAN_APPKEY) class SendMobile: REGISTER = 0 FIND_PWD = 1 LOGIN = 2 CHINA = 0 ABROAD = 1 texts = [ [ '【六七九】本次注册的验证码为#code#,五分钟内有效。', '【六七九】本次密码找回的验证码为#code#,五分钟内有效。', '【六七九】本次登录的验证码为#code#,五分钟内有效。', ], [ '【Six79】Code for registering is #code#, valid within 5 minutes.', '【Six79】Code for retrieving password is #code#, valid within 5 minutes.', '【Six79】Code for logging in is #code#, valid within 5 minutes.', ] ]
""" 190815 Adel Liu """ from QitianSDK import QitianManager from Config.models import Config, CI DEBUG = True QITIAN_APP_ID = Config.get_value_by_key(CI.QITIAN_APP_ID) QITIAN_APP_SECRET = Config.get_value_by_key(CI.QITIAN_APP_SECRET) SECRET_KEY = Config.get_value_by_key(CI.PROJECT_SECRET_KEY) JWT_ENCODE_ALGO = Config.get_value_by_key(CI.JWT_ENCODE_ALGO) HOST = Config.get_value_by_key(CI.HOST) ADMIN_QITIAN = Config.get_value_by_key(CI.ADMIN_QITIAN) MAX_IMAGE_SIZE = int(Config.get_value_by_key(CI.MAX_IMAGE_SIZE)) MAX_FILE_SIZE = int(Config.get_value_by_key(CI.MAX_FILE_SIZE)) qt_manager = QitianManager(QITIAN_APP_ID, QITIAN_APP_SECRET, timeout=5)
from QitianSDK import QitianManager from SmartDjango.models import Pager from Config.models import Config, CI current_tz = datetime.timezone(datetime.timedelta(hours=8)) def get_time(timestamp=None): if timestamp is None: return datetime.datetime.now(tz=current_tz) return datetime.datetime.fromtimestamp(float(timestamp), tz=current_tz) time_pager = Pager(compare_field='time') def time_dictor(time): if isinstance(time, datetime.datetime): return time.timestamp() QITIAN_APP_ID = Config.get_value_by_key(CI.QITIAN_APP_ID) QITIAN_APP_SECRET = Config.get_value_by_key(CI.QITIAN_APP_SECRET) SECRET_KEY = Config.get_value_by_key(CI.PROJECT_SECRET_KEY) JWT_ENCODE_ALGO = Config.get_value_by_key(CI.JWT_ENCODE_ALGO) qt_manager = QitianManager(QITIAN_APP_ID, QITIAN_APP_SECRET)
from SmartDjango import NetPacker from Config.models import Config, CI from utils.spider.economist import EconomistSpider DEV_MODE = True NetPacker.set_mode(debug=DEV_MODE) # NetPacker.customize_http_code(fixed_http_code=200) SECRET_KEY = Config.get_value_by_key(CI.PROJECT_SECRET_KEY) JWT_ENCODE_ALGO = Config.get_value_by_key(CI.JWT_ENCODE_ALGO) SPIDER = EconomistSpider()
def get(request): start = int(Config.get_config_by_key('start').body.value) phrase = phrases[start] return response(phrase)
import requests from Base.common import deprint from Base.error import Error from Base.response import Ret from Config.models import Config QTB_HOST = 'https://ssoapi.6-79.cn' QTB_APP_ID = Config.get_value_by_key('qtb-app-id', 'DEFAULT-QTB-APP-ID').body QTB_APP_SECRET = Config.get_value_by_key('qtb-app-secret', 'DEFAULT-QTB-APP-SECRET').body def get_qtb_user_token(code): get_user_token_uri = '%s/api/oauth/token' % QTB_HOST req = requests.post(get_user_token_uri, json=dict( code=code, app_secret=QTB_APP_SECRET, ), timeout=3) if req.status_code != requests.codes.ok: return Ret(Error.QTB_AUTH_FAIL) try: res = req.json() except Exception as err: deprint(str(err)) return Ret(Error.QTB_AUTH_FAIL) if res['code'] != Error.OK.eid:
"""171203 Adel Liu 即将使用web前端直接上传到七牛 而无需通过服务器 减小服务器压力 """ import qiniu import requests from django.http import HttpRequest from qiniu import urlsafe_base64_encode, put_data, put_file, BucketManager from Config.models import Config, CI from SmartDjango import BaseError, ErrorCenter, Packing, E, Param ACCESS_KEY = Config.get_value_by_key(CI.QINIU_ACCESS_KEY, 'YOUR-ACCESS-KEY') SECRET_KEY = Config.get_value_by_key(CI.QINIU_SECRET_KEY, 'YOUR-SECRET-KEY') PUBLIC_BUCKET = Config.get_value_by_key(CI.QINIU_PUBLIC_BUCKET, 'YOUR-PUBLIC-BUCKET') PUBLIC_HOST = Config.get_value_by_key(CI.QINIU_PUBLIC_HOST, 'YOUR-PUBLIC-HOST') _AUTH = qiniu.Auth(access_key=ACCESS_KEY, secret_key=SECRET_KEY) _KEY_PREFIX = '' QINIU_MANAGE_HOST = "https://rs.qiniu.com" class QNError(ErrorCenter): UPLOAD_FAIL = E("上传出错", hc=500) REQUEST_QINIU = E("七牛请求错误", hc=500) QINIU_UNAUTHORIZED = E("七牛端身份验证错误", hc=403) FAIL_QINIU = E("未知原因导致的七牛端操作错误", hc=500) UNAUTH_CALLBACK = E("未经授权的回调函数", hc=403)
import datetime from SmartDjango import E from qcloud_image import Client from qcloud_image import CIUrls from Config.models import Config, CI APP_ID = Config.get_value_by_key(CI.QCLOUD_APP_ID) SECRET_ID = Config.get_value_by_key(CI.QCLOUD_SECRET_ID) SECRET_KEY = Config.get_value_by_key(CI.QCLOUD_SECRET_KEY) BUCKET = 'BUCKET' client = Client(APP_ID, SECRET_ID, SECRET_KEY, BUCKET) client.use_http() client.set_timeout(30) @E.register(id_processor=E.idp_cls_prefix()) class IDCardError: IDCARD_DETECT_ERROR = E("身份证自动验证错误") REAL_VERIFIED = E("已实名认证") CARD_NOT_COMPLETE = E("身份证正反面照片没有完善") CARD_VALID_EXPIRED = E("身份证认证过期") AUTO_VERIFY_FAILED = E("自动实名认证失败,请尝试人工认证") VERIFYING = E("您已提交认证") class IDCard: @staticmethod def detect_front(link):
data = func(r, *args, **kwargs) except E: data = '' return HttpResponse( data, status=200, content_type="application/json; encoding=utf-8", ) return wrapper NetPacker.set_mode(debug=True) # NetPacker.customize_data_packer(data_packer) ADMIN_PHONE = Config.get_value_by_key(CI.ADMIN_PHONE) WX_TOKEN = Config.get_value_by_key(CI.WX_TOKEN) WX_AES_KEY = Config.get_value_by_key(CI.WX_AES_KEY) WX_APP_ID = Config.get_value_by_key(CI.WX_APP_ID) WX_APP_SECRET = Config.get_value_by_key(CI.WX_APP_SECRET) QIX_APP_ID = Config.get_value_by_key(CI.QIX_APP_ID) QIX_APP_SECRET = Config.get_value_by_key(CI.QIX_APP_SECRET) YP_KEY = Config.get_value_by_key(CI.YP_KEY) SECRET_KEY = Config.get_value_by_key(CI.SECRET_KEY) JWT_ENCODE_ALGO = Config.get_value_by_key(CI.JWT_ENCODE_ALGO) wechat_client = WeChatClient(appid=WX_APP_ID, secret=WX_APP_SECRET)
def get(_): start = int(Config.get_value_by_key('start')) phrase = worker.phrases[start] return phrase