mailstats/Playground.ipynb

365 lines
9.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "0bda9dff-b37e-4dd1-b842-24f8947937cf",
"metadata": {},
"outputs": [],
"source": [
"from imapclient import IMAPClient\n",
"import getpass"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1a95dfc6-747a-4e9d-ba55-061c3e1e53f6",
"metadata": {},
"outputs": [],
"source": [
"host = \"imap.mailbox.org\"\n",
"port = 993"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "92854690-2cc0-45fa-a609-1a3dfbe0d2bf",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
" ········\n"
]
}
],
"source": [
"user = \"hallo@pbrinkmeier.de\"\n",
"pw = getpass.getpass()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "dfb17802-c27e-4e82-af2a-b9ac5383071e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"b'[CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS BINARY MOVE SNIPPET=FUZZY PREVIEW=FUZZY PREVIEW STATUS=SIZE SAVEDATE SEARCH=X-MIMEPART XDOVECOT ACL UIDPLUS LITERAL+ NOTIFY SPECIAL-USE FILTER=SIEVE COMPRESS=DEFLATE XAPPLEPUSHSERVICE QUOTA ACL RIGHTS=texk] Logged in'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"server = IMAPClient(host, use_uid=True)\n",
"server.login(user, pw)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "2e1b4ab9-96bb-4cad-9420-2eedcb1c0372",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Junk',\n",
" 'Trash',\n",
" 'Trash/Amazon 1',\n",
" 'Drafts',\n",
" 'Sent',\n",
" 'INBOX/Scalable',\n",
" 'INBOX/Channable',\n",
" 'INBOX/Channable/Kamernet',\n",
" 'INBOX/EasyCare',\n",
" 'INBOX/Sonstiges',\n",
" 'INBOX/TK',\n",
" 'INBOX/Spam@',\n",
" 'INBOX/Onlineshopping',\n",
" 'INBOX/Onlineshopping/Momox',\n",
" 'INBOX/Onlineshopping/E-Bay Kleinanzeigen',\n",
" 'INBOX/Onlineshopping/E-Shop Direct',\n",
" 'INBOX/Onlineshopping/Gamefound',\n",
" 'INBOX/Onlineshopping/Buffalo',\n",
" 'INBOX/Onlineshopping/Fantasywelt',\n",
" 'INBOX/Onlineshopping/Amazon',\n",
" 'INBOX/Privat',\n",
" 'INBOX/Uni',\n",
" 'INBOX/Uni/Tutorien',\n",
" 'INBOX/Uni/Tutorien/PP',\n",
" 'INBOX/Uni/Fachschaft',\n",
" 'INBOX/Dienste',\n",
" 'INBOX/Dienste/Contabo',\n",
" 'INBOX/Dienste/IONOS',\n",
" 'INBOX/Dienste/Steam',\n",
" 'INBOX/Dienste/paydirekt',\n",
" 'INBOX/Dienste/Bahn',\n",
" 'INBOX/Dienste/Strato',\n",
" 'INBOX/Dienste/Booking.com',\n",
" 'INBOX/Dienste/Pakete',\n",
" 'INBOX/Dienste/PayPal',\n",
" 'INBOX/Dienste/Mailbox',\n",
" 'INBOX']"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[name for (meta, parent, name) in server.list_folders()]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "070194da-6cf0-4717-a55d-0e8c4338a897",
"metadata": {},
"outputs": [],
"source": [
"select_info = server.select_folder('INBOX/Uni/Fachschaft')"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "671556ba-5f40-440e-98de-313a47e8274a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{b'PERMANENTFLAGS': (b'\\\\Answered',\n",
" b'\\\\Flagged',\n",
" b'\\\\Deleted',\n",
" b'\\\\Seen',\n",
" b'\\\\Draft',\n",
" b'NonJunk',\n",
" b'$Forwarded',\n",
" b'$social',\n",
" b'$MailFlagBit1',\n",
" b'$NotJunk',\n",
" b'NotJunk',\n",
" b'$ITipAnalyzed',\n",
" b'\\\\*'),\n",
" b'CLOSED': [b''],\n",
" b'FLAGS': (b'\\\\Answered',\n",
" b'\\\\Flagged',\n",
" b'\\\\Deleted',\n",
" b'\\\\Seen',\n",
" b'\\\\Draft',\n",
" b'NonJunk',\n",
" b'$Forwarded',\n",
" b'$social',\n",
" b'$MailFlagBit1',\n",
" b'$NotJunk',\n",
" b'NotJunk',\n",
" b'$ITipAnalyzed'),\n",
" b'EXISTS': 7921,\n",
" b'RECENT': 0,\n",
" b'UIDVALIDITY': 1592410848,\n",
" b'UIDNEXT': 7929,\n",
" b'HIGHESTMODSEQ': 16372,\n",
" b'READ-WRITE': True}"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"select_info"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "8fc437cd-f1d4-4e26-8f22-b24bc2f627ef",
"metadata": {},
"outputs": [],
"source": [
"message_ids = server.search()\n",
"messages = list(data for id, data in server.fetch(message_ids, ['ENVELOPE']).items())"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "299ed420-ca62-4ac9-95f4-f0cd0afed205",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7921"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(messages)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "b1a7a03b-fed0-40e1-be2e-2f4ed2d09fe5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{b'SEQ': 1,\n",
" b'ENVELOPE': Envelope(date=datetime.datetime(2020, 6, 18, 8, 46, 27), subject=b'[fsmi-aktiv] Protokoll vom Fachschaftsrat am 2020-05-13', from_=(Address(name=b'FSMI-Homepage', route=None, mailbox=b'webmaster', host=b'fsmi.uni-karlsruhe.de'),), sender=(Address(name=b'Mathe-Info-aktiv', route=None, mailbox=b'mathe-info-aktiv-bounces', host=b'fsmi.uni-karlsruhe.de'),), reply_to=(Address(name=b'FSMI-Homepage', route=None, mailbox=b'webmaster', host=b'fsmi.uni-karlsruhe.de'),), to=(Address(name=None, route=None, mailbox=b'aktiv', host=b'fsmi.uni-karlsruhe.de'),), cc=None, bcc=None, in_reply_to=None, message_id=b'<1592462787.262119.17324.nullmailer@fsmi-www.fsmi.uni-karlsruhe.de>')}"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages[0]"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "a93b166e-f8cf-4fe4-be1c-ba4a3a3d48e8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7827"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tos = [list(message[b'ENVELOPE'].to) for message in messages if message[b'ENVELOPE'].to]\n",
"len(tos)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"id": "68d34864-3b30-4d96-8681-1181e56e230d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11451"
]
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"flat_tos = [f\"{to.mailbox.decode('utf-8')}@{to.host.decode('utf-8')}\" for tos_ in tos for to in tos_ if to.mailbox and to.host]\n",
"len(flat_tos)"
]
},
{
"cell_type": "code",
"execution_count": 109,
"id": "0ac4e032-b665-4c38-8615-f4168356f307",
"metadata": {},
"outputs": [],
"source": [
"from collections import Counter"
]
},
{
"cell_type": "code",
"execution_count": 124,
"id": "2c91b254-b9f5-4448-b5ea-a18a034e827e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1278 aktiv@fsmi.uni-karlsruhe.de\n",
"1126 mathe-info@fsmi.uni-karlsruhe.de\n",
"1077 info@fsmi.uni-karlsruhe.de\n",
" 545 mathe@fsmi.uni-karlsruhe.de\n",
" 397 top@fsmi.uni-karlsruhe.de\n",
" 153 fsk@asta-kit.de\n",
" 131 webmaster@fsmi.uni-karlsruhe.de\n",
" 120 intern@fsmi.uni-karlsruhe.de\n",
" 118 paul.brinkmeier@fsmi.uni-karlsruhe.de\n",
" 113 offtopic@fsmi.uni-karlsruhe.de\n",
" 105 stupal@asta-kit.de\n",
" 92 info-klausuren@fsmi.uni-karlsruhe.de\n",
" 88 mitarbeiter@ira.uka.de\n",
" 81 fsr-einladung@fsmi.uni-karlsruhe.de\n",
" 65 fachschaft@physik.kit.edu\n",
" 65 Mitarbeiter_Mathe@mail.math.kit.edu\n",
" 60 fschembio@lists.kit.edu\n",
" 60 info@fs-etec.kit.edu\n",
" 60 info@fachschaft.org\n",
" 60 hallo@pbrinkmeier.de\n"
]
}
],
"source": [
"tos_count = list(Counter(flat_tos).items())\n",
"tos_count.sort(key=lambda x: -x[1])\n",
"\n",
"for to, count in tos_count[:20]:\n",
" print(f\"{count:4} {to}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e225426-5446-45a9-b7e9-11c15bedb54b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python3 - Python",
"language": "python",
"name": "ipython_python"
},
"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.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}