A small selection of Mastodon-Bots
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.5 KiB

#!/usr/bin/python3
import urllib.request
from bs4 import BeautifulSoup
import time as _time
import pickle
from mastodon import Mastodon
import os
import ssl
os.chdir(os.path.dirname(os.path.abspath(__file__)))
mastodon = Mastodon(
# File for "Your access token", can be created unter "Settings" -> "Development" -> "Your applications"
access_token='stoerungsmelder.secret',
api_base_url='https://botsin.space' # URL that the bot is hosted on
)
lastTootTime = 0
class tweet:
def time(self):
return int(self.tweet.find('span', attrs={'class', '_timestamp'})['data-time'])
def content(self):
out = self.tweet.find('p', attrs={'class', 'tweet-text'})
try:
out.a.decompose()
except:
pass
return out.text
def media(self):
media = self.tweet.find_all(
'div', attrs={'class', 'AdaptiveMedia-photoContainer'})
for i in range(len(media)):
media[i] = media[i]['data-image-url']
return media
def success(self):
return True if type(self.tweet) == type(BeautifulSoup('<b/>', features="html.parser").b) else False
def __init__(self, body):
self.tweet = body
def tweetTooter(thisTweet):
global lastTootTime
if thisTweet.success() and lastTootTime < thisTweet.time():
tweetTooter(tweet(thisTweet.tweet.find_next(
'div', attrs={'class': 'tweet'})))
if thisTweet.media():
mediaPost = []
for i in range(len(thisTweet.media())):
mediaPost.append(mastodon.media_post(urllib.request.urlopen(
thisTweet.media()[i]).read(), "image/jpeg"))
mastodon.status_post(thisTweet.content(), media_ids=mediaPost)
else:
mastodon.status_post(thisTweet.content())
lastTootTime = thisTweet.time()
else:
return
if __name__ == "__main__":
URL = 'https://twitter.com/stoerungsmelder'
ssl._create_default_https_context = ssl._create_unverified_context
twitter = urllib.request.urlopen(URL).read().decode('utf-8').rstrip()
parsed_html = BeautifulSoup(twitter, features="html.parser")
firstTweet = tweet(parsed_html.body.find('div', attrs={'class': 'tweet'}))
try:
lastTootTime = pickle.load(open("./lastTootTime.log", 'rb'))
except:
print("Time-file does not exist. Creating one.")
lastTootTime = _time.time()
pickle.dump(lastTootTime, open("./lastTootTime.log", 'wb'))
tweetTooter(firstTweet)
pickle.dump(lastTootTime, open("./lastTootTime.log", 'wb'))