批量导入腾讯企业邮箱日历提醒

最近公司的人力小姐姐问我有没有用过备忘录,可以批量导入的那种。我只在手机自带的那种便利贴之类的APP。我一般会把重要的事情或者要记录的事情放在我的dokuwiki上,可以随时记录。很显然小姐姐是有重复性的工作会议或者什么的需要提醒。我好奇的问了,告诉我是用的腾讯企业邮箱。是在日历功能里有个提醒。我想是可以实现批量导入备忘事件的。
原本以为就是几个简单的post就可以搞定,然鹅…..用浏览器看到提交的表单后表示想放弃

提取RSA算法的公钥

1
2
3
4
5
def get_public_key(content):
regexp = r'var\s*PublicKey\s*=\s*"(\w+?)";'
results = re.findall(regexp, content)
if results:
return results[0]

获取ts参数

1
2
3
4
5
def get_ts(content):
regexp = r'PublicTs\s*=\s*"([0-9]+)"'
results = re.findall(regexp, content)
if results:
return results[0]

计算p参数

1
2
3
4
5
def get_p(public_key, password, ts):
public_key = rsa.PublicKey(int(public_key, 16), 65537)
res_tmp = rsa.encrypt(
'{password}\n{ts}\n'.format(password=password, ts=ts), public_key)
return base64.b64encode(res_tmp)

提交提醒数据

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
def Calender_reminder(cookies, sid, Reminder_thing):
# 提醒
StartTime = Reminder_thing[0]
EndTime = Reminder_thing[1]
Something = Reminder_thing[2]
Reminder_time = Reminder_thing[3]
post_data = {}
post_data['res'] = 'CalendarRes.Events'
post_data['sid'] = sid
post_data['resp_charset'] = 'UTF8'
post_data['f'] = 'json'
# post_data['requests_array'] = '[{"Header":{"sResId":"CalendarRes","sMethod":"Create","sPath":"Events","sRandId":9474366},"Data":{"sStartTime":"2018-04-30|00:00:00","sEndTime":"2018-04-30|00:00:00","sSubject":"测试","nAllDay":1,"oReminder":{"nReminderType":1,"sTime":"2018-04-30|15:30:00"},"nFolder":"0","nCreateTime":1523604407622,"sColorClass":"sharedCalendar_circle_brownness"}}]'
post_data['requests_array'] = '[{"Header":{"sResId":"CalendarRes","sMethod":"Create","sPath":"Events","sRandId":9474366},"Data":{"sStartTime":"'+StartTime+'","sEndTime":"'+EndTime+'","sSubject":"'+Something+'","nAllDay":1,"oReminder":{"nReminderType":1,"sTime":"'+Reminder_time+'"},"nFolder":"0","nCreateTime":1523604407622,"sColorClass":"sharedCalendar_circle_brownness"}}]'

headers = {'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Host': 'mail.lifehealthcare.com',
'Cookie': cookies
}
url = 'http://mail.lifehealthcare.com/cgi-bin/WmCommCgi'
post_data = urllib.urlencode(post_data)
resp = session.get(url, headers=headers, params=post_data)
return resp.content

登录邮箱

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
if __name__ == "__main__":
description = "获取腾讯企业邮箱通讯录"
parser = argparse.ArgumentParser(description=description, usage=msg())
parser.add_argument(
"-u", "--email", required=True, dest="email", help="邮箱名")
parser.add_argument(
"-p", "--password", required=True, dest="password", help="邮箱密码")
parser.add_argument(
"-f", "--ifile", required=True, dest="inputfile", help="输入文件,格式为scv逗号分隔,4列分别为:开始时间,结束时间,备忘事件,提醒时间")
args = parser.parse_args()
email = args.email
password = args.password
limit = args.limit
emailfile = args.emailfile
departfile = args.departfile
inputfile = args.inputfile
session = requests.Session()

headers = {'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
'DNT': '1',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4',
}
resp = session.get('https://exmail.qq.com/login', headers=headers)
content = resp.content

public_key = get_public_key(content)

ts = get_ts(content)

p = get_p(public_key, password, ts)

uin = email.split('@')[0]
domain = email.split('@')[1]

post_data = {}
post_data['sid'] = ''
post_data['firstlogin'] = False
post_data['domain'] = domain
post_data['aliastype'] = 'other'
post_data['errtemplate'] = 'dm_loginpage'
post_data['first_step'] = ''
post_data['buy_amount'] = ''
post_data['year'] = ''
post_data['company_name'] = ''
post_data['is_get_dp_coupon'] = ''
post_data['starttime'] = int(time.time() * 1000)
post_data['redirecturl'] = ''
post_data['f'] = 'biz'
post_data['uin'] = uin
post_data['p'] = p
post_data['delegate_url'] = ''
post_data['ts'] = ts
post_data['from'] = ''
post_data['ppp'] = ''
post_data['chg'] = 0
post_data['loginentry'] = 3
post_data['s'] = ''
post_data['dmtype'] = 'bizmail'
post_data['fun'] = ''
post_data['inputuin'] = email
post_data['verifycode'] = ''

headers = {'Content-Type': 'application/x-www-form-urlencoded'}
url = 'https://exmail.qq.com/cgi-bin/login'
resp = session.post(url, headers=headers, data=post_data)

regexp = r'sid=(.*?)"'
cookies_dict = resp.cookies.get_dict()
cookies = ''
for i in cookies_dict.keys():
cookies += "%s=%s; " % (i, cookies_dict[i])

sid = re.findall(regexp, resp.content)[0]

with open(inputfile,'r') as infile:
# infile.readline()
for line in infile:
Reminder_thing = line.strip().strip("\n").split(",")
Calender_reminder(cookies, sid, Reminder_thing)

然后把提醒事件整理成上面的格式传入就可以了,大功告成

本来是想用py2exe做一个小应用然后别人用的,发现实在太复杂了,为了这么一个小功能还要编写一些没必要图形界面,然后最后编译出一大坨文件。

最后的解决方案是Flask,轻便的web框架。直接在网页上像服务器上传提醒事件即可。

Flask框架

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
#! coding=utf-8
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
import commands
import os
from werkzeug.utils import secure_filename
import time

upload_script = '../uploat_beiwanglu.py'

app = Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def upload():
error = None
if request.method == 'POST':
Mail_address = request.form['Mail_address']
Password = request.form['Password']
zhanghaomima = open("zhanghaomima.txt",'a+')
zhanghaomima.write("%s\t%s\n" % (Mail_address, Password))
Upload_file = request.files['Upload_file']
unix_time = int(time.time())
Upload_file_path = "/my_project/login_mail/web/static/uploads/%s_%s" % (unix_time, secure_filename(Upload_file.filename))
Upload_file.save(Upload_file_path)
os.system('python %s -u %s -p %s -f %s' % (upload_script, Mail_address, Password, Upload_file_path))
return '导入完成,快去你的邮箱看看吧!<br><br><a href="http://60.205.203.207:5555/">继续导入</a>'
# return 'python %s -u %s -p %s -f %s' % (upload_script, Mail_address, Password, Upload_file_path)
return render_template('index.html', error=error)

@app.route('/<filename>')
def download(filename):
return None

if __name__ == '__main__':
app.run(host='0.0.0.0',port=5555)