141 lines
4.7 KiB
Python
141 lines
4.7 KiB
Python
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"]
|
|
LOGOUT = config["logout"]
|
|
BROWSER = mechanize.Browser()
|
|
|
|
|
|
def login():
|
|
login_url = "https://www.giantswd.org/forum/ucp.php?mode=login"
|
|
|
|
# Login
|
|
print("Logging in...")
|
|
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()
|
|
|
|
|
|
connected = []
|
|
|
|
|
|
def loop():
|
|
global connected
|
|
homepage_url = "https://www.giantswd.org/forum/"
|
|
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()
|
|
|
|
if config["logged_users"]:
|
|
# Send a webhook when people connect on GWD forums
|
|
print("Getting connected people...")
|
|
homepage_resp = BROWSER.open(homepage_url)
|
|
homepage_html = homepage_resp.get_data()
|
|
bs = BeautifulSoup(homepage_html, features="html5lib")
|
|
div_online = bs.find("div", attrs={"class": "online-list"})
|
|
div_username_online = div_online.find_all("a", attrs={"class": "username"})
|
|
|
|
new_users = []
|
|
for username_elem in div_username_online:
|
|
user = username_elem.contents[0]
|
|
|
|
if user not in connected:
|
|
new_users.append(user)
|
|
print("User connected: %s" % user)
|
|
webhook_data = {
|
|
"embeds": [
|
|
{
|
|
"title": "A user has logged in on GWD forums!",
|
|
"description": "**%s** has just logged in" % user,
|
|
"color": 55039,
|
|
"author": {
|
|
"name": "GiantsWD forums"
|
|
},
|
|
"footer": {
|
|
"text": ""
|
|
},
|
|
"timestamp": datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
|
|
}
|
|
]
|
|
}
|
|
requests.post(WEBHOOK_URL, json=webhook_data)
|
|
|
|
connected = new_users
|
|
|
|
# Get new posts
|
|
print("Getting new posts...")
|
|
has_newposts = False
|
|
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"})
|
|
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]
|
|
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 in [**%s**](%s)" % (newpost_username, newpost_title, newpost_link_real),
|
|
"color": 55039,
|
|
"author": {
|
|
"name": "GiantsWD forums"
|
|
},
|
|
"footer": {
|
|
"text": ""
|
|
},
|
|
"timestamp": datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
|
|
}
|
|
]
|
|
}
|
|
requests.post(WEBHOOK_URL, json=webhook_data)
|
|
|
|
if has_newposts:
|
|
# Mark all read
|
|
print("Mark all read...")
|
|
a = bs.find("a", class_="mark-read")
|
|
mark_read_url = a["href"]
|
|
BROWSER.open(mark_read_url)
|
|
|
|
# Logout
|
|
if LOGOUT:
|
|
print("Logout...")
|
|
BROWSER.open(logout_url)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
while True:
|
|
try:
|
|
loop()
|
|
except:
|
|
print(datetime.datetime.now())
|
|
traceback.print_exc()
|
|
print("Sleeping...")
|
|
time.sleep(60*10)
|