robot_qw_logpaper.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # 1、定时获取当天的git日志。
  4. # 2、定时发送到群里确认。
  5. # 3、定时调用日报接口发布日志。
  6. import git
  7. import requests
  8. import datetime
  9. import schedule
  10. import time
  11. from datetime import datetime, timezone
  12. import re
  13. import requests
  14. # 发送请求
  15. url = "https://doc.weixin.qq.com/formcol/answer_page"
  16. headers = {
  17. 'Host': 'doc.weixin.qq.com',
  18. 'Accept': 'application/json, text/plain, */*',
  19. 'Sec-Fetch-Site': 'same-origin',
  20. # 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryVfP45gTdNBENAZ70',
  21. 'Accept-Language': 'zh-CN,zh-Hans;q=0.9',
  22. 'Sec-Fetch-Mode': 'cors',
  23. 'Accept-Encoding': 'gzip, deflate, br',
  24. 'Origin': 'https://doc.weixin.qq.com',
  25. 'Content-Length': '1890',
  26. 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_2_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 wxwork/4.1.3 MicroMessenger/7.0.1 Language/zh ColorScheme/Light',
  27. 'Connection': 'close',
  28. 'Sec-Fetch-Dest': 'empty',
  29. 'Cookie': 'fingerprint=ca6fb466a7454afa8ed96d103adabd5a35; hashkey=f1dd9a02; traceid=f1dd9a022a; TOK=4111dcd01a3c4f20; tdoc_uid=13102702184490373; uid=; uid_key=; wedoc_openid=wozbKqDgAAMAA3rCOMGE0RRvFxRz4SaQ; wedoc_sid=1oVWVozlRGQuTVJ2AJVVQwAA; wedoc_sids=13102702184490373&1oVWVozlRGQuTVJ2AJVVQwAA; wedoc_skey=13102702184490373&4016e8cf1ea3cf8039cbc8cd5f56f72f; wedoc_ticket=13102702184490373&CAESILpv6XseP1IZarCVrYqGK-psJXWRKE0v7f9qC-RFxH32; optimal_cdn_domain=rescdn.qqmail.com'
  30. }
  31. def post_form_data(url, headers, text_reply_var, plain_text_reply_var, cookie):
  32. # Update headers with the provided Cookie
  33. headers['Cookie'] = cookie
  34. multipart_data = {
  35. 'form_id': ('', 'AIgA0QcMAAgAI0A6AaoAOUFH2ayj3FSzj_base'),
  36. 'form_reply': ('', f'{{"items":[{{"question_id":"1","rich_text_reply":{{"text_reply":"{text_reply_var}","plain_text_reply":"{plain_text_reply_var}"}}}}]}}'),
  37. 'type': ('', '8'),
  38. 'check_setting': ('', '{"can_anonymous":2}'),
  39. 'use_anonymous': ('', 'false'),
  40. 'submit_again': ('', 'true'),
  41. 'wwjournal_data': ('', '{"entry":{"mngreporter":[{"vid":"1688853713315981"},{"vid":"1688853713316022"},{"vid":"1688853713316001"},{"vid":"1688853713316000"},{"vid":"1688856046406642"}],"reporter":[],"templateid":"ZLqsG4jMZzTM7oJFatFcrDiTN25WS8BQaQrMuZ","doc_info":{"type":2,"form_id":"AIgA0QcMAAgAI0A6AaoAOUFH2ayj3FSzj_base","template_id":"ZLqsG4jMZzTM7oJFatFcrDiTN25WS8BQaQrMuZ"}}}'),
  42. 'isSendToRoom': ('', 'false'),
  43. 'f': ('', 'json'),
  44. }
  45. response = requests.post(url, headers=headers, files=multipart_data)
  46. return response
  47. # 操作git
  48. def extract_message_content(message):
  49. # 使用正则表达式匹配 ] 或 】 后的字符串
  50. match = re.search(r'[\]】](.+)', message)
  51. if match:
  52. return match.group(1).strip()
  53. return message
  54. def pull_latest_changes(repo):
  55. # 拉取最新代码
  56. repo.remotes.origin.pull()
  57. # 获取今天我的提交
  58. def get_last_commits_today(repo_path):
  59. repo = git.Repo(repo_path)
  60. # 拉取最新代码
  61. pull_latest_changes(repo)
  62. # 获取今天的日期
  63. today = datetime.now(timezone.utc).replace(
  64. hour=0, minute=0, second=0, microsecond=0)
  65. commits_today = []
  66. for commit in repo.iter_commits('gy_3.39', max_count=100):
  67. # 仅处理作者是 'gongyuan',且提交消息包含中文
  68. if commit.author.name == 'gongyuan' or commit.author.name == 'sszf7605' or commit.author.name == '龚圆1' and any(ord(c) > 127 for c in commit.message):
  69. # 判断提交的日期是否是今天
  70. if commit.authored_datetime >= today:
  71. commits_today.append(commit)
  72. return commits_today
  73. # 返回今天需要发布日报的文本
  74. def get_today_commit_text(commits_today):
  75. for commit in commits_today:
  76. print(f"Commit: {commit.hexsha}")
  77. print(f"Author: {commit.author.name} <{commit.author.email}>")
  78. print(f"Date: {commit.authored_datetime}")
  79. print(f"Message: {commit.message}")
  80. print()
  81. # 提取每个 commit 对象的 message 内容中 ] 或 】 后的字符串
  82. extracted_messages = [extract_message_content(
  83. commit.message) for commit in commits_today]
  84. # 使用空格连接数组中的字符串
  85. logpaper_text = ''.join(extracted_messages)
  86. # 打印拼接后的字符串
  87. print('提交记录='+logpaper_text)
  88. return logpaper_text
  89. # 处理为月报需要的文本并保存
  90. def save_month_text(commits_today):
  91. # 根据标识分为优化功能、新增需求、修改bug,然后存起来
  92. print('根据标识分为优化功能、新增需求、修改bug,然后存起来')
  93. # 通过接口发送消息到企微群
  94. def send_qwgroup_message(content):
  95. webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=85c23f32-4df1-4e4d-af51-d952d927a3fb"
  96. data = {
  97. "msgtype": "text",
  98. "text": {
  99. "content": content
  100. }
  101. }
  102. response = requests.post(webhook_url, json=data)
  103. print(response.status_code, response.text)
  104. # 通过接口发送消息到企微日志平台
  105. def send_qwlogpaperplatform(text_reply_var, plain_text_reply_var):
  106. cookie = 'fingerprint=ca6fb466a7454afa8ed96d103adabd5a35; hashkey=f1dd9a02; traceid=f1dd9a022a; TOK=4111dcd01a3c4f20; tdoc_uid=13102702184490373; uid=; uid_key=; wedoc_openid=wozbKqDgAAMAA3rCOMGE0RRvFxRz4SaQ; wedoc_sid=1oVWVozlRGQuTVJ2AJVVQwAA; wedoc_sids=13102702184490373&1oVWVozlRGQuTVJ2AJVVQwAA; wedoc_skey=13102702184490373&4016e8cf1ea3cf8039cbc8cd5f56f72f; wedoc_ticket=13102702184490373&CAESILpv6XseP1IZarCVrYqGK-psJXWRKE0v7f9qC-RFxH32; optimal_cdn_domain=rescdn.qqmail.com'
  107. response = post_form_data(
  108. url, headers, text_reply_var, plain_text_reply_var, cookie)
  109. print(response.status_code)
  110. print(response.text)
  111. # 发送日报到企微群里,以及发布日报到企微日志平台上
  112. def send_message_to_qwgroup_and_qwlogpaperplatform(logpaper_text):
  113. if logpaper_text:
  114. # 发送给企微群
  115. qw_text = '10分钟后将自动发布以下内容!:\n'+logpaper_text
  116. send_qwgroup_message(qw_text)
  117. # 发布日报到企微日志平台上(延时执行)
  118. text_reply_var = logpaper_text
  119. plain_text_reply_var = logpaper_text
  120. send_qwlogpaperplatform(text_reply_var, plain_text_reply_var)
  121. else: # git没提交,且为工作日 发个提醒到企微群(节假日上班😄)
  122. today = datetime.now().date()
  123. if is_weekday(today):
  124. # qw_text = '没找到今天的git提交记录,记得手动填写发布哦!!!'
  125. # send_message(qw_text)
  126. timeStr = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  127. qw_text = '没找到今天的git提交记录,随便发测试!!!' + timeStr
  128. send_message_to_qwgroup_and_qwlogpaperplatform(qw_text)
  129. else:
  130. print("今天是周末")
  131. def is_weekday(date):
  132. # Monday is 0 and Sunday is 6
  133. return 0 <= date.weekday() < 10
  134. # 定时执行任务
  135. def job():
  136. # 得到当天我的提交记录
  137. commits_today = get_last_commits_today(
  138. '/Users/gy/Downloads/file/softisland/IGXEAssistant2')
  139. # 以月报形式保存
  140. save_month_text(commits_today)
  141. # 获取日报形式的文本
  142. logpaper_text = get_today_commit_text(commits_today)
  143. send_message_to_qwgroup_and_qwlogpaperplatform(logpaper_text)
  144. if __name__ == "__main__":
  145. # 定义定时任务,每天下午6点执行 job 函数
  146. # schedule.every().day.at("18:20").do(job)
  147. # 测试
  148. job()
  149. # 保持脚本运行
  150. while True:
  151. schedule.run_pending()
  152. time.sleep(1)