班级电教程序设计

cctv新闻时间自动播放自动关闭和高考倒计时提醒

(这里使用了ffplay,因为可以直接加参数,而PotPlayer还要手动调,我挺懒的wwwwww)

话不多说,上代码

import time
import os
from datetime import datetime
import win32api, win32con

def get_left_days():
    future = datetime.strptime('2021-06-07 00:00:00', '%Y-%m-%d %H:%M:%S')
    now = datetime.now()
    delta = future - now
    return delta.days

def c():
    t = time.localtime()
    if t.tm_hour == 19 and t.tm_min in range(5):
        return True

if __name__ == '__main__':
    a = win32api.MessageBox(0, "距离高考还有{}天".format(get_left_days()), "提醒学习小助手", win32con.MB_ICONWARNING)
    while True:
        if c() == True:
            os.popen('ffplay -i https://cctvcnch5ca.v.wscdns.com/live/cctv1_2/index.m3u8 -window_title 新闻联播 -fs')
            time.sleep(1860)
            os.system('taskkill /IM ffplay.exe /f')
        else:
            time.sleep(10)

发送PPT和目录内其他文件至班级邮箱

为什么会有这个想法呢,因为每天看电教机旁边都挤着一堆人去拷ppt。都0202年了,感觉这种机械的工作应该给电脑去做。于是就研究了一下smtp,发现挺好用的,再加上Python,几十行代码就搞定了wwwwww

话不多说,上代码

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os
import time
import win32api, win32con
from tqdm import tqdm

def send_file(path):

    msg_from = 't????n?????z@16???om'
    passward = 'F??????????EE'
    msg_to = '[email protected]'

    t = time.localtime()
    subject = '{}-{}-{}_{}:{}:{} Files'.format(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
    content = '<html><body><h1>基于Python的HTML邮件发送</h1>'\
    '<h2>Programmed by <a href="https://www.cnblogs.com/tonysyz/">tony_syz</a> on <a href="http://python.org/">Python</a></h2> <br/>'\
    '<h3>Hi,欢迎关注博客<a href="https://www.cnblogs.com/tonysyz/">tony_syz</a>,个人感觉里面几篇文章还是很有用的,<br/>还有我的<a href="https://space.bilibili.com/562400796">Bilibili</a><br/></h3>'\
    '<h3>以及推荐一些大佬们的<a href="https://space.bilibili.com/66806831/favlist?fid=839410231&ftype=create">manim(超赞的数学动画!)</a></h3><br/>'\
    '<h3>发送邮件的程序的源代码戳这里--><a href="https://github.com/tonysyz/my_code">tonysyz的GitHub</a>,欢迎follow!</h3><br/>'\
    '<h4>使用愉快wwwwww</h4><br/>'\
    '</body></html>'

    #msg = MIMEText(content,'html','utf-8')
    msg = MIMEMultipart()
    msg['Subject'] = subject
    msg['From'] = msg_from
    msg['To'] = msg_to

    #添加html内容
    msg.attach(MIMEText(content,'html','utf-8'))

    files = []
    d=[]
    e=[]
    f=[]
    #添加附件
    for a,b,c in os.walk(path):
        d.append(a)
        e.append(b)
        f.append(c)
    filenames = []
    print('获取文件目录...\n')
    for each in d:
        for e in f[d.index(each)]:
            filenames.append(e)
            files.append('{}\\{}'.format(each, e))
    print('添加文件...\n')
    for each in tqdm(files, ncols=66):
        with open(each,'rb') as f:
            mime = MIMEBase('image','jpg',filename=filenames[files.index(each)])
            mime.add_header('Content-Disposition','attachment',filename=filenames[files.index(each)])
            mime.add_header('Content-ID', '<0>')
            mime.add_header('X-Attachment-Id', '0')
            mime.set_payload(f.read())
            encoders.encode_base64(mime)
            msg.attach(mime)
    print('uploading...\n')
    try:
        s = smtplib.SMTP('smtp.163.com',25)
        s.login(msg_from,passward)
        print('log_in as [email protected]')
        s.sendmail(msg_from,msg_to,msg.as_string())
        win32api.MessageBox(0, '发送成功!', "tony_syz", win32con.MB_OK)
    except smtplib.SMTPException as e:
        win32api.MessageBox(0, 'Error:'+e, "tony_syz", win32con.MB_ICONWARNING)
    finally:
        s.quit()

if __name__ == '__main__':
    i = win32api.MessageBox(0, '是否发送至邮箱?', "tony_syz", win32con.MB_YESNO)
    if i == 6:
        send_file(r'.\\')

猜你喜欢

转载自www.cnblogs.com/tonysyz/p/13369861.html