From dc7bdafa4c4ccbc4256bad3cda6929bd4f816068 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sun, 29 Jan 2017 19:21:00 +0100 Subject: [PATCH] First commit --- .gitignore | 1 + .readme.md.swp | Bin 0 -> 12288 bytes notes.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 18 +++++++++++ requeriments.txt | 2 ++ 5 files changed, 102 insertions(+) create mode 100644 .gitignore create mode 100644 .readme.md.swp create mode 100755 notes.py create mode 100644 readme.md create mode 100644 requeriments.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e61812f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +notes/ diff --git a/.readme.md.swp b/.readme.md.swp new file mode 100644 index 0000000000000000000000000000000000000000..7f59212e071e3f5a1ee8665d88e5cb236778e48a GIT binary patch literal 12288 zcmeI2&yLeD5XNUYa6no>%K;%S1A9Z%?Y0H>h{Qi3Q9$f2f*Wq)ZlXV0{oN-FoDw%sKTtby9uM=4*jwLwy$5q zOIru0+mlx_0Vco%m;e)C0!)AjFaaj8Rs@3GfcND1Oy_#5n>YTNyIY=^025#WOn?b6 z0Vco%m;e)C0!)AjFo88BAnDWl*G+)S=gB;M|6l(9|8W7}JLwDQGwCDg18Gbek#3OA zk-~Dq^qQM&oyMimYDz(U;<2l2`~XBzyz286JP@WHGwznfW;KAV4IK` zJ;L^=wy+KMdU+`=#)~*CWnPHHloYMJa#%KZS7TOsUmKU=&Turobvy2hQ|U8+d#1E9 z9Ym9LPH9V%nS863Ysq#8P9Ry)(c>b!&7zi;MN(yzPL)n__%xoQr+ytyWb$n9CO(utw9N@Yl_=_tQd8?#pi3qT KF?ye(2)_XW*U_Q? literal 0 HcmV?d00001 diff --git a/notes.py b/notes.py new file mode 100755 index 0000000..32a0fc3 --- /dev/null +++ b/notes.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import email +from email.header import decode_header +import imaplib +import html2text +import sys +import os +import shutil +import click + + +@click.command() +@click.option('--imap', help='Exampl Gmail: imap.gmail.com') +@click.option('--user', help='Example: my@gmail.com') +@click.option('--password', help='Example: 123456') +def get_notes(imap, user, password): + # Connect + mail = imaplib.IMAP4_SSL(imap) + mail.login(user, password) + # Get data + mail.list() + mail.select("Notes") + result, data = mail.search(None, "ALL") + ids = data[0] + # Ids is a space separated string + folder = 'notes/' + if os.path.exists(folder): + shutil.rmtree(folder) + os.makedirs(os.path.dirname(folder)) + # List of IDs + id_list = ids.split() + # List alls messages + for key, item in enumerate(id_list): + # Get message + result, data = mail.fetch(item, "(RFC822)") + raw_email = data[0][1] + # Parse data + msg = email.message_from_bytes(raw_email) + # Get subject + try: + subject_html = decode_header(msg['Subject'])[0][0].decode('utf-8') + except Exception: + subject_html = decode_header(msg['Subject'])[0][0] + # Get body + try: + body_html = msg.get_payload(decode=True).decode('utf-8') + except Exception: + body_html = msg.get_payload(decode=True) + # Get markdown + if body_html: + # Get text + subject_md = html2text.html2text(str(subject_html)).strip() + body_md = html2text.html2text(str(body_html)).strip() + # Save + filename = folder + (subject_md.replace('/', '-')) + '.md' + new_file = open(filename, 'a') + new_file.write(body_md) + new_file.close() + # Progress + progress(key, len(id_list), subject_md) + # Print information + print('\nFinish! 100%') + + +def progress(count, total, status=''): + ''' + Print progress bar + ''' + bar_len = 60 + filled_len = int(round(bar_len * count / float(total))) + + percents = round(100.0 * count / float(total), 1) + bar = '=' * filled_len + '-' * (bar_len - filled_len) + + sys.stdout.write('[%s] %s%s %s\r' % (bar, percents, '%', status)) + sys.stdout.flush() + + +if __name__ == '__main__': + get_notes() diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..79b47c6 --- /dev/null +++ b/readme.md @@ -0,0 +1,18 @@ +# Notes to MarkDown + +Exports the notes of the application Notes for MacOS, Fastmail... in Markdown. + +## Requeriments + +You will need to have installed python3. Then you will have to install dependencies. + +``` bash +pip install -r requeriments.txt +``` +## Use + +Compatible with any account. You only need to know what the SMTP address of your email account. The following example would be using Gmail. + +``` bash +python3 notes.py --imap imap.gmail.com --user my@gmail.com --password 123456 +``` diff --git a/requeriments.txt b/requeriments.txt new file mode 100644 index 0000000..c539dcd --- /dev/null +++ b/requeriments.txt @@ -0,0 +1,2 @@ +html2text +click