-
Notifications
You must be signed in to change notification settings - Fork 1
/
kindle.py
103 lines (90 loc) · 3.67 KB
/
kindle.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import re
import os
import time
import poplib
import shelve
from email.parser import Parser
from email.header import decode_header
import fanfou
poplib._MAXLINE = 20480
curdir = os.path.dirname(os.path.abspath(__file__))
db = shelve.open(os.path.join(curdir, 'kindle-sent'))
class Inbox:
def __init__(self, server, user, pass_):
self.conn = poplib.POP3_SSL(server)
self.conn.user(user)
self.conn.pass_(pass_)
self.mails = []
self.inbox = []
def fetch(self):
resp, mails, octets = self.conn.uidl()
mail_ids = [mail.split(' ')[1] for mail in mails]
for mail_id in mail_ids:
if not db.get(mail_id):
pos = mail_ids.index(mail_id) + 1
resp, lines, octets = self.conn.retr(pos)
self.mails.append({'mail_id': mail_id, 'mail': '\r\n'.join(lines)})
self.conn.quit()
for mail in self.mails:
self.parse(mail)
return self.inbox
def parse(self, mail):
msg = Parser().parsestr(mail.get('mail'))
text = None
for part in msg.walk():
if not part.is_multipart():
if part.get_content_type() in ('text/plain', 'text/html'):
content = part.get_payload(decode=True)
charset = self.get_charset(part)
if charset:
content = content.decode(charset)
templates = (
u'Hi, I[\'ʼ]m reading this book,? and wanted to share this quote with you\.[\r\n]+"([\s\S]+)" \(by',
u'Hi, I[\'ʼ]m reading this book,? and wanted to share this quote with you\.[\r\n]+"([\s\S]+)" \(from',
u'Hi, I[\'ʼ]m reading this book,? and wanted to share this quote with you\.[\r\n]+《([\s\S]+)》\(摘自由',
u'您好,我觉得这本书值得一读,您怎么看?[\r\n]+"([\s\S]+)" by',
u'嗨,我正在读这本书,想跟您分享一句名言。[\r\n]+"([\s\S]+)" \(from',
u'嗨,我正在读这本书,想跟您分享一句名言。[\r\n]+《([\s\S]+)》\(摘自由',
)
for template in templates:
matching = re.search(template, content)
if matching:
text = matching.group(1)
break
if text:
self.inbox.append({'text': text, 'mail_id': mail['mail_id']})
def get_charset(self, msg):
charset = msg.get_charset()
if charset is None:
content_type = msg.get('Content-Type', '').lower()
pos = content_type.find('charset=')
if pos >= 0:
charset = content_type[pos + 8:].strip()
end_pos = charset.find(';')
if end_pos > 0:
charset = charset[:end_pos]
charset = charset.strip('"').strip("'")
return charset or 'utf-8'
def decode_str(self, s):
value, charset = decode_header(s)[0]
if charset:
value = value.decode(charset)
return value
if __name__ == '__main__':
server = 'pop.qq.com' # 邮箱 pop3 服务地址
user = '[email protected]' # 邮箱地址
pass_ = 'qwertyuiopz' # 授权码
consumer = {'key': 'your key',
'secret': 'your secret'} # 你的 Consumer
client = fanfou.XAuth(consumer, 'username', 'password') # 你的用户名和密码
fanfou.bound(client)
for mail in Inbox(server, user, pass_).fetch():
print('update {0}'.format(mail['mail_id']))
client.statuses.update({'status': mail['text']})
db[mail['mail_id']] = 1
time.sleep(0.5)