diff --git a/config.example.yaml b/config.example.yaml new file mode 100644 index 0000000..e1bfc01 --- /dev/null +++ b/config.example.yaml @@ -0,0 +1,11 @@ +--- +instances: + - mastodon.social + - chaos.social + +tags: + - fediblock + - fediadmin + +own_instance: gts.here.be.dragon +own_bearer_token: AAAAHUNTER2 diff --git a/tagtotoot.py b/tagtotoot.py new file mode 100644 index 0000000..9a834e3 --- /dev/null +++ b/tagtotoot.py @@ -0,0 +1,86 @@ +"""Scrape mastodon tag searches and feed into your search.""" +import yaml +import sys + +import requests + + +def fetch_toots(conf): + toots = set() + for instance in conf.get("instances", []): + for tag in conf.get("tags", []): + try: + curr_posts = requests.get( + f"https://{instance}/tags/{tag}.json" + ).json() + except Exception as e: + print("Got some error fetching toots, continuing...") + print(e) + for post in curr_posts.get("orderedItems", []): + toots.add(post) + return toots + + +def search_for(toots): + for toot in toots: + headers = { + "Authorization": f"Bearer {config.get('own_bearer_token', '')}" + } + print(f"searching for {toot}...") + try: + requests.get(f"https://{config.get('own_instance', '')}/api/v2/search?type=statuses&resolve=true&q={toot}", headers=headers, timeout=30) + except Exception as e: + print(f"Searching for {toot} failed *shrug*") + print(e) + + +def __check_lst_of_str(a): + return bool(a) and isinstance(a, list) and all(isinstance(elem, str) for elem in a) + + +def __check_str(a): + return bool(a) and isinstance(a, str) + + +def __check_nonplain_host(a): + return 1 in [c in a for c in {':', '/'}] + + +if __name__ == "__main__": + try: + config = yaml.full_load(open('config.yaml', encoding="utf-8").read()) + except Exception as e: + print("Couldn't open config.yaml. Please check that" + "it exists and is readable") + print(e) + + if not __check_lst_of_str(config.get('instances', [])): + print("*instances* configuration must be a list of strings") + sys.exit(-1) + if any(__check_nonplain_host(instance) + for instance in config.get('instances', [])): + print("all *instances* must be plain hostnames, no" + "paths or protcol") + sys.exit(-1) + + if not __check_lst_of_str(config.get('tags', [])): + print("*tags* configuration must be a list of strings") + sys.exit(-1) + if any('#' in tag for tag in config.get('tags', '')): + print("*tags* must not contain # charactger") + sys.exit(-1) + + if not __check_str(config.get('own_instance', "")): + print("*own_instance* configuration must be a string") + sys.exit(-1) + if __check_nonplain_host(config.get('own_instance', '')): + print("*own_instance* must only contain your instances hostname, no" + "paths or protocol") + sys.exit(-1) + + if not __check_str(config.get('own_bearer_token', "")): + print("*own_bearer_token* configuration must be a string") + sys.exit(-1) + + toots = fetch_toots(config) + search_for(toots)