很早之前率寡,無意中我發(fā)現(xiàn)二十四節(jié)氣上的節(jié)氣壁紙非常的好看。于是我就把壁紙的高清版下載了下來废累,每到新節(jié)氣開始的時候就手動設置壁紙邓梅。后來為了嫌麻煩,就做了一個由 batch邑滨,vbs日缨,和outlook里面寫macro 結(jié)合的一個小程序,每天判斷當天是否是一個新的節(jié)氣的開始掖看,如果是就自動設當前節(jié)氣為主題的壁紙匣距,并且將該壁紙embeded 在 outlook郵件中發(fā)給我自己。
再后來覺得這么一個個的script文件不好看哎壳,就干脆用一個python文件把它們整合起來毅待。
第一步, 把24節(jié)氣的24張壁紙下載到本地归榕,并以拼音進行命名
第二步尸红,爬介紹24節(jié)氣的網(wǎng)頁,得到各個節(jié)氣的日期刹泄,名字和簡介外里。
我用的是以下兩個網(wǎng)站
page 1
page 2
第三步,判斷今天日期是否與其中的節(jié)氣開始的日期相等循签,如果相等則準備發(fā)郵件和設壁紙
由于該網(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進行操作
需要注意的是,當插入圖片作為附件時贼穆,郵件并不會自動展示該圖片。但是我想做的是把圖片embeded在郵件內(nèi)容中進行發(fā)送兰粉,這樣接收者不僅可以很清晰的預覽該圖片故痊,并且可以下載它。
在code里面玖姑,我們不僅需要在htmlbody里面插入img標簽(注意需要加入cid
)愕秫,
而且需要給郵件的attachment設置 屬性:
sttachment = mail.Attachments.Add(attachment_path)
sttachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", File_name )
第五步慨菱,設置壁紙
如果是windows系統(tǒng),則需要
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("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>![](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()