kertser commited on
Commit
f6e93d7
1 Parent(s): 827896c

Delete WarOnline_Chat_test.py

Browse files
Files changed (1) hide show
  1. WarOnline_Chat_test.py +0 -254
WarOnline_Chat_test.py DELETED
@@ -1,254 +0,0 @@
1
- # This is an quote and post library for a specific thread in the WarOnline forum.
2
-
3
- import WarClient
4
- import requests
5
- import re
6
- from bs4 import BeautifulSoup
7
- import urllib.request as urllib
8
- import warnings
9
- import schedule
10
- import time
11
- from tqdm import tqdm
12
- warnings.filterwarnings("ignore")
13
-
14
- # Define the login URL and the thread URL
15
- login_url = 'https://waronline.org/fora/index.php?login/login'
16
- thread_url = 'https://waronline.org/fora/index.php?threads/warbot-playground.17636/'
17
- post_url = "https://waronline.org/fora/index.php?threads/warbot-playground.17636/add-reply"
18
-
19
- # Sending the message (change that!)
20
- message = "Test"
21
-
22
- # Define the login credentials
23
- username = 'WarBot'
24
- password = 'naP2tion'
25
-
26
- # Start a session to persist the login cookie across requests
27
- session = requests.Session()
28
-
29
- def compare_pages(url1, url2):
30
- #Compares 2 pages and returns True if they are the same
31
- return urllib.urlopen(url1).geturl() == urllib.urlopen(url2).geturl()
32
-
33
- def remove_non_english_russian_chars(s):
34
- # Regular expression to match all characters that are not in English or Russian
35
- pattern = '[^0-9A-Za-zА-Яа-яЁё(),.:!;"\s-]'
36
- # Replace all matched characters with an empty string
37
- return re.sub(pattern, '', s)
38
- def login(username=username, password=password, thread_url=thread_url):
39
- # Log-In to the forum and redirect to thread
40
-
41
- # Retrieve the login page HTML to get the CSRF token
42
- login_page_response = session.get(login_url)
43
- soup = BeautifulSoup(login_page_response.text, 'html.parser')
44
- csrf_token = soup.find('input', {'name': '_xfToken'})['value']
45
-
46
- # Login to the website
47
- login_data = {
48
- 'login': username,
49
- 'password': password,
50
- 'remember': '1',
51
- '_xfRedirect': thread_url,
52
- '_xfToken': csrf_token
53
- }
54
- response = session.post(login_url, data=login_data)
55
-
56
- # Check if the login was successful
57
- if 'Invalid login' in response.text:
58
- print('Login failed!')
59
- exit()
60
-
61
- def post(message=message, thread_url=thread_url, post_url=post_url, quoted_by="",quote_text="",quote_source=""):
62
- #Post a message to the forum (with or without the quote
63
- #quote_source is in format 'post-3920992'
64
- quote_source = quote_source.split('-')[-1] # Take the numbers only
65
-
66
- if quoted_by:
67
- message = f'[QUOTE="{quoted_by}, post: {quote_source}"]{quote_text}[/QUOTE]{message}'
68
- #message = f'[QUOTE="{quoted_by}, data-source=post: {quote_source}"]{quote_text}[/QUOTE]{message}'
69
- # optionally add @{quoted_by} to indent the quoter
70
-
71
- # Retrieve the thread page HTML
72
- response = session.get(thread_url)
73
-
74
- # Parse the HTML with BeautifulSoup
75
- soup = BeautifulSoup(response.text, 'html.parser')
76
-
77
- # Extract the _xfToken value from the hidden form field
78
- xf_token = soup.find('input', {'name': '_xfToken'}).get('value')
79
-
80
- # Construct the message data for the POST request
81
- message_data = {
82
- '_xfToken': xf_token,
83
- 'message': message,
84
- 'attachment_hash': '',
85
- 'last_date': '',
86
- '_xfRequestUri': post_url,
87
- '_xfWithData': '1',
88
- '_xfResponseType': 'json'
89
- }
90
-
91
- response = session.post(post_url, data=message_data)
92
-
93
- # Check if the post was successful
94
- if not response.ok:
95
- print('Post failed!')
96
- exit()
97
-
98
- print('Post submitted successfully.')
99
-
100
- def getMessages(thread_url=thread_url, quotedUser="", startingPage=1):
101
- # Returns all the quotes for #username in the specific multi-page thread url
102
- allquotes =[]
103
-
104
- page = startingPage # Counter
105
- lastPage = False
106
-
107
- # Initial values for messangerName and the message ID
108
- messengerName = ""
109
- messageID = ""
110
- quotedID = ""
111
-
112
- # Patterns to search in the last quote.
113
- namePattern = re.compile('data-lb-caption-desc="(.*?) ·')
114
- messageIDPattern = re.compile('data-lb-id="(.*?)"')
115
- quotedIDPattern = re.compile('data-source="(.*?)"')
116
- quotedNamePattern = re.compile('data-quote="(.*?)"')
117
-
118
- while not lastPage:
119
- response = requests.get(thread_url + 'page-' + str(page))
120
- if response.status_code == 200:
121
-
122
- # Core of the function
123
- html_content = response.content
124
-
125
- # Parse the HTML content using BeautifulSoup
126
- soup = BeautifulSoup(html_content, 'html.parser')
127
-
128
- # Find all the message in the thread page
129
- messageData = soup.find_all('div', {'class': 'message-userContent lbContainer js-lbContainer'})
130
-
131
- for data in messageData:
132
- try:
133
- # Get the messager username
134
- matchName = namePattern.search(str(data))
135
- if matchName:
136
- messengerName = matchName.group(1)
137
-
138
- # Get the quoted ID
139
- matchID = quotedIDPattern.search(str(data))
140
- if matchID:
141
- quotedID = matchID.group(1)
142
-
143
- # Get the message ID
144
- matchID = messageIDPattern.search(str(data))
145
- if matchID:
146
- messageID = matchID.group(1)
147
-
148
- matchQuotedName = quotedNamePattern.search(str(data))
149
- if matchQuotedName:
150
- quotedName = matchQuotedName.group(1)
151
- if quotedUser and (quotedUser != quotedName):
152
- continue
153
-
154
- # Make sure that the messages have a quote inside
155
- blockquote = data.find('blockquote')
156
- if blockquote:
157
- # Extract the text
158
- text = data.find('div', {'class': 'bbWrapper'})
159
- for bq in text.find_all('blockquote'):
160
- bq.extract()
161
- reply = text.get_text().replace('\n', ' ').strip()
162
-
163
- allquotes.append({'reply': reply, 'messengerName': messengerName, 'messageID': messageID, 'quotedID': quotedID})
164
-
165
- except:
166
- continue # There was no text in quote, move next
167
-
168
- #check if that is not a last page
169
- if not compare_pages(thread_url + 'page-' + str(page), thread_url + 'page-' + str(page + 1)):
170
- page += 1
171
- else:
172
- lastPage = True
173
- else:
174
- lastPage = True
175
-
176
- return allquotes
177
-
178
- # Core Engine of the Client
179
- def WarOnlineBot():
180
-
181
- login(username=username, password=password, thread_url=thread_url)
182
- #print("logged in")
183
-
184
- # All messages (with quotes) by ALL users:
185
- allMessages = getMessages(thread_url=thread_url, quotedUser='', startingPage=1)
186
-
187
- # IDs of the quoted messages, for messages posted by bot:
188
- messages_by_bot_IDs = []
189
-
190
- for msg in allMessages:
191
- # Set a list of replied IDs
192
- if msg['messengerName'] == username:
193
- messages_by_bot_IDs.append(msg['quotedID'].split(': ')[-1])
194
- # remove empty elements
195
- messages_by_bot_IDs = [elem for elem in messages_by_bot_IDs if elem]
196
-
197
- # All messages (with quotes) sent _FOR_ the Bot:
198
- messagesForBot = getMessages(thread_url=thread_url, quotedUser=username, startingPage=1)
199
-
200
- # IDs of the messages, quoting the bot:
201
- messages_for_bot_IDs = []
202
-
203
- for msg in messagesForBot:
204
- # Set a list of posted message IDs
205
- messages_for_bot_IDs.append(msg['messageID'].split('-')[-1])
206
- # remove empty elements
207
- messages_for_bot_IDs = [elem for elem in messages_for_bot_IDs if elem]
208
-
209
- # Filter the unanswered messages
210
- for msgID in messages_for_bot_IDs:
211
- if msgID in messages_by_bot_IDs:
212
- messages_for_bot_IDs.remove(msgID)
213
-
214
- # print unanswered messages
215
- for msg in messagesForBot:
216
- if msg['messageID'].split('-')[-1] in messages_for_bot_IDs:
217
-
218
- quote = remove_non_english_russian_chars(msg['reply'])
219
- message = ""
220
-
221
- while not message:
222
- message = WarClient.getReply(message=quote)
223
-
224
- print('Quote: ', quote)
225
- print('Reply: ', message)
226
-
227
- login(username=username, password=password, thread_url=thread_url)
228
- time.sleep(1)
229
- post(message=message, thread_url=thread_url, post_url=post_url, quoted_by=msg['messengerName'], quote_text=quote, quote_source=msg['messageID'])
230
-
231
- time.sleep(5) # Standby time for server load release
232
-
233
- if __name__ == '__main__':
234
- timeout = 2 # min
235
-
236
- WarOnlineBot()
237
-
238
- #login(username=username, password=password, thread_url=thread_url)
239
- #post(message=message, thread_url=thread_url, post_url=post_url, quoted_by='WarBot', quote_text='quoted message', quote_source='12345')
240
-
241
-
242
- """
243
- # Start the scheduler
244
- while True:
245
- login(username=username, password=password, thread_url=thread_url)
246
- print("logged in")
247
- WarOnlineBot()
248
- p_bar = tqdm(range(60 * timeout))
249
-
250
- for i in p_bar:
251
- p_bar.update(1)
252
- p_bar.refresh()
253
- time.sleep(1)
254
- """