123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # 1、定时获取当天的git日志。
- # 2、定时发送到群里确认。
- # 3、定时调用日报接口发布日志。
- import git
- import requests
- import datetime
- import schedule
- import time
- from datetime import datetime, timezone
- import re
- import requests
- # 发送请求
- url = "https://doc.weixin.qq.com/formcol/answer_page"
- headers = {
- 'Host': 'doc.weixin.qq.com',
- 'Accept': 'application/json, text/plain, */*',
- 'Sec-Fetch-Site': 'same-origin',
- # 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryVfP45gTdNBENAZ70',
- 'Accept-Language': 'zh-CN,zh-Hans;q=0.9',
- 'Sec-Fetch-Mode': 'cors',
- 'Accept-Encoding': 'gzip, deflate, br',
- 'Origin': 'https://doc.weixin.qq.com',
- 'Content-Length': '1890',
- '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',
- 'Connection': 'close',
- 'Sec-Fetch-Dest': 'empty',
- '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'
- }
- def post_form_data(url, headers, text_reply_var, plain_text_reply_var, cookie):
- # Update headers with the provided Cookie
- headers['Cookie'] = cookie
- multipart_data = {
- 'form_id': ('', 'AIgA0QcMAAgAI0A6AaoAOUFH2ayj3FSzj_base'),
- 'form_reply': ('', f'{{"items":[{{"question_id":"1","rich_text_reply":{{"text_reply":"{text_reply_var}","plain_text_reply":"{plain_text_reply_var}"}}}}]}}'),
- 'type': ('', '8'),
- 'check_setting': ('', '{"can_anonymous":2}'),
- 'use_anonymous': ('', 'false'),
- 'submit_again': ('', 'true'),
- '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"}}}'),
- 'isSendToRoom': ('', 'false'),
- 'f': ('', 'json'),
- }
- response = requests.post(url, headers=headers, files=multipart_data)
- return response
- # 操作git
- def extract_message_content(message):
- # 使用正则表达式匹配 ] 或 】 后的字符串
- match = re.search(r'[\]】](.+)', message)
- if match:
- return match.group(1).strip()
- return message
- def pull_latest_changes(repo):
- # 拉取最新代码
- repo.remotes.origin.pull()
- # 获取今天我的提交
- def get_last_commits_today(repo_path):
- repo = git.Repo(repo_path)
- # 拉取最新代码
- pull_latest_changes(repo)
- # 获取今天的日期
- today = datetime.now(timezone.utc).replace(
- hour=0, minute=0, second=0, microsecond=0)
- commits_today = []
- for commit in repo.iter_commits('gy_3.39', max_count=100):
- # 仅处理作者是 'gongyuan',且提交消息包含中文
- 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):
- # 判断提交的日期是否是今天
- if commit.authored_datetime >= today:
- commits_today.append(commit)
- return commits_today
- # 返回今天需要发布日报的文本
- def get_today_commit_text(commits_today):
- for commit in commits_today:
- print(f"Commit: {commit.hexsha}")
- print(f"Author: {commit.author.name} <{commit.author.email}>")
- print(f"Date: {commit.authored_datetime}")
- print(f"Message: {commit.message}")
- print()
- # 提取每个 commit 对象的 message 内容中 ] 或 】 后的字符串
- extracted_messages = [extract_message_content(
- commit.message) for commit in commits_today]
- # 使用空格连接数组中的字符串
- logpaper_text = ''.join(extracted_messages)
- # 打印拼接后的字符串
- print('提交记录='+logpaper_text)
- return logpaper_text
- # 处理为月报需要的文本并保存
- def save_month_text(commits_today):
- # 根据标识分为优化功能、新增需求、修改bug,然后存起来
- print('根据标识分为优化功能、新增需求、修改bug,然后存起来')
- # 通过接口发送消息到企微群
- def send_qwgroup_message(content):
- webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=85c23f32-4df1-4e4d-af51-d952d927a3fb"
- data = {
- "msgtype": "text",
- "text": {
- "content": content
- }
- }
- response = requests.post(webhook_url, json=data)
- print(response.status_code, response.text)
- # 通过接口发送消息到企微日志平台
- def send_qwlogpaperplatform(text_reply_var, plain_text_reply_var):
- 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'
- response = post_form_data(
- url, headers, text_reply_var, plain_text_reply_var, cookie)
- print(response.status_code)
- print(response.text)
- # 发送日报到企微群里,以及发布日报到企微日志平台上
- def send_message_to_qwgroup_and_qwlogpaperplatform(logpaper_text):
- if logpaper_text:
- # 发送给企微群
- qw_text = '10分钟后将自动发布以下内容!:\n'+logpaper_text
- send_qwgroup_message(qw_text)
- # 发布日报到企微日志平台上(延时执行)
- text_reply_var = logpaper_text
- plain_text_reply_var = logpaper_text
- send_qwlogpaperplatform(text_reply_var, plain_text_reply_var)
- else: # git没提交,且为工作日 发个提醒到企微群(节假日上班😄)
- today = datetime.now().date()
- if is_weekday(today):
- # qw_text = '没找到今天的git提交记录,记得手动填写发布哦!!!'
- # send_message(qw_text)
- timeStr = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
- qw_text = '没找到今天的git提交记录,随便发测试!!!' + timeStr
- send_message_to_qwgroup_and_qwlogpaperplatform(qw_text)
- else:
- print("今天是周末")
- def is_weekday(date):
- # Monday is 0 and Sunday is 6
- return 0 <= date.weekday() < 10
- # 定时执行任务
- def job():
- # 得到当天我的提交记录
- commits_today = get_last_commits_today(
- '/Users/gy/Downloads/file/softisland/IGXEAssistant2')
- # 以月报形式保存
- save_month_text(commits_today)
- # 获取日报形式的文本
- logpaper_text = get_today_commit_text(commits_today)
- send_message_to_qwgroup_and_qwlogpaperplatform(logpaper_text)
- if __name__ == "__main__":
- # 定义定时任务,每天下午6点执行 job 函数
- # schedule.every().day.at("18:20").do(job)
- # 测试
- job()
- # 保持脚本运行
- while True:
- schedule.run_pending()
- time.sleep(1)
|