#流程
- 從gmail裡面讀取電子報(特定的寄件者)
- 取得郵件內容
- 分析html
- 取得所要的部份(s)
- 將要轉錄的文章發布到wordpress上
- 將已處理的信件從收件夾中移到另一個資料夾中
- 結束作業
#開發環境 python 3.3
#有用到的package
- imaplib --> 用來處理imap type email
- email --> email結構
- re
- BeautifulSoup4 --> work with python 3.3 --> from bs4 import BeautifulSoup
- wordpress_xmlrpc --> 透過xmlrpc的方式新增文章
所遇到的問題
- 編碼 a. 信件 b. 信件內容
- html形式的email裡面藏有\xa0 , 還是因為編碼的關係
其實在使用bs4的過程中,也是花最多時間的地方是如何讓中文正常的顯示出來。其實在第一段要取得信件裡的內容時,就遇到中文編碼的問題。
demo code
1 | import imaplib,email,re |
在3.3裡, email.message_from_string 傳進去的文字如果不加decode,會傳錯誤訊息出來。
1 | email.message_from_string(response_part[1].decode()) |
發布文章到wordpress 參考網址: http://python-wordpress-xmlrpc.readthedocs.org/en/latest/index.html
class postToWP:
def post(title,content,tag):
client = Client('http://blog url//xmlrpc.php', 'username', 'password')
post = WordPressPost()
post.title = title
post.content = content
post.terms_names = {
'post_tag': [tag], # 標籤(Tag)
'category': [tag], # 分類(Categories)
}
post.post_status = 'publish' # 如果沒有這一個,就會是草稿狀態
post.id = client.call(NewPost(post))