#!/usr/bin/env python3 from urllib.parse import urljoin from pyquery import PyQuery as pq from collections import Counter from random import shuffle from dateutil import rrule def get_active_pmwiki_authors(base_url='https://hsmr.cc/'): recent_changes = pq(urljoin(base_url, 'Site/AllRecentChanges')) recent_authors = recent_changes('a[href*=Profiles]') active_authors = Counter( [ author.text for author in recent_authors if not author.text.startswith('Profiles.') # edited profile and not author.text == '?' # no profile / .createlinktext ] ).most_common() # list of tuples (name, appearances) return active_authors def random_active_pmwiki_authors(base_url='https://hsmr.cc/'): random_active_authors = [ author[0] for author in get_active_pmwiki_authors(base_url) ] shuffle(random_active_authors) return random_active_authors def next_weeks(count): mondays = rrule.rrule(rrule.WEEKLY, byweekday=rrule.MO, count=count) sundays = rrule.rrule(rrule.WEEKLY, byweekday=rrule.SU, count=count, dtstart=mondays[0]) return list(zip(mondays, sundays)) users = random_active_pmwiki_authors() for week in next_weeks(len(users) * 3): if len(users) == 0: users = random_active_pmwiki_authors() # lazy. maybe unfair. print( '|| {iso_weeknumber} || {begin} || {end} || [[Profiles/{user}]] ||'.format( iso_weeknumber=week[0].isocalendar()[1], begin=week[0].strftime('%A, %Y-%m-%d'), end=week[1].strftime('%A, %Y-%m-%d'), user=users.pop() ) )