|
| 1 | +# |
| 2 | + |
| 3 | +from mastodon import Mastodon |
| 4 | +from pathlib import PosixPath |
| 5 | +import os.path |
| 6 | +import argparse |
| 7 | +from datetime import datetime, timedelta, timezone |
| 8 | +import dateparser |
| 9 | + |
| 10 | +python_gsoc_url = 'https://social.python-gsoc.org/' |
| 11 | +client_id = 'psf-gsoc-bot' |
| 12 | +cred_path = PosixPath('~/.psf-gsoc-bot/clientcred.secret').expanduser() |
| 13 | + |
| 14 | +def check_recent_posts(client: Mastodon, account_name: str, deadline: datetime): |
| 15 | + today = datetime.now(timezone.utc) |
| 16 | + |
| 17 | + week = timedelta(weeks=1) |
| 18 | + initial_window = deadline - week |
| 19 | + |
| 20 | + # take an account name |
| 21 | + account = client.account_lookup(account_name) |
| 22 | + |
| 23 | + # look up the posts, in order. Find the most recent. |
| 24 | + posts = client.account_statuses(account['id'], exclude_reblogs = True, exclude_replies = True, limit=10) |
| 25 | + print(f'Returned {len(posts)} posts.') |
| 26 | + |
| 27 | + for post in posts: |
| 28 | + print(post) |
| 29 | + |
| 30 | + if (len(posts) > 0): |
| 31 | + post = posts[0] |
| 32 | + postdate = post['created_at'] |
| 33 | + print() |
| 34 | + # Record when the most recent post was |
| 35 | + print(f"Most recent post posted on: ({type(postdate)}) {postdate}, contents: {post['content']}") |
| 36 | + |
| 37 | + # check the current date against the most recent deadline date. |
| 38 | + # if the most recent post is not within a week of the most recent deadline, |
| 39 | + if (today > deadline and postdate < initial_window): |
| 40 | + # log that they're behind. |
| 41 | + print(f"{account_name} hasn't posted in over a week.") |
| 42 | + # send them a message and flag them in a list to the mentors & admins |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +def register_app(): |
| 47 | + print("Checking for registration.") |
| 48 | + if not cred_path.exists(): |
| 49 | + print('No registration found.') |
| 50 | + if not cred_path.parent.exists(): |
| 51 | + print(f'Config directory {cred_path.parent} missing.') |
| 52 | + cred_path.parent.mkdir() |
| 53 | + print(f'Registering as {client_id}.') |
| 54 | + Mastodon.create_app( |
| 55 | + client_id, |
| 56 | + api_base_url = python_gsoc_url, |
| 57 | + to_file = cred_path |
| 58 | + ) |
| 59 | + |
| 60 | +register_app() |
| 61 | +cred = cred_path.read_text() |
| 62 | + |
| 63 | +mastodon = Mastodon( |
| 64 | + client_id, |
| 65 | + cred, |
| 66 | + api_base_url = python_gsoc_url |
| 67 | +) |
| 68 | + |
| 69 | +parser = argparse.ArgumentParser(prog='python-gsoc bot', |
| 70 | + description='Checks up on users.') |
| 71 | + |
| 72 | +parser.add_argument('-d', '--deadline') |
| 73 | + |
| 74 | +args = parser.parse_args() |
| 75 | + |
| 76 | +deadline = dateparser.parse(args.deadline) |
| 77 | +if deadline is None: |
| 78 | + print(f'{args.deadline} is not a correctly-formatted datetime.') |
| 79 | + exit(1) |
| 80 | + |
| 81 | +print(f'Deadline: {args.deadline} parsed as {deadline}') |
| 82 | + |
| 83 | +check_recent_posts(mastodon, 'ben@social.python-gsoc.org', deadline) |
| 84 | + |
0 commit comments