utils_ql_create_tasks.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import requests
  4. # 青龙面板的地址
  5. url = "https://ql-message.erhe.top"
  6. # 登录青龙面板
  7. def login():
  8. response = requests.post(f"{url}/api/user/login", json={"username": "root", "password": "!QAZ2wsx+0913"})
  9. print(response)
  10. if response.status_code != 200:
  11. print(response.status_code)
  12. print(response.text)
  13. exit(0)
  14. return response.json()['data']['token']
  15. # 获取任务列表
  16. def get_tasks(token):
  17. response = requests.get(f"{url}/api/crons", headers={"Authorization": f"Bearer {token}"})
  18. return response.json()['data']['data']
  19. # 创建任务
  20. def create_task(task_template, token):
  21. payload = {
  22. "name": task_template["name"],
  23. "command": task_template["command"],
  24. "schedule": task_template["schedule"],
  25. "labels": task_template["labels"]
  26. }
  27. headers = {
  28. "Authorization": f"Bearer {token}",
  29. "Content-Type": "application/json"
  30. }
  31. response = requests.post(f"{url}/api/crons", headers=headers, json=payload)
  32. return response.json()
  33. # 创建视图分类
  34. def create_view_type(token):
  35. view_type_list = ['base', 'spider_common']
  36. for view_type in view_type_list:
  37. payload = {
  38. "name": view_type,
  39. "filters": {
  40. 'property': 'labels',
  41. 'operation': 'Reg',
  42. 'value': view_type
  43. },
  44. 'filterRelation': 'and'
  45. }
  46. headers = {
  47. "Authorization": f"Bearer {token}",
  48. "Content-Type": "application/json"
  49. }
  50. response = requests.post(f"{url}/api/crons", headers=headers, json=payload)
  51. # 主逻辑
  52. def main():
  53. while True:
  54. try:
  55. token = login()
  56. print(f"已连接到 {url}")
  57. tasks = get_tasks(token)
  58. tasks_names = [task['name'] for task in tasks]
  59. if tasks:
  60. print("Current tasks name: \n{}, \ntotal: {}".format('\n'.join(tasks_names), str(len(tasks_names))))
  61. else:
  62. print("Tasks list is empty")
  63. project_path = '/ql/data/scripts/auto/'
  64. spider_common_path = os.path.join(project_path, 'spider_common')
  65. spider_news_path = os.path.join(project_path, 'spider_news')
  66. spider_rsshub_path = os.path.join(project_path, 'spider_rsshub')
  67. utils_path = os.path.join(project_path, 'utils')
  68. message_path = os.path.join(project_path, 'message')
  69. rss_data_path = os.path.join(project_path, 'rss_data_handel')
  70. tasks_template = [{
  71. 'base_tasks': [
  72. {
  73. "name": "每天开始自动创建日志",
  74. "command": "python3 {}/base_daily_logs_generate.py".format(utils_path),
  75. "schedule": "0 0 * * *",
  76. "labels": ["base"]
  77. },
  78. {
  79. "name": "每天结束自动发送日志",
  80. "command": "python3 {}/base_daily_logs_send.py".format(utils_path),
  81. "schedule": "58 23 * * *",
  82. "labels": ["base"]
  83. },
  84. {
  85. "name": "每天自动删除旧数据",
  86. "command": "python3 {}/base_timing_remove_data.py".format(utils_path),
  87. "schedule": "1 0 * * *",
  88. "labels": ["base"]
  89. },
  90. {
  91. "name": "每天新闻汇总,发送到邮箱",
  92. "command": "python3 {}/utils_news_data_collation.py".format(utils_path),
  93. "schedule": "0 10 6,12,18 * * *",
  94. "labels": ["base"]
  95. }
  96. ],
  97. 'spider_common_tasks': [
  98. {
  99. "name": "自动执行dlt爬虫",
  100. "command": "python3 {}/spider_get_and_check_dlt.py".format(spider_common_path),
  101. "schedule": "30 22 * * 1,3,6",
  102. "labels": ["spider_common"]
  103. }
  104. ],
  105. 'spider_news': [
  106. {
  107. "name": "自动执行反斗限免爬虫",
  108. "command": "python3 {}/news_get_apprcn.py".format(spider_news_path),
  109. "schedule": "0 0 3,6,9,12,15,18,21 * * *",
  110. "labels": ["spider-common"]
  111. },
  112. {
  113. "name": "自动执行chiphell爬虫",
  114. "command": "python3 {}/news_get_chiphell.py".format(spider_news_path),
  115. "schedule": "0 0 3,6,9,12,15,18,21 * * *",
  116. "labels": ["spider-common"]
  117. },
  118. {
  119. "name": "自动执行hello_github爬虫",
  120. "command": "python3 {}/news_get_hello_github.py".format(spider_news_path),
  121. "schedule": "0 0 3,6,9,12,15,18,21 * * *",
  122. "labels": ["spider-common"]
  123. },
  124. {
  125. "name": "自动执行Anyknew爬虫",
  126. "command": "python3 {}/news_get_news.py".format(spider_news_path),
  127. "schedule": "0 0 3,6,9,12,15,18,21 * * *",
  128. "labels": ["spider-common"]
  129. },
  130. {
  131. "name": "自动执行币界网文章爬虫",
  132. "command": "python3 {}/spider_web3_coin_world.py".format(spider_news_path),
  133. "schedule": "0 0 3,6,9,12,15,18,21 * * *",
  134. "labels": ["spider-common"]
  135. },
  136. {
  137. "name": "获取 web3 新闻",
  138. "command": "python3 {}/spider_web3_news.py".format(spider_news_path),
  139. "schedule": "0 0 3,6,9,12,15,18,21 * * *",
  140. "labels": ["spider-common"]
  141. },
  142. ],
  143. 'spider_rsshub_tasks': [
  144. {
  145. "name": "自动执行 rsshub 爬虫",
  146. "command": "python3 {}/rsshub_spider.py".format(spider_rsshub_path),
  147. "schedule": "0 0 3,6,9,12,15,18,21 * * *",
  148. "labels": ["spider-rsshub"]
  149. }
  150. ],
  151. 'message_tasks': [
  152. {
  153. "name": "获取 coinmarketcap 数字货币实时价格",
  154. "command": "python3 {}/message_coinmarketcap.py".format(message_path),
  155. "schedule": "*/30 * * * *",
  156. "labels": ["message"]
  157. },
  158. {
  159. "name": "获取实时 VIX 恐慌指数",
  160. "command": "python3 {}/message_vix.py".format(message_path),
  161. "schedule": "*/30 * * * *",
  162. "labels": ["message"]
  163. },
  164. {
  165. "name": "对比大乐透最新一期数据,匹配已购买号码,发送消息",
  166. "command": "python3 {}/message_dlt.py".format(message_path),
  167. "schedule": "30 22 * * 1,3,6",
  168. "labels": ["message"]
  169. },
  170. {
  171. "name": "获取未来 7 天的天气预报",
  172. "command": "python3 {}/message_get_one_week_weather.py".format(message_path),
  173. "schedule": "0 0 6,22 * * *",
  174. "labels": ["message"]
  175. }
  176. ],
  177. 'rss_data': [
  178. {
  179. "name": "从 freshrss-psql 数据库中读取数据并发送",
  180. "command": "python3 {}/rss_data_handel_main.py".format(rss_data_path),
  181. "schedule": "30 6,9,12,18,22 * * *",
  182. "labels": ["message"]
  183. },
  184. ]
  185. }]
  186. for task_template in tasks_template:
  187. for task_type, task_list in task_template.items():
  188. for task in task_list:
  189. task_name = task["name"]
  190. if task_name in tasks_names:
  191. print("Task {} already exists.".format(task_name))
  192. else:
  193. result = create_task(task, token)
  194. print("Task creation result:", result)
  195. # 创建所有任务之后, 创建视图分类
  196. # create_view_type(token)
  197. break # 正常执行完成后退出循环
  198. except Exception as e:
  199. print("An error occurred: ", e)
  200. print("Retrying...")
  201. if __name__ == "__main__":
  202. main()
  203. print('done!')