kertser commited on
Commit
10a95dd
1 Parent(s): 3152d9e

Upload WarOnline_Chat.py

Browse files

Updated Quote Read and Post methods. The quote shall be updated to search for the last page of the thread

Files changed (1) hide show
  1. WarOnline_Chat.py +84 -51
WarOnline_Chat.py CHANGED
@@ -1,4 +1,4 @@
1
- # This is an automation of Bot to post to a specific thread in the forum.
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
- # Retrieve the login page HTML to get the CSRF token
20
- login_page_response = session.get(login_url)
21
- soup = BeautifulSoup(login_page_response.text, 'html.parser')
22
- csrf_token = soup.find('input', {'name': '_xfToken'})['value']
23
-
24
- # Login to the website
25
- login_data = {
26
- 'login': username,
27
- 'password': password,
28
- 'remember': '1',
29
- '_xfRedirect': thread_url,
30
- '_xfToken': csrf_token
31
- }
32
- response = session.post(login_url, data=login_data)
33
-
34
- # Check if the login was successful
35
- if 'Invalid login' in response.text:
36
- print('Login failed!')
37
- exit()
38
-
39
- # Retrieve the thread page HTML
40
- response = session.get(thread_url)
41
-
42
- # Parse the HTML with BeautifulSoup
43
- soup = BeautifulSoup(response.text, 'html.parser')
44
-
45
- # Extract the _xfToken value from the hidden form field
46
- xf_token = soup.find('input', {'name': '_xfToken'}).get('value')
47
-
48
- post_url = "https://waronline.org/fora/index.php?threads/warbot-playground.17636/add-reply"
49
-
50
- # Construct the message data for the POST request
51
- message_data = {
52
- '_xfToken': xf_token,
53
- 'message': message,
54
- 'attachment_hash': '',
55
- 'last_date': '',
56
- '_xfRequestUri': post_url,
57
- '_xfWithData': '1',
58
- '_xfResponseType': 'json'
59
- }
60
-
61
- response = session.post(post_url, data=message_data)
62
-
63
- # Check if the post was successful
64
- if not response.ok:
65
- print('Post failed!')
66
- exit()
67
-
68
- print('Post submitted successfully.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)