82 lines
2.4 KiB
Python
82 lines
2.4 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"]
|
||
|
|
||
|
|
||
|
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)
|