Created
January 8, 2016 18:53
-
-
Save alanbernstein/edca58cf4cacbf6e4135 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httplib2 | |
import os | |
from pprint import pprint | |
from apiclient import discovery | |
import oauth2client | |
from oauth2client import client | |
from oauth2client import tools | |
try: | |
import argparse | |
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() | |
except ImportError: | |
flags = None | |
def main(): | |
ga = GmailApi() | |
#ga.print_labels() | |
ga.archive_old_alerts() | |
class GoogleApi(object): | |
client_secret_file = 'client_secret.json' | |
application_name = 'test_app' | |
def __init__(self): | |
self.credentials = self.get_credentials() | |
self.http = self.credentials.authorize(httplib2.Http()) | |
self.service = discovery.build(self.api_name, self.api_version, http=self.http) | |
def get_credentials(self): | |
"""Gets valid user credentials from storage. | |
If nothing has been stored, or if the stored credentials are invalid, | |
the OAuth2 flow is completed to obtain the new credentials. | |
Returns: | |
Credentials, the obtained credential. | |
""" | |
home_dir = os.path.expanduser('~') | |
credential_dir = os.path.join(home_dir, '.credentials') | |
if not os.path.exists(credential_dir): | |
os.makedirs(credential_dir) | |
credential_path = os.path.join(credential_dir,'test-app.json') | |
store = oauth2client.file.Storage(credential_path) | |
credentials = store.get() | |
if not credentials or credentials.invalid: | |
flow = client.flow_from_clientsecrets(self.client_secret_file, self.scopes) | |
flow.user_agent = self.application_name | |
if flags: | |
credentials = tools.run_flow(flow, store, flags) | |
else: # Needed only for compatability with Python 2.6 | |
credentials = tools.run(flow, store) | |
print 'Storing credentials to ' + credential_path | |
return credentials | |
class GmailApi(GoogleApi): | |
scopes = 'https://mail.google.com/' | |
api_name = 'gmail' | |
api_version = 'v1' | |
def get_labels(self): | |
res = self.service.users().labels().list(userId='me').execute() | |
labels = res.get('labels', []) | |
return labels | |
def print_labels(self): | |
labels = self.get_labels() | |
for label in labels: | |
if label['type'] == 'user': | |
pprint(label) | |
def get_threads_by_label(self, label_ids): | |
res = self.service.users().threads().list(userId='me',labelIds=label_ids).execute() | |
threads = res.get('threads') | |
return threads | |
def archive_old_alerts(self, age_limit=2): | |
query = 'label:alerts label:inbox older_than:%dd' % age_limit | |
res = self.service.users().messages().list(userId='me', q=query).execute() | |
msgs = res.get('messages') | |
pprint(msgs) | |
body = {'removeLabelIds': ['INBOX'], 'addLabelIds': []} | |
for msg in msgs: | |
msg = self.service.users().messages().modify(userId='me', id=msg['id'], | |
body=body).execute() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment