2019-07-30 19:40:35 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2019-05-30 14:29:49 +02:00
|
|
|
import json
|
2019-07-30 19:40:35 +02:00
|
|
|
from threading import Thread
|
2019-05-30 14:29:49 +02:00
|
|
|
from mastodon import Mastodon, StreamListener
|
|
|
|
|
2019-05-30 22:49:09 +02:00
|
|
|
|
|
|
|
def print_status(status):
|
|
|
|
print("time: " + str(status["created_at"]))
|
|
|
|
print("from: @" + status["account"]["acct"])
|
|
|
|
|
2019-07-30 19:40:35 +02:00
|
|
|
class TootListener(StreamListener):
|
2019-07-30 21:06:25 +02:00
|
|
|
"""
|
|
|
|
Listener class, handling incoming statuses
|
|
|
|
"""
|
2019-07-30 19:40:35 +02:00
|
|
|
def __init__(self, source, drain):
|
2019-07-30 21:06:25 +02:00
|
|
|
self.source = source # Source instance Mastodon object
|
|
|
|
self.drain = drain # Drain instance Mastodon object
|
2019-07-30 19:40:35 +02:00
|
|
|
|
|
|
|
def on_update(self, status):
|
2019-07-30 21:06:25 +02:00
|
|
|
# Just printing some stuff to make it look beautiful in terminal
|
|
|
|
# More for debugging than anything else
|
2019-07-30 19:40:35 +02:00
|
|
|
print("")
|
|
|
|
print("--")
|
|
|
|
print("RECIEVE " + self.source.api_base_url + "")
|
|
|
|
print_status(status)
|
|
|
|
print("SEARCH " + self.drain.api_base_url + "")
|
2019-07-30 21:06:25 +02:00
|
|
|
|
|
|
|
# Searching for the URL of the incoming status in our drain instance
|
2019-07-30 19:40:35 +02:00
|
|
|
try:
|
|
|
|
search = self.drain.search(status.url)
|
|
|
|
for s in search["statuses"]:
|
|
|
|
print_status(s)
|
|
|
|
except:
|
|
|
|
print("failed")
|
2019-07-30 21:06:25 +02:00
|
|
|
|
2019-07-30 19:40:35 +02:00
|
|
|
print("--")
|
|
|
|
|
|
|
|
class HashtagSpreader(Thread):
|
2019-07-30 21:06:25 +02:00
|
|
|
"""
|
|
|
|
Thread class, streaming one hashtag from one the source instance and searching for the post URL in the drain instance
|
|
|
|
"""
|
2019-07-30 19:40:35 +02:00
|
|
|
def __init__(self, source, drain, hashtag):
|
2019-07-30 21:06:25 +02:00
|
|
|
Thread.__init__(self) # Configure the thread
|
2019-07-30 19:40:35 +02:00
|
|
|
|
2019-07-30 21:06:25 +02:00
|
|
|
self.source = source # Source instance Mastodon object
|
|
|
|
self.drain = drain # Drain instance Mastodon object
|
|
|
|
self.hashtag = hashtag # The hashtag (without #) as a string, we want to stream
|
2019-07-30 19:40:35 +02:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
tootListener = TootListener(self.source, self.drain)
|
2019-07-30 21:06:25 +02:00
|
|
|
self.source.stream_hashtag(self.hashtag, tootListener) # Stream our hashtag
|
|
|
|
|
2019-07-30 19:40:35 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
instances = {}
|
|
|
|
threads = []
|
|
|
|
|
|
|
|
with open("config.json", 'r') as f:
|
|
|
|
config = json.load(f)
|
|
|
|
|
|
|
|
if config:
|
2019-07-30 21:06:25 +02:00
|
|
|
# Create Mastodon objects for every instance
|
2019-07-30 19:40:35 +02:00
|
|
|
for instance in config["instances"]:
|
|
|
|
instances[instance] = Mastodon(access_token = config["instances"][instance], api_base_url = "https://" + instance)
|
|
|
|
|
2019-07-30 21:06:25 +02:00
|
|
|
# Create threads for every hashtag of a relation
|
2019-07-30 19:40:35 +02:00
|
|
|
for spread in config["spreads"]:
|
|
|
|
if spread["source"] in instances and spread["drain"] in instances:
|
|
|
|
for hashtag in spread["hashtags"]:
|
|
|
|
threads.append(HashtagSpreader(instances[spread["source"]], instances[spread["drain"]], hashtag))
|
|
|
|
|
2019-07-30 21:06:25 +02:00
|
|
|
# Start all threads
|
2019-07-30 19:40:35 +02:00
|
|
|
for thread in threads:
|
|
|
|
thread.start()
|