Merge pull request #1 from clerie/async

Async
This commit is contained in:
Clemens Riese 2019-07-30 21:09:32 +02:00 committed by GitHub
commit b915ed5d4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 39 deletions

View File

@ -1,12 +1,12 @@
# Mastodon Hashtag Spreader
For the #GPN19 my Mastodon instance fem.social wants to get all posts of the hashtag. Most users of this event are connected to the instance chaos.social.
For the #GPN19 my Mastodon instance [fem.social](https://fem.social) wants to get all posts of the hashtag. Most users of this event are connected to the instance [chaos.social](https://chaos.social).
## Our solution
We have a script on one of our servers, streaming all posts of one hashtag from chaos.socail and putting the url of the post to the search entry of our own instance. The url will get fetched by our instace and the resulting post is added to our hashtag timeline.
## Installing
```
## Installation
```bash
pip3 install Mastodon.py
git clone https://github.com/clerie/mastodon-hashtag-spreader.git
cd mastodon-hashtag-spreader/
@ -14,6 +14,16 @@ cp config.json.example config.json
nano config.json
```
Edit config for your needs.
```
```bash
python3 hashtag-spreader.py
```
## Scopes
To get this script work, you need an application token on each instance you want to get connected to.
The needed scopes are depending on the role of the instace.
### Source
* `read:statuses`
### Drain
* `read:search`

View File

@ -1,11 +1,16 @@
{
"source": {
"url": "https://chaos.social",
"token": "",
"hashtag": "gpn19"
"instances": {
"chaos.social": "access-token",
"fem.social": "access-token"
},
"drain": {
"url": "https://fem.social",
"token": ""
}
"spreads": [
{
"source": "chaos.social",
"drain": "fem.social",
"hashtags": [
"gpn19",
"cccamp19"
]
}
]
}

View File

@ -1,4 +1,7 @@
#!/usr/bin/env python3
import json
from threading import Thread
from mastodon import Mastodon, StreamListener
@ -6,35 +9,67 @@ def print_status(status):
print("time: " + str(status["created_at"]))
print("from: @" + status["account"]["acct"])
with open("config.json", 'r') as f:
config = json.load(f)
class TootListener(StreamListener):
"""
Listener class, handling incoming statuses
"""
def __init__(self, source, drain):
self.source = source # Source instance Mastodon object
self.drain = drain # Drain instance Mastodon object
if config:
chaos_social = Mastodon(
access_token = config["source"]["token"],
api_base_url = config["source"]["url"]
)
def on_update(self, status):
# Just printing some stuff to make it look beautiful in terminal
# More for debugging than anything else
print("")
print("--")
print("RECIEVE " + self.source.api_base_url + "")
print_status(status)
print("SEARCH " + self.drain.api_base_url + "")
fem_social = Mastodon(
access_token = config["drain"]["token"],
api_base_url = config["drain"]["url"]
)
# Searching for the URL of the incoming status in our drain instance
try:
search = self.drain.search(status.url)
for s in search["statuses"]:
print_status(s)
except:
print("failed")
class TootListener(StreamListener):
def on_update(self, status):
print("")
print(" -- new status in " + config["source"]["url"] + " -- ")
print_status(status)
#print(status)
print("")
print(" -- search in " + config["drain"]["url"] + " -- ")
try:
search = fem_social.search(status.url)
for s in search["statuses"]:
print_status(s)
except:
print("failed")
print("--")
tootListener = TootListener()
class HashtagSpreader(Thread):
"""
Thread class, streaming one hashtag from one the source instance and searching for the post URL in the drain instance
"""
def __init__(self, source, drain, hashtag):
Thread.__init__(self) # Configure the thread
chaos_social.stream_hashtag(config["source"]["hashtag"], tootListener, run_async=False, timeout=300, reconnect_async=False, reconnect_async_wait_sec=5)
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
def run(self):
tootListener = TootListener(self.source, self.drain)
self.source.stream_hashtag(self.hashtag, tootListener) # Stream our hashtag
if __name__ == "__main__":
instances = {}
threads = []
with open("config.json", 'r') as f:
config = json.load(f)
if config:
# Create Mastodon objects for every instance
for instance in config["instances"]:
instances[instance] = Mastodon(access_token = config["instances"][instance], api_base_url = "https://" + instance)
# Create threads for every hashtag of a relation
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))
# Start all threads
for thread in threads:
thread.start()