From a888a74c4563cb3a5fd1d4311de00c06bc4f167c Mon Sep 17 00:00:00 2001 From: Hipstercat Date: Wed, 10 Jun 2020 19:38:58 +0200 Subject: [PATCH] initial --- .gitignore | 3 ++ config.json.example | 5 +++ main.py | 81 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 .gitignore create mode 100644 config.json.example create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d589f38 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +config.json +.idea/ +venv/ \ No newline at end of file diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..f459d7a --- /dev/null +++ b/config.json.example @@ -0,0 +1,5 @@ +{ + "username": "testuser", + "password": "testpassword", + "webhook": "https://discord.com/webhook/abc/def" +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..d62c097 --- /dev/null +++ b/main.py @@ -0,0 +1,81 @@ +import requests +from bs4 import BeautifulSoup +import mechanize +import time +import datetime +import traceback +import json + +with open("config.json", "r") as fp: + config = json.load(fp) + +USERNAME = config["username"] +PASSWORD = config["password"] +WEBHOOK_URL = config["webhook"] + + +def loop(): + login_url = "https://www.giantswd.org/forum/ucp.php?mode=login" + logout_url = "https://www.giantswd.org/forum/ucp.php?mode=logout" + newposts_url = "https://www.giantswd.org/forum/search.php?search_id=unreadposts" + + # Login + print("Logging in...") + br = mechanize.Browser() + br.open(login_url) + br.select_form(id="login") + br["username"] = USERNAME + br["password"] = PASSWORD + br.submit() + + # Get new posts + print("Getting new posts...") + newposts_resp = br.open(newposts_url) + newposts_html = newposts_resp.get_data() + bs = BeautifulSoup(newposts_html, features="html5lib") + all_newposts = bs.find_all("dl", attrs={"class": "topic_unread"}) + for newpost in all_newposts: + newpost_title = newpost.find(class_="topictitle").contents[0] + newpost_link = newpost.find(class_="topictitle")["href"] + newpost_username = newpost.find(class_="username").contents[0] + newpost_link_real = "https://www.giantswd.org/forum/%s" % (newpost_link[2:]) + + print("%s has created a new post %s (%s)" % (newpost_username, newpost_title, newpost_link_real)) + + webhook_data = { + "embeds": [ + { + "title": "A new message has been posted!", + "description": "**%s** has posted a new message: [**%s**](%s)" % (newpost_username, newpost_title, newpost_link_real), + "color": 55039, + "author": { + "name": "GiantsWD forums" + }, + "footer": { + "text": "" + }, + "timestamp": datetime.datetime.now().isoformat() + } + ] + } + requests.post(WEBHOOK_URL, json=webhook_data) + + # Mark all read + print("Mark all read...") + a = bs.find("a", class_="mark-read") + mark_read_url = a["href"] + br.open(mark_read_url) + + # Logout + print("Logout...") + br.open(logout_url) + + +if __name__ == "__main__": + while True: + try: + loop() + except: + traceback.print_exc() + print("Sleeping...") + time.sleep(3600)