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 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()