-
-
Save tdsmith/ca5766468827280481dcb7ae4e62f876 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from google_auth_oauthlib.flow import Flow\n", | |
"from concurrent.futures import ThreadPoolExecutor\n", | |
"from IPython.display import display, display_html\n", | |
"import ipywidgets\n", | |
"\n", | |
"# Create the flow using the client secrets file from\n", | |
"# https://console.cloud.google.com/apis/credentials\n", | |
"flow = Flow.from_client_secrets_file(\n", | |
" 'credentials.json',\n", | |
" scopes=['https://www.googleapis.com/auth/gmail.labels'],\n", | |
" redirect_uri='urn:ietf:wg:oauth:2.0:oob')\n", | |
"\n", | |
"# Tell the user to go to the authorization URL.\n", | |
"url, _ = flow.authorization_url(prompt='consent')\n", | |
"display_html(f'<a href=\"{url}\" target=\"_blank\" rel=\"noopener\">Click here</a><br />', raw=True)\n", | |
"display_html('<p>Paste code here:</p>', raw=True)\n", | |
"w = ipywidgets.Text()\n", | |
"display(w)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"flow.fetch_token(code=w.value)\n", | |
"session = flow.authorized_session()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"response = session.get(\n", | |
" \"https://www.googleapis.com/gmail/v1/users/me/labels\",\n", | |
")\n", | |
"\n", | |
"labels = response.json()[\"labels\"]\n", | |
"sorted(l[\"name\"] for l in labels)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"keep = [] # names of any labels to retain\n", | |
"kill_list = [l[\"id\"] for l in labels if l[\"type\"] == \"user\" and l[\"name\"] not in keep]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def needful(lid):\n", | |
" print(lid)\n", | |
" r = session.delete(\"https://www.googleapis.com/gmail/v1/users/me/labels/\" + lid)\n", | |
" r.raise_for_status()\n", | |
"\n", | |
"with ThreadPoolExecutor(max_workers=16) as e:\n", | |
" for lid in kill_list:\n", | |
" e.submit(needful, lid)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment