Upgraded imapfetch.py to use python3

Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
Janos SUTO 2020-02-23 09:58:24 +01:00
parent 4c0030b636
commit 1629a93b88

View File

@ -1,9 +1,10 @@
#!/usr/bin/python #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import argparse import argparse
import imaplib import imaplib
import pprint import pprint
import re
opts = {} opts = {}
INBOX = 'INBOX' INBOX = 'INBOX'
@ -13,14 +14,29 @@ def read_folder_list(conn):
result = [] result = []
rc, folders = conn.list() rc, folders = conn.list()
if opts['verbose']:
print("Folders:", folders)
for folder in folders: for folder in folders:
f = folder.decode('utf-8').split(' "." ') if opts['verbose']:
result.append(f[1]) print("Got folder", folder)
if isinstance(folder, type(b'')):
folder = folder.decode('utf-8')
elif isinstance(folder, type(())):
folder = re.sub(r'\{\d+\}$', '', folder[0]) + folder[1]
# The regex should match ' "/" ' and ' "." '
if folder:
f = re.split(r' \"[\/\.]\" ', folder)
result.append(f[1])
return [x for x in result if x not in opts['skip_folders']] return [x for x in result if x not in opts['skip_folders']]
def process_folder(conn, folder): def process_folder(conn, folder):
print("Processing {}".format(folder))
rc, data = conn.select(folder) rc, data = conn.select(folder)
n = int(data[0]) n = int(data[0])
print("Folder {} has {} messages".format(folder, n)) print("Folder {} has {} messages".format(folder, n))
@ -29,8 +45,10 @@ def process_folder(conn, folder):
rc, data = conn.search(None, 'ALL') rc, data = conn.search(None, 'ALL')
for num in data[0].split(): for num in data[0].split():
rc, data = conn.fetch(num, '(RFC822)') rc, data = conn.fetch(num, '(RFC822)')
if opts['verbose']:
print(rc, num)
opts['counter'] = opts['counter'] + 1 opts['counter'] = opts['counter'] + 1
with open("{}.eml".format(opts['counter']), "w") as f: with open("{}.eml".format(opts['counter']), "wb") as f:
f.write(data[0][1]) f.write(data[0][1])
@ -42,8 +60,9 @@ def main():
parser.add_argument("-p", "--password", type=str, help="imap password", parser.add_argument("-p", "--password", type=str, help="imap password",
required=True) required=True)
parser.add_argument("-x", "--skip-list", type=str, help="IMAP folders to skip", parser.add_argument("-x", "--skip-list", type=str, help="IMAP folders to skip",
default="junk,trash,spam,draft,") default="junk,trash,spam,draft")
parser.add_argument("-f", "--folder", type=str, help="IMAP folder to download") parser.add_argument("-f", "--folders", type=str,
help="Comma separated list of IMAP folders to download")
parser.add_argument("-v", "--verbose", help="verbose mode", action='store_true') parser.add_argument("-v", "--verbose", help="verbose mode", action='store_true')
args = parser.parse_args() args = parser.parse_args()
@ -63,11 +82,14 @@ def main():
conn.login(args.user, args.password) conn.login(args.user, args.password)
conn.select() conn.select()
if args.folder: if args.folders:
folders = [args.folder] folders = args.folders.split(',')
else: else:
folders = read_folder_list(conn) folders = read_folder_list(conn)
if opts['verbose']:
print("Folders will be processed: {}".format(folders))
for folder in folders: for folder in folders:
process_folder(conn, folder) process_folder(conn, folder)