From 50597fffef788cfd35c21cab9299e1ebe7f78e0b Mon Sep 17 00:00:00 2001 From: Hipstercat Date: Wed, 10 Jun 2020 19:51:19 +0200 Subject: [PATCH] don't mark new posts if no posts, option for logout --- config.json.example | 3 ++- main.py | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/config.json.example b/config.json.example index f459d7a..4a6024c 100644 --- a/config.json.example +++ b/config.json.example @@ -1,5 +1,6 @@ { "username": "testuser", "password": "testpassword", - "webhook": "https://discord.com/webhook/abc/def" + "webhook": "https://discord.com/webhook/abc/def", + "logout": false } \ No newline at end of file diff --git a/main.py b/main.py index ad9359f..70f384c 100644 --- a/main.py +++ b/main.py @@ -12,6 +12,7 @@ with open("config.json", "r") as fp: USERNAME = config["username"] PASSWORD = config["password"] WEBHOOK_URL = config["webhook"] +LOGOUT = config["logout"] def loop(): @@ -30,11 +31,13 @@ def loop(): # Get new posts print("Getting new posts...") + has_newposts = False 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: + has_newposts = True newpost_title = newpost.find(class_="topictitle").contents[0] newpost_link = newpost.find(class_="topictitle")["href"] newpost_username = newpost.find(class_="username").contents[0] @@ -60,15 +63,17 @@ def loop(): } 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) + if has_newposts: + # 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 LOGOUT: + print("Logout...") + br.open(logout_url) if __name__ == "__main__":