Skip to content
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

Add --google-takeout-flagged-labels #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ Options:
--google-takeout-language=GOOGLE_TAKEOUT_LANGUAGE
[Use specific language. Supported languages: 'en es ca
de'. default: en]
--google-takeout-flagged-labels=GOOGLE_TAKEOUT_FLAGGED_LABELS
Mark Mails with given labels (comma separated) as
flagged, by default the Important flag is used
--debug Debug: Make some error messages more verbose.
--dry-run Do not perform IMAP writing actions
```
Expand Down
29 changes: 20 additions & 9 deletions imap_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def __init__(self):
help="Priority of labels, if --google-takeout-first-label is used")
self.add_option("--google-takeout-language",
help="[Use specific language. Supported languages: '%s'. " % (" ".join(self.google_takeout_supported_languages)) + "default: %default]" )
self.add_option("--google-takeout-flagged-labels", type="string",
help="Mark Mails with given labels (comma separated) as flagged, by default the Important flag is used. Labels have to be specified according to the language of your export")
self.add_option("--debug", action="store_true",
help="Debug: Make some error messages more verbose.")
self.add_option("--dry-run", action="store_true",
Expand All @@ -119,7 +121,8 @@ def __init__(self):
google_takeout_first_label=False,
google_takeout_label_priority="",
google_takeout_language="en",
debug=False,
google_takeout_flagged_labels="",
debug=False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this even run?
Shouldn't debug=False be debug=False, (with a comma) as it's not the latest parametre?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry - had to rebase and did not test... will fix that..

dry_run=False,
)

Expand Down Expand Up @@ -250,7 +253,7 @@ class Progress():
"""Store and output progress information."""

def __init__(self, total_count, google_takeout=False, google_takeout_first_label=False,
google_takeout_label_priority=None, google_takeout_language="en"):
google_takeout_label_priority=None, google_takeout_language="en", google_takeout_flagged_labels=""):
self.total_count = total_count
self.ok_count = 0
self.count = 0
Expand All @@ -260,6 +263,7 @@ def __init__(self, total_count, google_takeout=False, google_takeout_first_label
self.google_takeout_first_label = google_takeout_first_label
self.google_takeout_label_priority = google_takeout_label_priority
self.google_takeout_language = google_takeout_language
self.google_takeout_flagged_labels = google_takeout_flagged_labels.split(',')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about someone using:

--google-takeout-flagged-labels='"My spaced label","My spaced with commas, label", "NonSpacedLabel"' ?

Please check csv_file in the current code where I implemented a parser for these edge-corner cases.


def begin(self, msg):
"""Called when start proccessing of a new message."""
Expand Down Expand Up @@ -355,9 +359,13 @@ def begin(self, msg):
else:
flags.append('\Seen')

if labels.count(gmail_important_str) > 0:
flags.append('\Flagged')
labels.remove(gmail_important_str)
if len(self.google_takeout_flagged_labels) == 0:
self.google_takeout_flagged_labels = [gmail_important_str]

for label in self.google_takeout_flagged_labels:
if labels.count(label) > 0:
flags.append('\Flagged')
labels.remove(label)

if ((labels.count(gmail_sent_str) > 0) and (len(labels) > 1)):
labels.remove(gmail_sent_str)
Expand Down Expand Up @@ -422,13 +430,14 @@ def endAll(self):


def upload(imap, box, src, err, time_fields, google_takeout=False, google_takeout_first_label=False,
google_takeout_label_priority=None, google_takeout_box_as_base_folder=False, google_takeout_language="en",
debug=False):
google_takeout_label_priority=None, google_takeout_box_as_base_folder=False, google_takeout_language="en",
google_takeout_flagged_labels="", debug=False):
print("Uploading to {}...".format(box))
print("Counting the mailbox (it could take a while for the large one).")
p = Progress(len(src), google_takeout=google_takeout, google_takeout_first_label=google_takeout_first_label,
google_takeout_label_priority=google_takeout_label_priority,
google_takeout_language=google_takeout_language)
google_takeout_language=google_takeout_language,
google_takeout_flagged_labels=google_takeout_flagged_labels)
for i, msg in src.iteritems():
try:
p.begin(msg)
Expand Down Expand Up @@ -717,6 +726,7 @@ def main(args=None):
google_takeout_first_label = options.pop("google_takeout_first_label")
google_takeout_label_priority = options.pop("google_takeout_label_priority").split(",")
google_takeout_language = options.pop("google_takeout_language")
google_takeout_flagged_labels = options.pop("google_takeout_flagged_labels")
debug = options.pop("debug")


Expand All @@ -741,7 +751,8 @@ def main(args=None):
if err:
err = mailbox.mbox(err)
upload(uploader, options["box"], src, err, time_fields, google_takeout, google_takeout_first_label,
google_takeout_label_priority, google_takeout_box_as_base_folder, google_takeout_language, debug)
google_takeout_label_priority, google_takeout_box_as_base_folder, google_takeout_language,
google_takeout_flagged_labels, debug)
else:
recursive_upload(uploader, "", src, err, time_fields, email_only_folders, separator)

Expand Down