Skip to content
Snippets Groups Projects
Commit 141f25d0 authored by ZettaScript's avatar ZettaScript
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #6164 failed
config.json
data.ubjson
__pycache__
This diff is collapsed.
# Discourse moderator bot
## Installation
Install package `python3`.
Install Python dependencies:
sudo pip3 install --upgrade pydiscourse ubjson
Create `config.json` file:
{
"server": "https://discourse.example.net",
"username": "robocop",
"key": "ac3d7ed4b61b8ab79f1877c614bea4e18691e94170f6e22551edc0a209c033a7"
}
Note: this example key is random; don't try it, it won't work.
Run it!
python3 modbot.py
#!/usr/bin/env python3
"""
CopyLeft 2019 Pascal Engélibert
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.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os, time, json
import pydiscourse, ubjson
DATAFILE = "data.ubjson"
CONFIGFILE = "config.json"
SERVER = "https://forum.monnaie-libre.fr"
USERNAME = "modbot"
KEY = ""
topics_seen = {}
to_time = lambda t: time.mktime(time.strptime(t.split(".")[0], "%Y-%m-%dT%H:%M:%S"))
class Topic:
def __init__(self, data, post_seen_latest=-1):
self.data = data
self.post_seen_latest = post_seen_latest
self.id = data["id"]
self.posts_count = data["posts_count"]
self.bumped_at = to_time(data["bumped_at"])
def export(self):
return {
"data": self.data,
"post_seen_latest": self.post_seen_latest
}
if __name__ == "__main__":
print("Loading config...")
configfile = open(CONFIGFILE, "r+")
config = json.load(configfile)
configfile.close()
SERVER = config.setdefault("server", SERVER)
USERNAME = config.setdefault("username", USERNAME)
KEY = config.setdefault("key", KEY)
configfile = open(CONFIGFILE, "w")
json.dump(config, configfile, indent=1)
configfile.close()
print("Loading data...")
if os.path.isfile(DATAFILE):
datafile = open(DATAFILE, "rb")
topics_seen = {topic_seen["data"]["id"]: Topic(topic_seen["data"], topic_seen["post_seen_latest"]) for topic_seen in ubjson.load(datafile)["topics_seen"]}
datafile.close()
client = pydiscourse.DiscourseClient(SERVER, USERNAME, KEY)
print("Getting latest topics...")
topics_latest = client.latest_topics()
for topic in topics_latest["topic_list"]["topics"]:
if not topic["id"] in topics_seen \
or topics_seen[topic["id"]].bumped_at < to_time(topic["bumped_at"]) \
or topics_seen[topic["id"]].posts_count < topic["posts_count"]:
topics_seen[topic["id"]] = Topic(topic)
print("Getting posts in topic {}".format(topic["id"]))
topic_posts = client.topic_posts(topic["id"])
for post in topic_posts["post_stream"]["posts"]:
if post["id"] > topics_seen[topic["id"]].post_seen_latest:
print("Unseen post: {}".format(post["id"]))
topics_seen[topic["id"]].post_seen_latest = post["id"]
print("Saving data...")
datafile = open(DATAFILE, "wb")
ubjson.dump({
"topics_seen": [topics_seen[topic_id].export() for topic_id in topics_seen]
}, datafile)
datafile.close()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment