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"]
WEBHOOK_URL = config["webhook"]
LOGOUT = config["logout"]
BROWSER = mechanize.Browser()
def loop():
def 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
print("Logging in...")
br = mechanize.Browser()
br.open(login_url)
br.select_form(id="login")
br["username"] = USERNAME
br["password"] = PASSWORD
br.submit()
BROWSER.open(login_url)
BROWSER.select_form(id="login")
BROWSER["username"] = USERNAME
BROWSER["password"] = PASSWORD
BROWSER.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
print("Getting new posts...")
has_newposts = False
newposts_resp = br.open(newposts_url)
newposts_resp = BROWSER.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"})
@ -49,7 +61,7 @@ def loop():
"embeds": [
{
"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,
"author": {
"name": "GiantsWD forums"
@ -57,7 +69,7 @@ def loop():
"footer": {
"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...")
a = bs.find("a", class_="mark-read")
mark_read_url = a["href"]
br.open(mark_read_url)
BROWSER.open(mark_read_url)
# Logout
if LOGOUT:
print("Logout...")
br.open(logout_url)
BROWSER.open(logout_url)
if __name__ == "__main__":
@ -83,4 +95,4 @@ if __name__ == "__main__":
except:
traceback.print_exc()
print("Sleeping...")
time.sleep(60*30)
time.sleep(60*10)