Fix mailman-sar script

Signed-off-by: Lenka Segura <lsegura@redhat.com>
This commit is contained in:
Lenka Segura 2024-05-08 10:49:43 +02:00
parent ae7b7db942
commit 14c37e9e8e

View file

@ -6,46 +6,38 @@ Extract all emails from a selected address and prints them in JSON to the
standard output. standard output.
""" """
from __future__ import absolute_import, unicode_literals, print_function
import argparse import argparse
import json import json
import logging import logging
import os import os
import sys import sys
from configparser import ConfigParser
import requests import requests
from six.moves.urllib.parse import urljoin
config = ConfigParser()
config.read("/etc/mailman.cfg")
username = config.get("webservice", "admin_user")
userpass = config.get("webservice", "admin_pass")
ENV_EMAIL = "SAR_EMAIL" ENV_EMAIL = "SAR_EMAIL"
HYPERKITTY_INSTANCE = "http://localhost/archives/" HYPERKITTY_INSTANCE = "http://localhost/archives/"
MAILMAN_INSTANCE = "http://localhost:8001/" MAILMAN_INSTANCE = "http://localhost:8001/"
MAILMAN_AUTH = ("restadmin", "restpass") MAILMAN_AUTH = (username, userpass)
log = logging.getLogger() log = logging.getLogger()
def get_emails(address): def get_emails(address):
url = urljoin(HYPERKITTY_INSTANCE, "api/sender/{}/emails/".format(address)) url = urljoin(HYPERKITTY_INSTANCE, "api/sender/{}/emails/".format(address))
result = {"next": url}
count = None
email_urls = [] email_urls = []
while result.get("next"): response = requests.get(url)
url = result["next"] if response.status_code >= 300:
response = requests.get(url) log.error("Could not get URL %s: %d %s",
if response.status_code >= 300: url, response.status_code, response.reason)
log.error("Could not get URL %s: %d %s", raise ConnectionError
url, response.status_code, response.reason) result = response.json()
break email_urls.extend([e["url"] for e in result])
result = response.json()
if count is None:
count = result["count"]
email_urls.extend([e["url"] for e in result["results"]])
if count != len(email_urls):
log.error("Mismatch in the number of emails: got %s but there are "
"%s in total.", len(email_urls), count)
raise ValueError
emails = [] emails = []
for url in email_urls: for url in email_urls:
response = requests.get(url) response = requests.get(url)