|
| 1 | +# Copyright 2024 Dave O'Connor |
| 2 | +# Derived from code by Joaquin M Lopez Munoz. |
| 3 | +# Distributed under the Boost Software License, Version 1.0. |
| 4 | +# (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | +# http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +import djclick as click |
| 7 | +import logging |
| 8 | +import re |
| 9 | +import warnings |
| 10 | +from datetime import timedelta, datetime |
| 11 | +import html |
| 12 | + |
| 13 | +from dateutil.relativedelta import relativedelta |
| 14 | +from unidecode import unidecode |
| 15 | + |
| 16 | +import requests |
| 17 | + |
| 18 | +from mailing_list.constants import ( |
| 19 | + ML_STATS_URLS, |
| 20 | + LATIN_1_EQUIVS, |
| 21 | + ARG_DATE_REGEX, |
| 22 | + AUTHOR_PATTERN_REGEX, |
| 23 | + DATE_PATTERN_REGEX, |
| 24 | +) |
| 25 | +from mailing_list.models import PostingData |
| 26 | + |
| 27 | +logger = logging.getLogger(__name__) |
| 28 | + |
| 29 | +arg_date_pattern = re.compile(ARG_DATE_REGEX) |
| 30 | +author_pattern = re.compile(AUTHOR_PATTERN_REGEX) |
| 31 | +date_pattern = re.compile(DATE_PATTERN_REGEX) |
| 32 | + |
| 33 | + |
| 34 | +def decode_broken_html(str): |
| 35 | + def latin_1_ord(char): |
| 36 | + n = ord(char) |
| 37 | + return LATIN_1_EQUIVS.get(n, n) |
| 38 | + |
| 39 | + with warnings.catch_warnings(): |
| 40 | + warnings.simplefilter("ignore") |
| 41 | + return unidecode( |
| 42 | + bytearray(map(latin_1_ord, html.unescape(str))).decode("utf-8", "ignore") |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def parse_start_datetime(date_str): |
| 47 | + m = arg_date_pattern.match(date_str) |
| 48 | + if not m: |
| 49 | + raise ValueError("wrong date format") |
| 50 | + logger.info(f"{m=} {m.group(1)=} {m.group(2)=} {m.group(3)=}") |
| 51 | + return datetime( |
| 52 | + int(m.group(3)) if m.group(3) else 1, |
| 53 | + int(m.group(2)) if m.group(2) else 1, |
| 54 | + int(m.group(1)), |
| 55 | + 0, |
| 56 | + 0, |
| 57 | + 0, |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def parse_end_datetime(date_str): |
| 62 | + m = arg_date_pattern.match(date_str) |
| 63 | + if not m: |
| 64 | + raise ValueError("wrong date format") |
| 65 | + logger.info(f"{m=} {m.group(1)=} {m.group(2)=} {m.group(3)=}") |
| 66 | + if m.group(2): |
| 67 | + if m.group(3): |
| 68 | + return datetime( |
| 69 | + int(m.group(3)), int(m.group(2)), int(m.group(1)), 23, 59, 59 |
| 70 | + ) |
| 71 | + else: |
| 72 | + return ( |
| 73 | + datetime(int(m.group(1)), int(m.group(2)), 1) + timedelta(days=31), |
| 74 | + 23, |
| 75 | + 59, |
| 76 | + 59, |
| 77 | + ).replace(day=1) - timedelta(days=1) |
| 78 | + return datetime(int(m.group(1)), 12, 31, 23, 59, 59) |
| 79 | + |
| 80 | + |
| 81 | +def retrieve_authors_from_ml(url, start_date, end_date): |
| 82 | + posts = [] |
| 83 | + logger.info(f"Retrieving data from {url=}.") |
| 84 | + r = requests.get(url) |
| 85 | + if r.status_code == 404: |
| 86 | + return posts |
| 87 | + |
| 88 | + author = None |
| 89 | + for line in r.text.splitlines(): |
| 90 | + author_match = author_pattern.match(line) |
| 91 | + if author_match: |
| 92 | + # needs multiple passes to work |
| 93 | + author = decode_broken_html(author_match.group(1)) |
| 94 | + else: |
| 95 | + date_pattern_match = date_pattern.match(line) |
| 96 | + if author and date_pattern_match: |
| 97 | + post_date = datetime.strptime( |
| 98 | + date_pattern_match.group(1), "%Y-%m-%d %H:%M:%S" |
| 99 | + ) |
| 100 | + if start_date <= post_date and post_date <= end_date: |
| 101 | + posts.append(PostingData(name=author, post_time=post_date)) |
| 102 | + return posts |
| 103 | + |
| 104 | + |
| 105 | +def retrieve_authors(start_date, end_date): |
| 106 | + logger.info(f"retrieve_authors from {start_date=} to {end_date=}") |
| 107 | + start_month = datetime(start_date.year, start_date.month, 1) |
| 108 | + end_month = datetime(end_date.year, end_date.month, 1) |
| 109 | + authors = [] |
| 110 | + while start_month <= end_month: |
| 111 | + for ml in ML_STATS_URLS: |
| 112 | + authors += retrieve_authors_from_ml( |
| 113 | + ml.format(start_month.year, start_month.month), start_date, end_date |
| 114 | + ) |
| 115 | + start_month = start_month + relativedelta(months=+1) |
| 116 | + PostingData.objects.filter( |
| 117 | + post_time__gte=start_date, post_time__lte=end_date |
| 118 | + ).delete() |
| 119 | + PostingData.objects.bulk_create(authors) |
| 120 | + |
| 121 | + |
| 122 | +@click.command() |
| 123 | +@click.option("--start_date", is_flag=False, help="Start Date", default=None) |
| 124 | +@click.option("--end_date", is_flag=False, help="End Date", default=None) |
| 125 | +def command(start_date, end_date): |
| 126 | + logger.info(f"Starting import_ml_counts {start_date=} {end_date=}") |
| 127 | + start_date = ( |
| 128 | + parse_start_datetime(start_date) if start_date else datetime(1998, 11, 11) |
| 129 | + ) |
| 130 | + logger.info(f"{start_date=}") |
| 131 | + end_date = parse_end_datetime(end_date) if end_date else datetime.now() |
| 132 | + logger.info(f"{end_date=}") |
| 133 | + retrieve_authors(start_date, end_date) |
0 commit comments