From 6aee14e4429af38cf5b37f5c8f07cb6531322b08 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Dec 26 2021 16:45:44 +0000 Subject: starting on this --- diff --git a/move-between-categories.py b/move-between-categories.py new file mode 100755 index 0000000..185ac75 --- /dev/null +++ b/move-between-categories.py @@ -0,0 +1,102 @@ +#!/usr/bin/python3 -u + +from os import write +import sys +import itertools +import json +import re +from pprint import pprint + +from apiutils import getconfig, api + +TESTING = False + + +def get_category_info(config): + categorynames = {} + categoryslugs = {} + + categorylist = api(config, f"site.json")['categories'] + + for cat in sorted(categorylist, key=lambda item: item['id']): + categorynames[cat['id']] = cat['name'] + categoryslugs[cat['id']] = cat['slug'] + print(f"{cat['id']:5} {cat['name']}") + + print(categoryslugs.keys()) + return(categorynames, categoryslugs) + + +def get_fromlist(categorynames, categoryslugs): + pass + + +def get_destination(categorynames, categoryslugs): + target = input("Enter destination category by number: ").strip() + if not target.isnumeric(): + print("🔣 That's not a number!") + sys.exit(1) + destination = int(target) + print( + f"Selected category \"{categorynames[destination]}\" ({categoryslugs[destination]})") + print("⚠️ This might be annoying to reverse if it's wrong.") + confirm = input("Type the category slug to continue: ").strip() + if confirm != categoryslugs[destination]: + print("💣 That doesn't match.") + sys.exit(1) + + print( + f"✅ Ok! We'll move everything to 🗃️ \"{categorynames[destination]}\"") + + +def move_between_categories(config, fromlist, destination): + """ + + new_category = {"category_id": DISCOURSE_ACCEPTED_CATEGORY} + move_results = requests.put( + f"https://{DISCOURSE}/t/{topic['slug']}/{topic['id']}", json=new_category, headers=HEADERS) + + for category in categorylist: + + sys.stdout.write('🗃️ ') + print(f"{category['slug']}", end='') + + if category['slug'] in SKIPLIST: + sys.stdout.write('🦘') + continue + + # note "none" in the API call -- otherwise, + # descends to subcategories + next_query = f"c/{category['id']}/none.json" + + while True: + topic_page = api(config, next_query)['topic_list'] + topics = topic_page['topics'] + sys.stdout.write('📄') + for topic in topics: + sys.stdout.write('🏷️ ') + tags = set(topic['tags']) + tags.add(category['slug']) + api(config, f"t/{topic['slug']}/{topic['id']}", + method="put", json={"tags": list(tags)}) + # now, go to the next page, or stop if there isn't one + # We want the json version of the page, and we have to + # sneak that in before the query'?' + try: + next_query = f"{topic_page['more_topics_url'].replace('?','.json?')}" + except KeyError: + break + """ + print("🔚") + + +def main(): + config = getconfig() + (categorynames, categoryslugs) = get_category_info(config) + destination = get_destination(categorynames, categoryslugs) + fromlist = get_fromlist(categorynames, categoryslugs) + move_between_categories(config, fromlist, destination) + + +if __name__ == "__main__": + main()