tools_ql_create_tasks.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import httpx
  4. # 青龙面板的地址
  5. url = "https://ql.erhe.link"
  6. # 登录青龙面板
  7. def login():
  8. response = httpx.post(f"{url}/api/user/login", json={"username": "toor", "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 = httpx.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 = httpx.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 = httpx.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/AutoInfo/'
  64. base_path = os.path.join(project_path, 'base')
  65. to_email_tasks_path = os.path.join(project_path, 'to_email')
  66. to_gotify_tasks_path = os.path.join(project_path, 'to_gotify')
  67. manual_path = os.path.join(project_path, 'manual')
  68. tasks_template = [{
  69. 'base': [
  70. {
  71. "name": "每天开始自动创建日志",
  72. "command": "python3 {}/base_daily_logs_generate.py".format(base_path),
  73. "schedule": "0 0 * * *",
  74. "labels": ["base"]
  75. },
  76. {
  77. "name": "每天结束自动发送日志",
  78. "command": "python3 {}/base_daily_logs_send.py".format(base_path),
  79. "schedule": "58 23 * * *",
  80. "labels": ["base"]
  81. },
  82. {
  83. "name": "每天自动删除旧数据",
  84. "command": "python3 {}/base_timing_remove_data.py".format(base_path),
  85. "schedule": "1 0 * * *",
  86. "labels": ["base"]
  87. },
  88. {
  89. "name": "每天新闻汇总,发送到邮箱",
  90. "command": "python3 {}/base_news_data_collation.py".format(base_path),
  91. "schedule": "0 10 6,12,18 * * *",
  92. "labels": ["base"]
  93. }
  94. ],
  95. 'to-email': [
  96. {
  97. "name": "对比大乐透最新一期数据,匹配已购买号码,发送消息",
  98. "command": "python3 {}/dlt.py".format(to_email_tasks_path),
  99. "schedule": "30 22 * * 1,3,6",
  100. "labels": ["to-email"]
  101. },
  102. {
  103. "name": "链捕手快讯消息推送",
  104. "command": "python3 {}/chaincatcher.py".format(to_email_tasks_path),
  105. "schedule": "0 */2 * * *",
  106. "labels": ["to-email"]
  107. },
  108. {
  109. "name": "anyknew聚合新闻消息推送",
  110. "command": "python3 {}/anyknew.py".format(to_email_tasks_path),
  111. "schedule": "0 */3 * * *",
  112. "labels": ["to-email"]
  113. },
  114. {
  115. "name": "反斗限免消息推送",
  116. "command": "python3 {}/apprcn.py".format(to_email_tasks_path),
  117. "schedule": "0 */12 * * *",
  118. "labels": ["to-email"]
  119. },
  120. {
  121. "name": "chiphell消息推送",
  122. "command": "python3 {}/chiphell.py".format(to_email_tasks_path),
  123. "schedule": "0 */12 * * *",
  124. "labels": ["to-email"]
  125. },
  126. {
  127. "name": "hello-github消息推送",
  128. "command": "python3 {}/hello_github.py".format(to_email_tasks_path),
  129. "schedule": "0 */12 * * *",
  130. "labels": ["to-email"]
  131. }
  132. ],
  133. 'to-gotify': [
  134. {
  135. "name": "获取未来 7 天的天气预报",
  136. "command": "python3 {}/weather7day.py".format(to_gotify_tasks_path),
  137. "schedule": "0 0 6,22 * * *",
  138. "labels": ["to-gotify"]
  139. },
  140. {
  141. "name": "获取coin实时数据",
  142. "command": "python3 {}/coin_detail.py".format(to_gotify_tasks_path),
  143. "schedule": "0 * * * *",
  144. "labels": ["to-gotify"]
  145. },
  146. {
  147. "name": "空投任务消息",
  148. "command": "python3 {}/airdrop_tasks.py".format(to_gotify_tasks_path),
  149. "schedule": "0 8,20 * * *",
  150. "labels": ["to-gotify"]
  151. },
  152. {
  153. "name": "币界网消息推送",
  154. "command": "python3 {}/coin_world.py".format(to_gotify_tasks_path),
  155. "schedule": "0 8,20 * * *",
  156. "labels": ["to-gotify"]
  157. },
  158. {
  159. "name": " web3新闻消息推送",
  160. "command": "python3 {}/web3_news.py".format(to_gotify_tasks_path),
  161. "schedule": "0 9,21 * * *",
  162. "labels": ["to-gotify"]
  163. }
  164. ],
  165. 'manual': [
  166. ]
  167. }]
  168. for task_template in tasks_template:
  169. for task_type, task_list in task_template.items():
  170. for task in task_list:
  171. task_name = task["name"]
  172. if task_name in tasks_names:
  173. print("Task {} already exists.".format(task_name))
  174. else:
  175. result = create_task(task, token)
  176. print("Task creation result:", result)
  177. # 创建所有任务之后, 创建视图分类
  178. # create_view_type(token)
  179. break # 正常执行完成后退出循环
  180. except Exception as e:
  181. print("An error occurred: ", e)
  182. print("Retrying...")
  183. if __name__ == "__main__":
  184. main()
  185. print('done!')