很早之前,无意中我发现二十四节气上的节气壁纸非常的好看。于是我就把壁纸的高清版下载了下来,每到新节气开始的时候就手动设置壁纸。后来为了嫌麻烦,就做了一个由 batch,vbs,和outlook里面写macro 结合的一个小程序,每天判断当天是否是一个新的节气的开始,如果是就自动设当前节气为主题的壁纸,并且将该壁纸embeded 在 outlook邮件中发给我自己。
再后来觉得这么一个个的script文件不好看,就干脆用一个python文件把它们整合起来。
第一步, 把24节气的24张壁纸下载到本地,并以拼音进行命名
第二步,爬介绍24节气的网页,得到各个节气的日期,名字和简介。
第三步,判断今天日期是否与其中的节气开始的日期相等,如果相等则准备发邮件和设壁纸
由于该网站上是前三个缩写字母来表示月份,在python里面如何快速将一月份 Jan
转换成数字1 呢?
可以用calendar库里面的month_abbr
import calendar
self.abbr_to_num = {name: num for num, name in enumerate(calendar.month_abbr) if num}
Month_num=self.abbr_to_num[Month]
第四步,用outlook发邮件。
由于我安装的是outlook客户端,所以我可以直接调用 win32 的库来对outlook进行操作
需要注意的是,当插入图片作为附件时,邮件并不会自动展示该图片。但是我想做的是把图片embeded在邮件内容中进行发送,这样接收者不仅可以很清晰的预览该图片,并且可以下载它。
在code里面,我们不仅需要在htmlbody里面插入img标签(注意需要加入cid
),
而且需要给邮件的attachment设置 属性:
sttachment = mail.Attachments.Add(attachment_path)
sttachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", File_name )
第五步,设置壁纸
如果是windows系统,则需要
import ctypes
SPI_SETDESKWALLPAPER = 20
output=ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, attachment_path , 0)
第六步,每天用windows scheduler 定时运行该程序。
最后code:
#coding = utf - 8
import time
from datetime import *
import pycurl
import os
import os.path
import argparse
import sys
from os.path import getsize
from StringIO import StringIO
import re
import lxml.html
from lxml.html.soupparser import fromstring
import calendar
import win32com.client as win32
import ctypes
#class definition
class jieqi_class():
url_1 = 'https://www.travelchinaguide.com/intro/focus/solar-term.htm'
url_2 = 'https://www.chinahighlights.com/festivals/the-24-solar-terms.htm'
def __init__(self):
self.Get_jieqi_list()
self.Check_Today_is_Jieqi()
self.send_email()
def Get_jieqi_list(self):
print "start to gather infomation"
c = pycurl.Curl()
c.setopt(pycurl.PROXY, 'http://192.168.87.15:8080')
c.setopt(pycurl.PROXYUSERPWD, 'LL66269:')
c.setopt(pycurl.PROXYAUTH, pycurl.HTTPAUTH_NTLM)
buffer = StringIO()
c.setopt(pycurl.URL, self.url_1)
c.setopt(c.WRITEDATA, buffer)
c.perform()
body = buffer.getvalue().decode('utf-8', 'ignore')
doc = lxml.html.fromstring(body)
jieqi_list = doc.xpath("//table[@class='c_table']/tbody/tr")
self.jieqi_map={}
for i,each_row in enumerate(jieqi_list):
if i>0:
detail=[]
#print "----"
each_row_ = each_row.xpath(".//td")
for each_column in each_row_:
detail.append(each_column.text_content())
self.jieqi_map[i]=detail
buffer = StringIO()
c.setopt(pycurl.URL, self.url_2)
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
body = buffer.getvalue().decode('utf-8', 'ignore')
doc = lxml.html.fromstring(body)
jieqi_list_1 = doc.xpath("//table[@class='table']/tbody/tr")
self.jieqi_explanation_map={}
for i,each_row in enumerate(jieqi_list_1):
if i>0:
more_detail=[]
#print "----"
more_detail = each_row.xpath(".//td/p")[3].text_content()
self.jieqi_explanation_map[i]=more_detail
def Check_Today_is_Jieqi(self):
self.abbr_to_num = {name: num for num, name in enumerate(calendar.month_abbr) if num}
self.hit = False
Today=date.today()
for key,detail in self.jieqi_map.iteritems():
Month = re.search(r"^(\w{3})", detail[1]).group(1)
Month_num=self.abbr_to_num[Month]
day=re.search(r"(\d+)", detail[1]).group(1)
date_jieqi=date(2017, Month_num, int(day))
if Today==date_jieqi:
print "Today, a new jieqi begin! --" + detail[0]
self.hit = True
self.index=key
self.detail=detail
def send_email(self):
if self.hit == True:
print "sending email!"
jieqiname=self.detail[0]
File_name = re.search(r"\((.*)\)", jieqiname).group(1).replace(" ", "").lower()+".jpg"
Summary = self.detail[2]
detail_summary= self.jieqi_explanation_map[self.index]
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'xingwanlibigtrace@gmail.com'
mail.Subject = jieqiname
mail.CC = ""#
mail.BCC = ""
html_body = "<html><body><p><strong>" + Summary + "</strong></p><p>"+detail_summary+"</p>![](cid:"+File_name+")</body></html>"
attachment_path = "D:/wallpaper_pool/"+File_name
sttachment = mail.Attachments.Add(attachment_path)
sttachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", File_name )
mail.HTMLBody = html_body
mail.send
self.set_wallpaper(attachment_path)
def set_wallpaper(self,attachment_path):
SPI_SETDESKWALLPAPER = 20
output=ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, attachment_path , 0)
if output ==1 :
print "set wallpaper successfully!"
app = jieqi_class()