commit 8efb4b85214ac2fbd46b26e452e9e1c0ad9f7928 Author: Jess 3Jane Date: Sun Feb 18 15:38:21 2018 -0500 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..a86ea9f --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +hey look it's an ebooks bot + +python3 + +install the requirements with `sudo pip3 install -r requirements` + +make a bot (probably on bots in space) and follow the target accounts + +run `python3 main.py` to login and scrape + +run `pyhton3 gen.py` to make a toot + +this will toot once every three hours after until it crashes + +maybe the code won't be total jenk eventually but \shrug diff --git a/gen.py b/gen.py new file mode 100644 index 0000000..3c3ac0f --- /dev/null +++ b/gen.py @@ -0,0 +1,23 @@ +import markovify +import json +from mastodon import Mastodon + +with open("settings.json") as fp: + settings = json.load(fp) + +api_base_url = settings.setdefault("api_base_url", "https://botsin.space") + +client = Mastodon( + client_id="clientcred.secret", + access_token="usercred.secret", + api_base_url=api_base_url) + +with open("corpus.txt") as fp: + model = markovify.NewlineText(fp.read()) + +print("Running...") +while True: + print("tooting") + client.toot(model.make_sentence().replace(chr(31), "\n")) + print("sleeping") + time.sleep(60*60*3) diff --git a/main.py b/main.py new file mode 100644 index 0000000..71677af --- /dev/null +++ b/main.py @@ -0,0 +1,56 @@ +from mastodon import Mastodon +from getpass import getpass +from os import path +import json +import re + +api_base_url = "https://botsin.space" + +if not path.exists("clientcred.secret"): + print("No clientcred.secret, registering application") + Mastodon.create_app("ebooks", api_base_url=api_base_url, to_file="clientcred.secret") + +if not path.exists("usercred.secret"): + print("No usercred.secret, registering application") + email = input("Email: ") + password = getpass("Password: ") + client = Mastodon(client_id="clientcred.secret", api_base_url=api_base_url) + client.log_in(email, password, to_file="usercred.secret") + +def remove_tags(text): + text = text.strip().replace("
", chr(31)) + TAG_RE = re.compile(r'<[^>]+>') + next_re = TAG_RE.sub('', text) + last = re.sub(r"(?:\@|https?\"//)\S+", "", next_re) + if len(last) > 0: + if last[0] == " ": + last = last[1:] + else: + last = "" + return last + +def get_toots(client, id): + i = 0 + toots = client.account_statuses(id) + while toots is not None: + for toot in toots: + if toot.spoiler_text == "" and toot.reblog is None and toot.visibility in ["public", "unlisted"]: + yield remove_tags(toot.content) + toots = client.fetch_next(toots) + i += 1 + if i%10 == 0: + print(i) + +client = Mastodon( + client_id="clientcred.secret", + access_token="usercred.secret", + api_base_url=api_base_url) + +me = client.account_verify_credentials() +following = client.account_following(me.id) + +with open("corpus.txt", "w+") as fp: + for f in following: + print(f.username) + for t in get_toots(client, f.id): + fp.write(t + "\n") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..349fe0c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Mastodon.py==1.2.1 +markovify==0.7.1