added config.json to make changing api base url easier

This commit is contained in:
Lynne 2018-10-14 16:58:58 +10:00
parent 2858e7c960
commit e4d661caa6
3 changed files with 17 additions and 13 deletions

6
gen.py
View File

@ -4,7 +4,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
from mastodon import Mastodon from mastodon import Mastodon
import argparse, sys, traceback import argparse, sys, traceback, json
import create import create
parser = argparse.ArgumentParser(description='Generate and post a toot.') parser = argparse.ArgumentParser(description='Generate and post a toot.')
@ -15,12 +15,12 @@ parser.add_argument('-s', '--simulate', dest='simulate', action='store_true',
args = parser.parse_args() args = parser.parse_args()
api_base_url = "https://botsin.space" #todo: this shouldn't be hardcoded cfg = json.load(open('config.json', 'r'))
client = Mastodon( client = Mastodon(
client_id="clientcred.secret", client_id="clientcred.secret",
access_token="usercred.secret", access_token="usercred.secret",
api_base_url=api_base_url) api_base_url=cfg['site'])
toot = create.make_toot() toot = create.make_toot()
if not args.simulate: if not args.simulate:

16
main.py Normal file → Executable file
View File

@ -4,23 +4,22 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
from mastodon import Mastodon from mastodon import Mastodon
from getpass import getpass
from os import path from os import path
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import shutil, os, sqlite3, signal, sys import shutil, os, sqlite3, signal, sys, json
# import re # import re
api_base_url = "https://botsin.space"
scopes = ["read:statuses", "read:accounts", "read:follows", "write:statuses"] scopes = ["read:statuses", "read:accounts", "read:follows", "write:statuses"]
cfg = json.load(open('config.json', 'r'))
if not path.exists("clientcred.secret"): if not path.exists("clientcred.secret"):
print("No clientcred.secret, registering application") print("No clientcred.secret, registering application")
Mastodon.create_app("lynnesbian_mastodon_ebooks", api_base_url=api_base_url, to_file="clientcred.secret", scopes=scopes, website="https://github.com/Lynnesbian/mastodon-ebooks") Mastodon.create_app("lynnesbian_mastodon_ebooks", api_base_url=cfg['site'], to_file="clientcred.secret", scopes=scopes, website="https://github.com/Lynnesbian/mastodon-ebooks")
if not path.exists("usercred.secret"): if not path.exists("usercred.secret"):
print("No usercred.secret, registering application") print("No usercred.secret, registering application")
client = Mastodon(client_id="clientcred.secret", api_base_url=api_base_url) client = Mastodon(client_id="clientcred.secret", api_base_url=cfg['site'])
print("Visit this url:") print("Visit this url:")
print(client.auth_request_url(scopes=scopes)) print(client.auth_request_url(scopes=scopes))
client.log_in(code=input("Secret: "), to_file="usercred.secret", scopes=scopes) client.log_in(code=input("Secret: "), to_file="usercred.secret", scopes=scopes)
@ -68,7 +67,10 @@ def parse_toot(toot):
# next up: store this and patch markovify to take it # next up: store this and patch markovify to take it
# return {"text": text, "mentions": mentions, "links": links} # return {"text": text, "mentions": mentions, "links": links}
# it's 4am though so we're not doing that now, but i still want the parser updates # it's 4am though so we're not doing that now, but i still want the parser updates
return "\0".join(list(text)) #todo: we split above and join now, which is dumb, but i don't wanna mess with the map code bc i don't understand it uwu
text = "\n".join(list(text))
text = text.replace("'", "'")
return text
def get_toots(client, id, since_id): def get_toots(client, id, since_id):
i = 0 i = 0
@ -95,7 +97,7 @@ def get_toots(client, id, since_id):
client = Mastodon( client = Mastodon(
client_id="clientcred.secret", client_id="clientcred.secret",
access_token="usercred.secret", access_token="usercred.secret",
api_base_url=api_base_url) api_base_url=cfg['site'])
me = client.account_verify_credentials() me = client.account_verify_credentials()
following = client.account_following(me.id) following = client.account_following(me.id)

View File

@ -4,15 +4,17 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
import mastodon import mastodon
import os, random, re import os, random, re, json
import create import create
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
api_base_url = "https://botsin.space" cfg = json.load(open('config.json', 'r'))
api_base_url = "https://knzk.me"
client = mastodon.Mastodon( client = mastodon.Mastodon(
client_id="clientcred.secret", client_id="clientcred.secret",
access_token="usercred.secret", access_token="usercred.secret",
api_base_url=api_base_url) api_base_url=cfg['site'])
def extract_toot(toot): def extract_toot(toot):
#copied from main.py, see there for comments #copied from main.py, see there for comments