This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
executable file
·265 lines (233 loc) · 8.77 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python3
"""Copyright (c) 2014 Li Zhenbo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE."""
#功能:自动生成每周的 IRC 会议记录邮件
#
#输入:结束会议时 bot 的提示
# 示例:
'''
<zodbot> Minutes: http://meetbot.fedoraproject.org/fedora-zh/2014-01-31/fedora-zh.2014-01-31-13.02.html
<zodbot> Minutes (text): http://meetbot.fedoraproject.org/fedora-zh/2014-01-31/fedora-zh.2014-01-31-13.02.txt
<zodbot> Log: http://meetbot.fedoraproject.org/fedora-zh/2014-01-31/fedora-zh.2014-01-31-13.02.log.html
'''
#
#输出:.eml 文件
#
#程序结构:
# 1. 匹配 (获取用户输入,得到链接)
# 2. 抓取
# 3. 输出 (按照 .eml 文件的格式)
#
# 注意:
# 1. 所有编码都是 UTF-8
# 2. 仅保证 python3 能运行该程序
#config
URL_PREFIX="http://meetbot-raw.fedoraproject.org/fedora-zh/"
MEETING_NAME="FZUG Weekly Meeting"
MEETING_ISOWEEKDAY=7 # Day of week for meeting. (1--7 for Mon--Sun)
MEETING_TIME="20:00" # Start time of meeting. (24 hour format)
MEETING_TZ="Asia/Shanghai" # Meeting timezone.
TO="[email protected]"
CC=()
SUBJECT="Fedora Chinese Meeting Minutes"
GREETING="""Hi all,
The IRC meeting minutes of THIS_DATE are available at the links below.
Thanks everyone for attending the meeting.
The next IRC meeting will be held on NEXT_DATE. Please come and join
the discussion if you can!
"""
FOOTNOTE="Mail generated by ggmm VERSION"
import re
import email
import email.mime.text
import datetime
import pytz
import sys
import urllib.request
import subprocess
import argparse
#debug config
ENABLE_TRACE = False
WORKING_FINE = True
def trace(s):
if ENABLE_TRACE:
print(s)
def get_user_input(gui=False):
'''获取用户的输入
:param gui: whether to use GUI interface or not (default: False)
:type gui: boolean
:return: 一个 list,里面有三个不带换行符的字符串
'''
result = []
input_tip = "请输入结束会议时 bot 的三行会议纪要链接提示:"
if gui:
output = subprocess.check_output(["zenity", "--text-info", "--editable",
"--width", "600", "--height", "200", "--title", input_tip],
universal_newlines=True)
trace(output)
if output:
result = output.splitlines()
else:
try:
while(True):
get = input()
if get:
result.append(get)
except EOFError:
pass
trace(result)
return result
def get_url(s):
'''把从 IRC 客户端复制出的字符串处理成
("abc", "http://server.org/log.html") 的 tuple
Input example:
<zodbot>:abc http://server.org/log.html
(10:11:58 PM) zodbot: Minutes: http://meetbot.fedoraproject.org/fedora-zh/2015-04-10/fedora-zh.2015-04-10-13.04.html
'''
trace("get_url() from : " + s)
bot_pattern = re.compile(r"""^\(? # Left parenthesis
# Some client will show the time
(
\d{2,4}:\d{2,4} \s* # Hour and minute
(:\d{2}\s*[AP]M\))? # (10:11:58 PM)
\)? # Right parenthesis
)?\s* # Not neccesary to show the time
<?\+?zodbot>?\s*\:?\s* # zodbot
""", re.X)
#answer from http://stackoverflow.com/a/6883094
link_pattern = re.compile(\
'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
match = re.search(link_pattern, s)
if not match:
print("Url Match Error!")
WORKING_FINE = False
url = match.group(0)
trace(url)
s = s.replace(url, "")
match = re.search(bot_pattern, s)
if not match:
print("Can't find bot!")
WORKING_FINE = False
bot = match.group(0)
trace("bot name: " + bot)
s = s.replace(bot, "")
s = s.strip()
s = s.rstrip(':')
s = s.strip()
trace("Description: " + s)
return (s, url)
def fetch_data(url):
'''该函数内不做多线程,就是用对应的库抓取信息
返回一个列表,是解码后的字符串
'''
trace("Fetching "+url)
req = urllib.request.urlopen(url)
lines = req.readlines()
strs = [line.decode('utf-8').rstrip('\n') for line in lines]
trace(strs)
return strs
def make_eml(to, cc, subject, message, log, footnote, date):
'''to: string
cc: list/tuple of strings
subject: string (without date)
message: string
log: list of strings
date: log date
return: a string
'''
email.charset.add_charset('utf-8', None, None, 'utf-8')
msg = email.message.Message()
msg.add_header('To', to)
if cc:
cc = ','.join(cc)
msg.add_header('Cc', cc)
date_str = str(date)
subject += " (" + date_str + ")"
msg.add_header('Subject', subject)
now_utc = datetime.datetime.now(pytz.utc)
tz = pytz.timezone(MEETING_TZ)
# Need to normalize in case MEETING_TZ has DST etc.
now = tz.normalize(now_utc.astimezone(tz))
# Calculate date difference between today and next meeting date.
delta = MEETING_ISOWEEKDAY - now.isoweekday()
if delta == 0:
# Today is also meeting day. Need to check time.
meeting_time = datetime.datetime.strptime(MEETING_TIME, '%H:%M').time()
if now.time() > meeting_time:
delta = 7
next_date = now.date() + datetime.timedelta(delta)
next_date_str = str(next_date)
message = message.replace('THIS_DATE', date_str)
message = message.replace('NEXT_DATE', next_date_str)
content = message + '\n' + '\n'.join(log) + '\n\n' + footnote + '\n'
trace(content)
msg.set_payload(content, "utf-8")
return msg.as_string()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='GGMM: GGMM Generates Minutes Mail.')
parser.add_argument('--gui', dest='gui', action='store_true',
default=False, help='enable GUI mode (default: disabled)')
parser.add_argument('-a', '--auto', dest='auto', action='store_true',
default=False, help='enable auto mode (default: disabled)')
args = parser.parse_args()
date = datetime.date.today()
if args.auto:
meeting_name_in_url = MEETING_NAME.lower().replace(' ', '_')
href_pattern_str = 'href="(' + meeting_name_in_url + '[\d.-]+\.txt)"'
href_pattern = re.compile(href_pattern_str)
while True:
trace("Trying date {}...".format(date))
found = True
try:
file_list_html = '\n'.join(fetch_data(URL_PREFIX + str(date)))
except urllib.error.HTTPError as e:
if e.code == 404:
found = False
pass
if found:
trace(file_list_html)
urls = re.findall(href_pattern, file_list_html)
if urls:
trace("Match!")
break
date = date - datetime.timedelta(days=1)
# In case there are multiple logs, assume the last one is what we want.
url = URL_PREFIX + str(date) + '/' + urls[-1]
print(url)
print("Fetching data from server......")
log = fetch_data(url)
else:
user_input = get_user_input(args.gui)
#urls: description -> url
urls = dict()
for uinpt in user_input:
url = get_url(uinpt)
urls[ url[0] ] = url[1]
link_str = []
for key, value in urls.items():
link_str.append(key + ": " + value)
print("Fetching data from server......")
log = fetch_data(urls['Minutes (text)'])
log = link_str + ["",""] + log
eml = make_eml(TO, CC, SUBJECT, GREETING, log, FOOTNOTE, date)
file = open('irc_meeting_log.eml', 'w')
file.write(eml)
file.close()
print("Finished!")
if not WORKING_FINE:
print("Warning! Something might be wrong!")
print("Please re-check the mail before sending it")