很早之前,無意中我發(fā)現(xiàn)二十四節(jié)氣上的節(jié)氣壁紙非常的好看。于是我就把壁紙的高清版下載了下來,每到新節(jié)氣開始的時候就手動設(shè)置壁紙。后來為了嫌麻煩,就做了一個由 batch,vbs,和outlook里面寫macro 結(jié)合的一個小程序,每天判斷當(dāng)天是否是一個新的節(jié)氣的開始,如果是就自動設(shè)當(dāng)前節(jié)氣為主題的壁紙,并且將該壁紙embeded 在 outlook郵件中發(fā)給我自己。
再后來覺得這么一個個的script文件不好看,就干脆用一個python文件把它們整合起來。
第一步, 把24節(jié)氣的24張壁紙下載到本地,并以拼音進(jìn)行命名

節(jié)氣壁紙
第二步,爬介紹24節(jié)氣的網(wǎng)頁,得到各個節(jié)氣的日期,名字和簡介。
我用的是以下兩個網(wǎng)站
page 1
page 2
第三步,判斷今天日期是否與其中的節(jié)氣開始的日期相等,如果相等則準(zhǔn)備發(fā)郵件和設(shè)壁紙

由于該網(wǎng)站上是前三個縮寫字母來表示月份,在python里面如何快速將一月份 Jan 轉(zhuǎn)換成數(shù)字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發(fā)郵件。
由于我安裝的是outlook客戶端,所以我可以直接調(diào)用 win32 的庫來對outlook進(jìn)行操作
需要注意的是,當(dāng)插入圖片作為附件時,郵件并不會自動展示該圖片。但是我想做的是把圖片embeded在郵件內(nèi)容中進(jìn)行發(fā)送,這樣接收者不僅可以很清晰的預(yù)覽該圖片,并且可以下載它。
在code里面,我們不僅需要在htmlbody里面插入img標(biāo)簽(注意需要加入cid),
而且需要給郵件的attachment設(shè)置 屬性:
sttachment = mail.Attachments.Add(attachment_path)
sttachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", File_name )

image.png
第五步,設(shè)置壁紙
如果是windows系統(tǒng),則需要
import ctypes
SPI_SETDESKWALLPAPER = 20
output=ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, attachment_path , 0)
第六步,每天用windows scheduler 定時運(yùn)行該程序。
最后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("http://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("http://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></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()