Skip to content

Instantly share code, notes, and snippets.

@giampaolo44
Last active July 17, 2016 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giampaolo44/0265895823ba358ed74223ded0e77af1 to your computer and use it in GitHub Desktop.
Save giampaolo44/0265895823ba358ed74223ded0e77af1 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET #import the xml library ElementTree
def getProperties(entry, ns): #function to find if it is being trashed and/or forwarded
for app_property in entry.findall('apps:property', ns):
name = app_property.attrib['name']
if name in ('forwardTo', 'shouldTrash'):
yield name
def getAttribute(entry,ns,searched_attrib): #function to find given attributes of entries
string_attrib = 'apps:property[@name=\"'+ searched_attrib + '\"]'
return_attrib = entry.find(string_attrib, ns)
final_return_attrib = entry.find(string_attrib, ns).attrib['value'] if return_attrib != None else ''
return final_return_attrib
ns = {
'atom':'http://www.w3.org/2005/Atom',
'apps':'http://schemas.google.com/apps/2006',
}
tree = ET.parse('mailFilters.xml') #parse the gmail xml filter file
for index, entry in enumerate(tree.findall('atom:entry', ns)): #search entries and extract attributes
label = getAttribute(entry,ns,"label")
fromField = getAttribute(entry,ns,"from")
HTWField = getAttribute(entry,ns,"hasTheWord")
print('%d,%s,%s,%s,%s' %(index, fromField, HTWField, label, ','.join(getProperties(entry, ns)))) #print in csv format
@giampaolo44
Copy link
Author

While learning some Python....
A script to read the mailFilters.xml file that you get by exporting your gmail filters
It creates a csv file with an index, the from field if it is populated, the hasTheWord if populate, the label to which the email gets assigned, and the properties forwardTo and/or the shouldTrash ones
With major contributions from @frafra

@frafra
Copy link

frafra commented Jul 17, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment