惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
G
Google Developers Blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
V
Visual Studio Blog
博客园 - Franky
S
SegmentFault 最新的问题
Jina AI
Jina AI
爱范儿
爱范儿
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
C
Check Point Blog
月光博客
月光博客
P
Proofpoint News Feed
T
The Blog of Author Tim Ferriss
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Martin Fowler
Martin Fowler

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟
for dinner
2015-08-16 · via 夜行人

部门新来了个同事,负责视频编导和制作。经常需要使用Premiere渲染视频,这是一项十分费时的工作,动辄3-4个小时。

同事时常在傍晚4-5点开始渲染视频,待其完成,将渲染好的视频发送出去,再关闭电脑回家。这样一来,往往就到晚上9/10点了。

这样一来,就没空做晚饭和次日的便当了,念其厨艺棒极,为了午饭时间能闻到土豆炖牛肉的香味,我决定写个脚本完成这个工作。


#思路 ###任务描述 写一个python脚本,该脚本运行在Windows环境下,该脚本监控视频导出目录,发现视频文件生成时,将其同步到云盘,同步完成后给相关人员发送邮件,通知其下载。此后关机。

###任务分解

  • 在Windows搭建python环境(easy)
  • 使用watchdog监控视频导出目录变化
  • 使用bypy同步文件到百度网盘
  • 使用smtp发送邮件
  • 关机:from sys import system;system('halt')

#准备工作

###配置bypy 在终端执行bypy.py info,之后按照提示操作就行

#show my code ###源代码 看这里:for_dinner

 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
#for_dinner.py
#!/usr/bin/env python
# encoding: utf-8

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler,FileSystemEventHandler

from send_emails import send_mail
import commands
from os import system
from config import watchdir,watch_file_type,remotedir

def notice2download(sub=u"下载视频",content=u"请登录百度网盘"):
    send_mail(sub,content)

def upload_watch_file(file):
    pass

class MyHandler(FileSystemEventHandler):
    def on_created(self, event):
        if (event.src_path).endswith(watch_file_type):
            print event.src_path
            #上传到云盘
            mycommands = "bypy.py upload " + event.src_path + " " + remotedir
            output = commands.getoutput(mycommands)
            print output
            #发送邮件
            content = u"新建文件:"+event.src_path
            notice2download(content=content)
            time.sleep(60*10) #十分钟后关机
            system('halt')

    def on_modified(self, event):
        if (event.src_path).endswith(watch_file_type):     #监控指定文件内容、权限等变化
            print "log file %s changed!" % event.src_path

if __name__ == "__main__":
    path = sys.argv[1] if len(sys.argv) > 1 else watchdir
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()          
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#send_emails.py
#!/usr/bin/env python
# encoding: utf-8

import smtplib
from email.mime.text import MIMEText
from config import mailto_list,mail_host,mail_user,mail_pass



def send_mail(sub,content,to_list=mailto_list):
    msg = MIMEText(content, 'html', 'utf-8')
    msg['Subject'] = sub
    msg['From'] = mail_user
    try:
        server = smtplib.SMTP()
        server.connect(mail_host)
        server.login(mail_user,mail_pass)
        server.sendmail(mail_user, to_list, msg.as_string())
        server.close()
        return True
    except Exception, e:
        print str(e)
        return False

配置文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#config.py
#!/usr/bin/env python
# encoding: utf-8

watchdir = "/tmp" #监控的目录
watch_file_type = "txt" #监控的文件类型
remotedir = "remotedir" #云盘上的目录,需要先创建bypy.py mkdir remotedir
mailto_list=["someone@example.com",] #接收人
mail_host="smtp.example.com"  #设置服务器
mail_user="user@example.com"    #用户名
mail_pass="password"   #口令

#使用 ###mac/linux sudo python for_dinner.py

其中/tmp是需要监控的目录

关机权限需要sudo

###windows 写一个bat脚本

1
2
#run.bat
python for_dinner.py

右键以管理员身份运行

文章作者 种瓜

上次更新 2015-08-16