-
Notifications
You must be signed in to change notification settings - Fork 1
/
wechat.py
71 lines (60 loc) · 2.76 KB
/
wechat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import requests
import sys
import json
import os
import shutil
def post_wx():
try:
get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CORPID}&corpsecret={CORPSECRET}"
response = requests.get(get_token_url).content
access_token = json.loads(response).get('access_token')
if access_token and len(access_token) > 0:
send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
data = {
"touser": '@all',
"agentid": AGENTID,
"msgtype": "textcard",
"textcard": {
"title": repository.split('/')[1] + " updated! 🚀",
"description": "🎉 Github Workflow Notification. 🍏 This job's status is " + status,
"url": "https://github.com/" + repository,
"btntxt": "More"
},
"enable_id_trans": 0,
"enable_duplicate_check": 0,
"duplicate_check_interval": 1800
}
requests.post(send_msg_url, data=json.dumps(data))
else:
print('error: Failed to post wechat notification. access_token is invalid. status: 500')
except Exception as e:
print('error: Failed to post wechat notification.' + str(e) + ' status: 500')
def copy_files_to_parent_directory():
current_directory = os.getcwd()
parent_directory = os.path.dirname(current_directory)
# Specify a list of folders not to be copied
directories_to_exclude = ['doc', '.github', '.git', '.idea']
items_to_exclude = ['.gitignore', 'requirements.txt', 'wechat.py', 'README.md']
for item in os.listdir(current_directory):
item_path = os.path.join(current_directory, item)
destination_path = os.path.join(parent_directory, item)
if item not in directories_to_exclude and item not in items_to_exclude:
if os.path.isfile(item_path):
# copy files
shutil.copy2(item_path, destination_path)
print(f"Copying file {item} to {parent_directory} succeeded!")
elif os.path.isdir(item_path):
# Copy a folder and its contents
shutil.copytree(item_path, destination_path)
print(f"Copy folder {item} to {parent_directory} SUCCESSFUL!")
if __name__ == '__main__':
try:
repository = sys.argv[1]
status = sys.argv[2]
AGENTID = sys.argv[3] # application id
CORPID = sys.argv[4] # enterprise id
CORPSECRET = sys.argv[5] # application secret
copy_files_to_parent_directory()
post_wx()
except IndexError:
print('Please specify five params: repository, job_status, AGENTID, CORPID, CORPSECRET')