WarBot / WarOnline_Chat.py
kertser's picture
Upload WarOnline_Chat.py
10a95dd
raw
history blame
No virus
3.21 kB
# This is an quote and post library for a specific thread in the WarOnline forum.
import requests
from bs4 import BeautifulSoup
# Define the login URL and the thread URL
login_url = 'https://waronline.org/fora/index.php?login/login'
thread_url = 'https://waronline.org/fora/index.php?threads/warbot-playground.17636/'
post_url = "https://waronline.org/fora/index.php?threads/warbot-playground.17636/add-reply"
# Sending the message
message = "Hello World"
# Define the login credentials
username = 'WarBot'
password = 'naP2tion'
# Start a session to persist the login cookie across requests
session = requests.Session()
def login(username=username, password=password, thread_url=thread_url):
# Log-In to the forum and redirect to thread
# Retrieve the login page HTML to get the CSRF token
login_page_response = session.get(login_url)
soup = BeautifulSoup(login_page_response.text, 'html.parser')
csrf_token = soup.find('input', {'name': '_xfToken'})['value']
# Login to the website
login_data = {
'login': username,
'password': password,
'remember': '1',
'_xfRedirect': thread_url,
'_xfToken': csrf_token
}
response = session.post(login_url, data=login_data)
# Check if the login was successful
if 'Invalid login' in response.text:
print('Login failed!')
exit()
def post(message=message, thread_url=thread_url, post_url=post_url):
#Post a message to the forum
# Retrieve the thread page HTML
response = session.get(thread_url)
# Parse the HTML with BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# Extract the _xfToken value from the hidden form field
xf_token = soup.find('input', {'name': '_xfToken'}).get('value')
# Construct the message data for the POST request
message_data = {
'_xfToken': xf_token,
'message': message,
'attachment_hash': '',
'last_date': '',
'_xfRequestUri': post_url,
'_xfWithData': '1',
'_xfResponseType': 'json'
}
response = session.post(post_url, data=message_data)
# Check if the post was successful
if not response.ok:
print('Post failed!')
exit()
print('Post submitted successfully.')
def readQuote(thread_url=thread_url, username=username):
# Retrieve the content of the specific thread
response = session.get(thread_url)
html_content = response.content
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')
# Find all the quotes in the thread
quotes = soup.find_all('div', {'class': 'bbWrapper'})
# Check each quote if it contains your name and keep track of the latest one
latest_quote = None
for quote in quotes:
if username in quote.text:
latest_quote = quote
if latest_quote:
reply = latest_quote.text.split("Click to expand...")[-1].replace('\n', ' ').strip()
print(reply)
if __name__ == '__main__':
login(username=username, password=password, thread_url=thread_url)
#post(message=message, thread_url=thread_url, post_url=post_url)
readQuote(thread_url=thread_url)