wiseflow/client/backend/background_task.py
2024-04-17 18:43:03 +08:00

39 lines
1001 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
通过编辑这个脚本,可以自定义需要的后台任务
"""
import schedule
import time
from work_process import ServiceProcesser
from pb_api import pb
sp = ServiceProcesser()
counter = 0
# 每小时唤醒一次如果pb的sites表中有信源会挑取符合周期的信源执行没有没有的话则每24小时执行专有爬虫一次
def task():
global counter
sites = pb.read('sites', filter='activated=True')
urls = []
for site in sites:
if not site['per_hours'] or not site['url']:
continue
if counter % site['per_hours'] == 0:
urls.append(site['url'])
print(f'\033[0;32m task execute loop {counter}\033[0m')
print(urls)
if urls:
sp(sites=urls)
else:
if counter % 24 == 0:
sp()
else:
print('\033[0;33mno work for this loop\033[0m')
counter += 1
schedule.every().hour.at(":38").do(task)
while True:
schedule.run_pending()
time.sleep(60)