#!/usr/bin/env python3 # https://github.com/Lynnesbian/fediverse-bot-template # Copyright (C) 2019 Lynne (@lynnesbian@fedi.lynnesbian.space) # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published # by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # import Mastodon.py and other modules from mastodon import Mastodon import json, random try: cfg = json.load(open('config.json', 'r')) except: print("Couldn't load config.json. Make sure you run login.py first!\n-----") raise meta = json.load(open('meta.json', 'r')) # log in client = Mastodon( client_id=cfg['client']['id'], client_secret=cfg['client']['secret'], access_token=cfg['secret'], api_base_url=cfg['site']) # make a post! data = json.load(open('data.json', 'r')) def get_object(kind): # fill in the given thought with the necessary objects. # for example, if the kind is "rides", generate a string like "Ride Name 1". num = random.choice([1, 1, 1, 1, 1, 2, 2, 3]) if kind == "rides": return "{} {}".format(random.choice(data['objects']['thrilling-rides'] + data['objects']['gentle-rides']), num) elif kind in ["gentle-rides", "thrilling-rides"]: return "{} {}".format(random.choice(data['objects'][kind]), num) elif kind == "items": return random.choice(list(data['objects']['stalls']['edible'].values()) + list(data['objects']['stalls']['other'].values())) elif kind == "items-edible": return random.choice(list(data['objects']['stalls']['edible'].values())) elif kind == "items-other": return random.choice(list(data['objects']['stalls']['other'].values())) elif kind == "stalls": return "{} {}".format(random.choice(list(data['objects']['stalls']['edible'].keys()) + list(data['objects']['stalls']['other'].keys())), num) elif kind == "item-stall": # return an item and the stall it comes from. stalls_weighted = [] for key in data['objects']['stalls']: for i in range(len(data['objects']['stalls'][key])): stalls_weighted.append(key) stall_type = random.choice(stalls_weighted) choice = random.choice(list(data['objects']['stalls'][stall_type].items()))[::-1] stall = "{} {}".format(choice[1], num) return (choice[0], stall) # generate a list of thought types for a weighted random choice. # for example, if there were two plain thoughts and one rides thought, the list would be: # ['plain', 'plain', 'rides'] thoughts_weighted = [] for key in data['thoughts']: for i in range(len(data['thoughts'][key])): thoughts_weighted.append(key) thought_type = random.choice(thoughts_weighted) # choose a random thought of the type chosen above thought = random.choice(data['thoughts'][thought_type]) if thought_type == "plain": # we don't need to fill in anything here. pass elif thought_type == "item-stall": # special case, get_object returns a tuple to fill in two blanks objects = get_object(thought_type) thought = thought.format(objects[0], objects[1]) else: # just fill in the one blank thought = thought.format(get_object(thought_type)) # post the thought client.status_post(thought)