tools_ql_create_tasks.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import json
  4. import urllib.request
  5. import urllib.parse
  6. # 青龙面板的地址
  7. url = "https://ql.erhe.link"
  8. # 登录青龙面板
  9. def login():
  10. data = json.dumps({"username": "toor", "password": "!QAZ2wsx+0913"}).encode('utf-8')
  11. req = urllib.request.Request(
  12. f"{url}/api/user/login",
  13. data=data,
  14. headers={'Content-Type': 'application/json'}
  15. )
  16. try:
  17. with urllib.request.urlopen(req) as response:
  18. result = json.loads(response.read().decode('utf-8'))
  19. return result['data']['token']
  20. except urllib.error.HTTPError as e:
  21. print(f"Login failed with status code: {e.code}")
  22. print(e.read().decode('utf-8'))
  23. exit(0)
  24. # 获取任务列表
  25. def get_tasks(token):
  26. req = urllib.request.Request(
  27. f"{url}/api/crons",
  28. headers={"Authorization": f"Bearer {token}"}
  29. )
  30. with urllib.request.urlopen(req) as response:
  31. result = json.loads(response.read().decode('utf-8'))
  32. return result['data']['data']
  33. # 创建任务
  34. def create_task(task_template, token):
  35. payload = {
  36. "name": task_template["name"],
  37. "command": task_template["command"],
  38. "schedule": task_template["schedule"],
  39. "labels": task_template["labels"]
  40. }
  41. data = json.dumps(payload).encode('utf-8')
  42. req = urllib.request.Request(
  43. f"{url}/api/crons",
  44. data=data,
  45. headers={
  46. "Authorization": f"Bearer {token}",
  47. "Content-Type": "application/json"
  48. }
  49. )
  50. with urllib.request.urlopen(req) as response:
  51. return json.loads(response.read().decode('utf-8'))
  52. # 创建视图分类
  53. def create_view_type(token):
  54. view_type_list = ['base', 'spider_common']
  55. for view_type in view_type_list:
  56. payload = {
  57. "name": view_type,
  58. "filters": {
  59. 'property': 'labels',
  60. 'operation': 'Reg',
  61. 'value': view_type
  62. },
  63. 'filterRelation': 'and'
  64. }
  65. data = json.dumps(payload).encode('utf-8')
  66. req = urllib.request.Request(
  67. f"{url}/api/crons",
  68. data=data,
  69. headers={
  70. "Authorization": f"Bearer {token}",
  71. "Content-Type": "application/json"
  72. }
  73. )
  74. try:
  75. with urllib.request.urlopen(req) as response:
  76. result = json.loads(response.read().decode('utf-8'))
  77. print(f"View type {view_type} created: {result}")
  78. except urllib.error.HTTPError as e:
  79. print(f"Failed to create view type {view_type}: {e}")
  80. # 主逻辑
  81. def main():
  82. while True:
  83. try:
  84. token = login()
  85. print(f"已连接到 {url}")
  86. tasks = get_tasks(token)
  87. tasks_names = [task['name'] for task in tasks]
  88. if tasks:
  89. print("Current tasks name: \n{}, \ntotal: {}".format('\n'.join(tasks_names), str(len(tasks_names))))
  90. else:
  91. print("Tasks list is empty")
  92. project_path = '/ql/data/scripts/AutoInfo/'
  93. base_path = os.path.join(project_path, 'base')
  94. to_email_tasks_path = os.path.join(project_path, 'to_email')
  95. to_gotify_tasks_path = os.path.join(project_path, 'to_gotify')
  96. manual_path = os.path.join(project_path, 'manual')
  97. tasks_template = [{
  98. 'base': [
  99. {
  100. "name": "每天开始自动创建日志",
  101. "command": "python3 {}/base_daily_logs_generate.py".format(base_path),
  102. "schedule": "0 0 * * *",
  103. "labels": ["base"]
  104. },
  105. {
  106. "name": "每天结束自动发送日志",
  107. "command": "python3 {}/base_daily_logs_send.py".format(base_path),
  108. "schedule": "58 23 * * *",
  109. "labels": ["base"]
  110. },
  111. {
  112. "name": "每天自动删除旧数据",
  113. "command": "python3 {}/base_timing_remove_data.py".format(base_path),
  114. "schedule": "1 0 * * *",
  115. "labels": ["base"]
  116. },
  117. {
  118. "name": "每天新闻汇总,发送到邮箱",
  119. "command": "python3 {}/base_news_data_collation.py".format(base_path),
  120. "schedule": "0 10 6,12,18 * * *",
  121. "labels": ["base"]
  122. }
  123. ],
  124. 'to-email': [
  125. {
  126. "name": "对比大乐透最新一期数据,匹配已购买号码,发送消息",
  127. "command": "python3 {}/dlt.py".format(to_email_tasks_path),
  128. "schedule": "30 22 * * 1,3,6",
  129. "labels": ["to-email"]
  130. },
  131. {
  132. "name": "链捕手快讯消息推送",
  133. "command": "python3 {}/chaincatcher.py".format(to_email_tasks_path),
  134. "schedule": "0 */2 * * *",
  135. "labels": ["to-email"]
  136. },
  137. {
  138. "name": "anyknew聚合新闻消息推送",
  139. "command": "python3 {}/anyknew.py".format(to_email_tasks_path),
  140. "schedule": "0 */3 * * *",
  141. "labels": ["to-email"]
  142. },
  143. {
  144. "name": "反斗限免消息推送",
  145. "command": "python3 {}/apprcn.py".format(to_email_tasks_path),
  146. "schedule": "0 */12 * * *",
  147. "labels": ["to-email"]
  148. },
  149. {
  150. "name": "chiphell消息推送",
  151. "command": "python3 {}/chiphell.py".format(to_email_tasks_path),
  152. "schedule": "0 */12 * * *",
  153. "labels": ["to-email"]
  154. },
  155. {
  156. "name": "hello-github消息推送",
  157. "command": "python3 {}/hello_github.py".format(to_email_tasks_path),
  158. "schedule": "0 */12 * * *",
  159. "labels": ["to-email"]
  160. }
  161. ],
  162. 'to-gotify': [
  163. {
  164. "name": "获取未来 7 天的天气预报",
  165. "command": "python3 {}/weather7day.py".format(to_gotify_tasks_path),
  166. "schedule": "0 0 6,22 * * *",
  167. "labels": ["to-gotify"]
  168. },
  169. {
  170. "name": "获取coin实时数据",
  171. "command": "python3 {}/coin_detail.py".format(to_gotify_tasks_path),
  172. "schedule": "0 * * * *",
  173. "labels": ["to-gotify"]
  174. },
  175. {
  176. "name": "空投任务消息",
  177. "command": "python3 {}/airdrop_tasks.py".format(to_gotify_tasks_path),
  178. "schedule": "0 8,20 * * *",
  179. "labels": ["to-gotify"]
  180. },
  181. {
  182. "name": "币界网消息推送",
  183. "command": "python3 {}/coin_world.py".format(to_gotify_tasks_path),
  184. "schedule": "0 8,20 * * *",
  185. "labels": ["to-gotify"]
  186. },
  187. {
  188. "name": " web3新闻消息推送",
  189. "command": "python3 {}/web3_news.py".format(to_gotify_tasks_path),
  190. "schedule": "0 9,21 * * *",
  191. "labels": ["to-gotify"]
  192. }
  193. ],
  194. 'manual': [
  195. ]
  196. }]
  197. for task_template in tasks_template:
  198. for task_type, task_list in task_template.items():
  199. for task in task_list:
  200. task_name = task["name"]
  201. if task_name in tasks_names:
  202. print("Task {} already exists.".format(task_name))
  203. else:
  204. result = create_task(task, token)
  205. print("Task creation result:", result)
  206. # 创建所有任务之后, 创建视图分类
  207. # create_view_type(token)
  208. break # 正常执行完成后退出循环
  209. except Exception as e:
  210. print("An error occurred: ", e)
  211. print("Retrying...")
  212. if __name__ == "__main__":
  213. main()
  214. print('done!')