-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve Gmail labels in the mbox as sub-labels of the main mbox label. #32
base: master
Are you sure you want to change the base?
Changes from all commits
3a7885a
cfa0c78
c356b82
5560c21
f2e186c
4e4d39e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
|
||
import argparse | ||
import base64 | ||
from csv import reader | ||
import io | ||
import json | ||
import logging | ||
|
@@ -87,6 +88,33 @@ | |
help= | ||
"Replace 'Content-Type: text/quoted-printable' with text/plain (default: " | ||
"replace it)") | ||
parser.add_argument( | ||
'--takeout-labels', | ||
dest='takeout_labels', | ||
choices=[True, False, 'sublabels'], | ||
required=False, | ||
default=True, | ||
#action='store_false', | ||
help= | ||
"Preserve Gmail labels from Takeout mbox files. " | ||
"Or optionally, transform them into sublabels. " | ||
"(default: keep them)") | ||
parser.add_argument( | ||
'--takeout-no-unread', | ||
dest='takeout_no_unread', | ||
required=False, | ||
action='store_false', | ||
help= | ||
"Don't preserve read/unread state from Takeout mbox files " | ||
"(default: keep it)") | ||
parser.add_argument( | ||
'--takeout-spam-trash', | ||
dest='takeout_spam_trash', | ||
required=False, | ||
action='store_true', | ||
help= | ||
"Import messages from Spam/Trash labels in Takeout mbox files " | ||
"(default: skip them)") | ||
parser.add_argument( | ||
'--num_retries', | ||
default=10, | ||
|
@@ -137,6 +165,8 @@ def get_label_id_from_name(service, username, labels, labelname): | |
if labelname.endswith('.mbox'): | ||
# Strip .mbox suffix from folder names | ||
labelname = labelname[:-5] | ||
if labelname.upper() == 'UNREAD': | ||
return u'UNREAD' # magic label always there | ||
for label in labels: | ||
if label['name'] == labelname: | ||
return label['id'] | ||
|
@@ -159,6 +189,39 @@ def get_label_id_from_name(service, username, labels, labelname): | |
raise | ||
|
||
|
||
# TODO: instead of passing mbox_label_name as param, can't we look it up from id? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can, but it would be less efficient (potentially much slower if you have many labels and many messages). I think we can drop this comment unless you feel strongly about it. |
||
def get_metadata(service, username, labels, msg, mbox_label_id, mbox_label_name): | ||
"""Find Gmail labels and preserve them for import. | ||
|
||
Returns: | ||
A metadata object. | ||
""" | ||
label_ids = [mbox_label_id] | ||
|
||
if args.takeout_labels and 'X-Gmail-Labels' in msg: | ||
gmail_labels = next(reader([msg['X-Gmail-Labels'].replace('\r\n', '')])) | ||
logging.info('Found Gmail Labels: %s', gmail_labels) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe "Found Gmail labels from Takeout"? Otherwise the user might not understand the context (if they don't remember this mbox came from Takeout, etc.), this would help differentiate it from finding a label in the sense of finding a new mbox file. |
||
|
||
if args.takeout_spam_trash: | ||
# TODO: test if we are importing spam/trash without renaming to sublabels | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we not testing that? |
||
if 'Spam' in gmail_labels: | ||
logging.info("Skipped Spam message %d in mbox '%s'", index, mbox_label_name) | ||
return False | ||
if 'Trash' in gmail_labels: | ||
logging.info("Skipped Trash message %d in mbox '%s'", index, mbox_label_name) | ||
return False | ||
|
||
if not args.takeout_no_unread and 'Unread' in gmail_labels: | ||
gmail_labels.remove('Unread') | ||
|
||
for gmail_label in gmail_labels: | ||
if args.takeout_labels == 'sublabels' and gmail_label != 'Unread': | ||
gmail_label = "%s/%s" %(mbox_label_name, gmail_label) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space after % please :) |
||
label_ids.append(get_label_id_from_name(service, username, labels, gmail_label)) | ||
|
||
return {'labelIds': label_ids} | ||
|
||
|
||
def process_mbox_files(username, service, labels): | ||
"""Iterates over the mbox files found in the user's subdir and imports them. | ||
|
||
|
@@ -247,7 +310,8 @@ def process_mbox_files(username, service, labels): | |
message.replace_header('Message-ID', msgid) | ||
except Exception: | ||
logging.exception('Failed to fix brackets in Message-ID header') | ||
metadata_object = {'labelIds': [label_id]} | ||
metadata_object = get_metadata(service, username, labels, message, label_id, labelname) | ||
if not metadata_object: continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
try: | ||
# Use media upload to allow messages more than 5mb. | ||
# See https://developers.google.com/api-client-library/python/guide/media_upload | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove :)