initial commit
This commit is contained in:
commit
8efb4b8521
4 changed files with 96 additions and 0 deletions
15
README.md
Normal file
15
README.md
Normal file
|
@ -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
|
23
gen.py
Normal file
23
gen.py
Normal file
|
@ -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)
|
56
main.py
Normal file
56
main.py
Normal file
|
@ -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("<br>", 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")
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Mastodon.py==1.2.1
|
||||||
|
markovify==0.7.1
|
Loading…
Reference in a new issue