fix timestamp, don't login if already logged in, decreased check time to 10 mins

This commit is contained in:
Amazed 2020-06-11 17:10:19 +02:00
parent 50597fffef
commit 5720225091

42
main.py
View File

@ -13,26 +13,38 @@ USERNAME = config["username"]
PASSWORD = config["password"] PASSWORD = config["password"]
WEBHOOK_URL = config["webhook"] WEBHOOK_URL = config["webhook"]
LOGOUT = config["logout"] LOGOUT = config["logout"]
BROWSER = mechanize.Browser()
def loop(): def login():
login_url = "https://www.giantswd.org/forum/ucp.php?mode=login" 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 # Login
print("Logging in...") print("Logging in...")
br = mechanize.Browser() BROWSER.open(login_url)
br.open(login_url) BROWSER.select_form(id="login")
br.select_form(id="login") BROWSER["username"] = USERNAME
br["username"] = USERNAME BROWSER["password"] = PASSWORD
br["password"] = PASSWORD BROWSER.submit()
br.submit()
def is_logged_in():
if not BROWSER.response():
return False
return b'<li id="username_logged_in" class="rightside " data-skip-responsive="true">\n\t\t\t\t\t\t<div class="header-profile dropdown-container">\n\t\t\t\t<a href="./ucp.php" class="header-avatar dropdown-trigger"> <span class="username">' + USERNAME.encode("utf8") in BROWSER.response().get_data()
def loop():
logout_url = "https://www.giantswd.org/forum/ucp.php?mode=logout"
newposts_url = "https://www.giantswd.org/forum/search.php?search_id=unreadposts"
if not is_logged_in():
login()
# Get new posts # Get new posts
print("Getting new posts...") print("Getting new posts...")
has_newposts = False has_newposts = False
newposts_resp = br.open(newposts_url) newposts_resp = BROWSER.open(newposts_url)
newposts_html = newposts_resp.get_data() newposts_html = newposts_resp.get_data()
bs = BeautifulSoup(newposts_html, features="html5lib") bs = BeautifulSoup(newposts_html, features="html5lib")
all_newposts = bs.find_all("dl", attrs={"class": "topic_unread"}) all_newposts = bs.find_all("dl", attrs={"class": "topic_unread"})
@ -49,7 +61,7 @@ def loop():
"embeds": [ "embeds": [
{ {
"title": "A new message has been posted!", "title": "A new message has been posted!",
"description": "**%s** has posted a new message: [**%s**](%s)" % (newpost_username, newpost_title, newpost_link_real), "description": "**%s** has posted a new message in [**%s**](%s)" % (newpost_username, newpost_title, newpost_link_real),
"color": 55039, "color": 55039,
"author": { "author": {
"name": "GiantsWD forums" "name": "GiantsWD forums"
@ -57,7 +69,7 @@ def loop():
"footer": { "footer": {
"text": "" "text": ""
}, },
"timestamp": datetime.datetime.now().isoformat() "timestamp": datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
} }
] ]
} }
@ -68,12 +80,12 @@ def loop():
print("Mark all read...") print("Mark all read...")
a = bs.find("a", class_="mark-read") a = bs.find("a", class_="mark-read")
mark_read_url = a["href"] mark_read_url = a["href"]
br.open(mark_read_url) BROWSER.open(mark_read_url)
# Logout # Logout
if LOGOUT: if LOGOUT:
print("Logout...") print("Logout...")
br.open(logout_url) BROWSER.open(logout_url)
if __name__ == "__main__": if __name__ == "__main__":
@ -83,4 +95,4 @@ if __name__ == "__main__":
except: except:
traceback.print_exc() traceback.print_exc()
print("Sleeping...") print("Sleeping...")
time.sleep(60*30) time.sleep(60*10)