WarBot / WarOnline_Chat.py
kertser's picture
Upload WarOnline_Chat.py
dd2dd36
raw
history blame
2.33 kB
# This is an automation of Bot to post to a specific thread in the 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/Творчество-роботов-нейросети-и-т-п.17617/'
#thread_url = 'https://waronline.org/fora/index.php?threads/%D0%A2%D0%B2%D0%BE%D1%80%D1%87%D0%B5%D1%81%D1%82%D0%B2%D0%BE-%D1%80%D0%BE%D0%B1%D0%BE%D1%82%D0%BE%D0%B2-%D0%BD%D0%B5%D0%B9%D1%80%D0%BE%D1%81%D0%B5%D1%82%D0%B8-%D0%B8-%D1%82-%D0%BF.17617/'
# Define the login credentials
username = 'WarBot'
password = 'naP2tion'
# Start a session to persist the login cookie across requests
session = requests.Session()
# 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()
# 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')
# Post a message to the thread
post_data = {
'message_html': 'Hello world!', # Replace with your message
'last_date': '', # Leave empty for a new post
'watch_thread': '1', # Subscribe to the thread (optional)
'_xfToken': xf_token, # Retrieve the token from the page HTML '1676385710,1cdf871cad6915a8f95aa9ddb0e03cc9'
'_xfRequestUri': thread_url, # Retrieve from the page HTML
'_xfWithData': '1', # Required for some XenForo versions
'_xfResponseType': 'json' # Receive the response in JSON format
}
response = session.post(thread_url, data=post_data)
# Check if the post was successful
if not response.ok:
print('Post failed!')
exit()
print('Post submitted successfully.')