show.py (1787B)
1 # standard imports 2 import json 3 import email 4 import time 5 import sys 6 import logging 7 8 logg = logging.getLogger(__file__) 9 10 def parse_args(argparser): 11 pass 12 13 def check_args(args): 14 pass 15 16 # TODO: this should call a render method of the entry instead of 17 # dictating how the display format should be 18 def execute(config, feed, args): 19 i = 0 20 while 1: 21 try: 22 e = feed.next_entry() 23 except IndexError as e: 24 break 25 j = json.loads(e) 26 m = email.message_from_string(j['payload']) 27 tf = email.utils.parsedate(m.get('Date')) 28 t = int(time.mktime(tf)) 29 tl = time.localtime(t) 30 ts = time.strftime('%x %X', tl) 31 body = '' 32 attachments = [] 33 ii = 0 34 for p in m.walk(): 35 ii += 1 36 if ii == 1: 37 continue 38 39 if 'attachment' in p.get_content_disposition(): 40 attachments.append('{} ({})'.format(p.get_filename(), p.get_content_type())) 41 elif p.get_content_maintype() == 'text': 42 subject = p.get('Subject') 43 if subject == None: 44 subject = p.get_filename() 45 #if p.get_filename() == '_content' or body == None: 46 body += '>>> {}\n\n{}\n\n\n'.format(subject, p.get_payload(decode=True).decode('utf-8')) 47 48 if i > 0: 49 sys.stdout.write('----\n') 50 51 if body != None: 52 if args.headers: 53 for k in m.keys(): 54 print('{}: {}'.format(k, m.get(k))) 55 sys.stdout.write('{} - {}\n'.format(ts, j['uuid'])) 56 sys.stdout.write('{}'.format(body)) 57 for a in attachments: 58 sys.stdout.write('+ {}\n'.format(a)) 59 60 i += 1 61 sys.stdout.flush()