Upload WarOnline_Chat.py
Browse filesUpdated Quote Read and Post methods. The quote shall be updated to search for the last page of the thread
- WarOnline_Chat.py +84 -51
WarOnline_Chat.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# This is an
|
2 |
|
3 |
import requests
|
4 |
from bs4 import BeautifulSoup
|
@@ -6,7 +6,9 @@ from bs4 import BeautifulSoup
|
|
6 |
# Define the login URL and the thread URL
|
7 |
login_url = 'https://waronline.org/fora/index.php?login/login'
|
8 |
thread_url = 'https://waronline.org/fora/index.php?threads/warbot-playground.17636/'
|
|
|
9 |
|
|
|
10 |
message = "Hello World"
|
11 |
|
12 |
# Define the login credentials
|
@@ -16,53 +18,84 @@ password = 'naP2tion'
|
|
16 |
# Start a session to persist the login cookie across requests
|
17 |
session = requests.Session()
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
#
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
'
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This is an quote and post library for a specific thread in the WarOnline forum.
|
2 |
|
3 |
import requests
|
4 |
from bs4 import BeautifulSoup
|
|
|
6 |
# Define the login URL and the thread URL
|
7 |
login_url = 'https://waronline.org/fora/index.php?login/login'
|
8 |
thread_url = 'https://waronline.org/fora/index.php?threads/warbot-playground.17636/'
|
9 |
+
post_url = "https://waronline.org/fora/index.php?threads/warbot-playground.17636/add-reply"
|
10 |
|
11 |
+
# Sending the message
|
12 |
message = "Hello World"
|
13 |
|
14 |
# Define the login credentials
|
|
|
18 |
# Start a session to persist the login cookie across requests
|
19 |
session = requests.Session()
|
20 |
|
21 |
+
def login(username=username, password=password, thread_url=thread_url):
|
22 |
+
# Log-In to the forum and redirect to thread
|
23 |
+
|
24 |
+
# Retrieve the login page HTML to get the CSRF token
|
25 |
+
login_page_response = session.get(login_url)
|
26 |
+
soup = BeautifulSoup(login_page_response.text, 'html.parser')
|
27 |
+
csrf_token = soup.find('input', {'name': '_xfToken'})['value']
|
28 |
+
|
29 |
+
# Login to the website
|
30 |
+
login_data = {
|
31 |
+
'login': username,
|
32 |
+
'password': password,
|
33 |
+
'remember': '1',
|
34 |
+
'_xfRedirect': thread_url,
|
35 |
+
'_xfToken': csrf_token
|
36 |
+
}
|
37 |
+
response = session.post(login_url, data=login_data)
|
38 |
+
|
39 |
+
# Check if the login was successful
|
40 |
+
if 'Invalid login' in response.text:
|
41 |
+
print('Login failed!')
|
42 |
+
exit()
|
43 |
+
|
44 |
+
def post(message=message, thread_url=thread_url, post_url=post_url):
|
45 |
+
#Post a message to the forum
|
46 |
+
|
47 |
+
# Retrieve the thread page HTML
|
48 |
+
response = session.get(thread_url)
|
49 |
+
|
50 |
+
# Parse the HTML with BeautifulSoup
|
51 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
52 |
+
|
53 |
+
# Extract the _xfToken value from the hidden form field
|
54 |
+
xf_token = soup.find('input', {'name': '_xfToken'}).get('value')
|
55 |
+
|
56 |
+
# Construct the message data for the POST request
|
57 |
+
message_data = {
|
58 |
+
'_xfToken': xf_token,
|
59 |
+
'message': message,
|
60 |
+
'attachment_hash': '',
|
61 |
+
'last_date': '',
|
62 |
+
'_xfRequestUri': post_url,
|
63 |
+
'_xfWithData': '1',
|
64 |
+
'_xfResponseType': 'json'
|
65 |
+
}
|
66 |
+
|
67 |
+
response = session.post(post_url, data=message_data)
|
68 |
+
|
69 |
+
# Check if the post was successful
|
70 |
+
if not response.ok:
|
71 |
+
print('Post failed!')
|
72 |
+
exit()
|
73 |
+
|
74 |
+
print('Post submitted successfully.')
|
75 |
+
|
76 |
+
def readQuote(thread_url=thread_url, username=username):
|
77 |
+
# Retrieve the content of the specific thread
|
78 |
+
response = session.get(thread_url)
|
79 |
+
html_content = response.content
|
80 |
+
|
81 |
+
# Parse the HTML content using BeautifulSoup
|
82 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
83 |
+
|
84 |
+
# Find all the quotes in the thread
|
85 |
+
quotes = soup.find_all('div', {'class': 'bbWrapper'})
|
86 |
+
|
87 |
+
# Check each quote if it contains your name and keep track of the latest one
|
88 |
+
latest_quote = None
|
89 |
+
for quote in quotes:
|
90 |
+
if username in quote.text:
|
91 |
+
latest_quote = quote
|
92 |
+
|
93 |
+
if latest_quote:
|
94 |
+
reply = latest_quote.text.split("Click to expand...")[-1].replace('\n', ' ').strip()
|
95 |
+
print(reply)
|
96 |
+
|
97 |
+
|
98 |
+
if __name__ == '__main__':
|
99 |
+
login(username=username, password=password, thread_url=thread_url)
|
100 |
+
#post(message=message, thread_url=thread_url, post_url=post_url)
|
101 |
+
readQuote(thread_url=thread_url)
|