概述

我们学校还是天天收健康码,这还不是最烦人的,最烦人的是有一些同学不配合,每天都需要催好几次,所以我就想到用 Python 写个脚本来提醒他们。

实现

数据库

我在本地建了一个数据库:如下图:

包含了他们的学号后两位,姓名,备注,QQ 号

因为我使用坚果云收件箱来收集文件,它可以自动重命名,这就给我省了好多事。

程序

通过文件名来匹配到学号,从而查到谁没提交文件,然后给他发邮件!!!

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

import time
import os
import pymysql
import re
import smtplib
from email.mime.text import MIMEText
import tkinter as tk
from tkinter import filedialog


class studentInfo(object):
'''
学生信息类
'''

def __int__(self):
self.sno: str
self.sname: str
self.snickname: str
self.sqq: str
self.email: str

def __init__(self, sno, sname, snickname, sqq):
self.sno = sno
self.sname = sname
self.snickname = snickname
self.sqq = sqq
self.email = self.sqq + "@qq.com"


class Email:
def __init__(self, content, msg_to):
self.msg_from = 'howiehye@qq.com'
# 授权码,不是邮箱密码
self.passwd = "xxxxxxx"
self.msg_to = msg_to
self.subject = time.strftime("%Y-%m-%d", time.localtime()) + "健康码提交提醒"
self.content = content
self.setemail()

def setemail(self):
self.msg = MIMEText(self.content)
self.msg['Subject'] = self.subject
self.msg['From'] = self.msg_from
self.msg['To'] = self.msg_to

def sendemail(self):
try:
self.s = smtplib.SMTP_SSL("smtp.qq.com", 465)
self.s.login(self.msg_from, self.passwd)
self.s.sendmail(self.msg_from, self.msg_to, self.msg.as_string())
print("发送成功")
except Exception:
print("发送失败")
finally:
self.s.quit()


def readDic():
'''
选取目录
:return:
'''
root = tk.Tk()
root.withdraw()
Folderpath = filedialog.askdirectory()
return Folderpath


def findFiles(FolderPath):
'''
目录下所有文件名
:param FolderPath:
:return:
'''
filelist = os.listdir(FolderPath)
return filelist


def returnSno(filename):
'''
文件名返回学号
:param filename:
:return:
'''
sno = re.compile('(.*?)-', re.S).findall(filename)
return sno


if __name__ == '__main__':
student = []
studentsno = []
link = input("请输入今天的提交联接:")
# "https://workspace.jianguoyun.com/inbox/collect/ea8a13072bcd45a7b74535ea2d1c3b0e/submit"
qq = "https://wpa.qq.com/msgrd?v=3&uin=1018921994&site=qq&menu=yes"
print("请选取存储文件夹:")
Folder1 = readDic()
print(f'当前选则的存储目录:{Folder1}')
print("请再次选取存储文件夹:")
Folder2 = readDic()
if Folder1 == Folder2:
print(f'当前存储目录:{Folder1}')
# 所有文件
namelist = findFiles(Folder1)
# print(namelist)
db = pymysql.connect(
host='localhost',
user='root',
passwd='123456',
db='class_info',
charset='utf8mb4'
)
cursor = db.cursor()
sql = "SELECT * FROM qq_info"
cursor.execute(sql)
data = cursor.fetchall()
# print(data)
for i in range(0, len(data)):
stu = studentInfo(
sno=data[i][0],
sname=data[i][1],
snickname=data[i][2],
sqq=data[i][3])
student.append(stu)
cursor.close()
db.close()

for i in range(0, len(namelist)):
sno = str(returnSno(namelist[i]))
sno = sno.replace("['", '')
sno = sno.replace("']", '')
studentsno.append(sno)

for i in range(0, len(student)):
if student[i].sno in studentsno:
print(student[i].snickname + "已经提交")
else:
print(student[i].snickname + "没有提交,正在发送提醒邮件。。。")
msg_to = student[i].email

content = str(student[
i].sname) + "同学:\r\n\t\t你好,\r\n请及时提交健康码!\r\n点击以下链接可直接提交:\r\n" + link + "\r\n如果无法提交,也可点击以下链接发送给我:\r\n" + qq + "\r\n谢谢配合!\r\n\t本邮件为系统自动发送,如果你已经提交,请忽略!"
email = Email(content, msg_to)
email.sendemail()
else:
print("两次选择的文件夹不同,请检查后重新选择文件夹!")