私はPythonでメール解析メカニズムを作成しました。
新しいメールを見つけて、データを正しく渡します。コードが正しく機能していることを99.999%確信しているので、問題はないはずです。問題は、Gmailの受信トレイに「見えない」と見なされるメッセージが殺到することがあることです。この時点で、私のコードでできることは何もありません。
それは失敗します:
imaplib.error:FETCHコマンドエラー:BAD['コマンドを解析できませんでした']
これは苦痛です、そして私はどちらかが欲しいです
- 見えないメッセージがこの状態にオーバーフローしたかどうかを確認する方法、または
- この特定のエラーを検出する方法を含め、すべてのメッセージを手動で(imaplibを介して)既読としてマークする方法。
これを達成する方法について何か考えはありますか?
これが私のコードです:
#!/usr/bin/env python
import imaplib, re, sys, time, OSC, threading, os
iparg = 'localhost'
oportarg = 9000
iportarg = 9002
usern = 'myusrname@gmail.com'
gpass = 'mypass'
kill_program = False
server = imaplib.IMAP4_SSL('imap.googlemail.com', 993)
oclient = OSC.OSCClient()
email_interval = 2.0
def login():
server.login(usern, gpass)
oclient.connect((iparg, oportarg))
def logout_handle(addr, tags, stuff, source):
print 'received kill call'
global kill_program
kill_program = True
def filter_signature(s): #so annoying; wish i didn't have to do this
try:
a_sig = re.sub(r'Sent|--Sent', '', s)
b_sig = re.sub(r'using SMS-to-email. Reply to this email to text the sender back and', '', a_sig)
c_sig = re.sub(r'save on SMS fees.', '', b_sig)
d_sig = re.sub(r'https://www.google.com/voice', '', c_sig)
no_lines = re.sub(r'\n|=|\r?', '', d_sig) #add weird characters to this as needed
except:
nolines = s
return no_lines
def parse_email(interval):
while True:
server.select('INBOX')
status, ids = server.search(None, 'UnSeen')
print 'status is: ', status
if not ids or ids[0] is '':
print 'no new messages'
else:
try:
print 'found a message; attempting to parse...'
latest_id = ids[0]
status, msg_data = server.fetch(latest_id, '(UID BODY[TEXT])')
raw_data = msg_data[0][1]
raw_filter = raw_data
print 'message result: ', raw_filter
time.sleep(interval)
#execute main block
while not kill_program:
login()
parse_email(email_interval)
st.kill()
sys.exit()