#!/usr/bin/env python3
# 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.
from mastodon import Mastodon, StreamListener
from bs4 import BeautifulSoup
# use multiprocessing to run multiple reply listener threads at once
# this means we can handle more than one reply at a time!
from multiprocessing import Pool
import os, random, re, json, re, sys
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'))
print("Logging in...")
client = Mastodon(
client_id=cfg['client']['id'],
client_secret=cfg['client']['secret'],
access_token=cfg['secret'],
api_base_url=cfg['site'])
# extract the handle (the @username@instance part)
handle = "@{}@{}".format(client.account_verify_credentials()['username'], re.match("https://([^/]*)/?", cfg['site']).group(1)).lower()
# convert the text of the status to plain text
# this part's a bit of a mess!!
def extract_toot(toot):
toot = toot.replace("'", "'") #convert HTML stuff to normal stuff
toot = toot.replace(""", '"') #ditto
soup = BeautifulSoup(toot, "html.parser")
for lb in soup.select("br"): #replace
with linebreak
lb.insert_after("\n")
lb.decompose()
for p in soup.select("p"): #ditto for
p.insert_after("\n") p.unwrap() for ht in soup.select("a.hashtag"): #make hashtags no longer links, just text ht.unwrap() for link in soup.select("a"): #convert