kertser commited on
Commit
ef02423
1 Parent(s): f6e93d7

Upload WarOnline_Chat.py

Browse files
Files changed (1) hide show
  1. WarOnline_Chat.py +101 -60
WarOnline_Chat.py CHANGED
@@ -6,9 +6,9 @@ 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
@@ -16,9 +16,6 @@ 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'
@@ -26,11 +23,15 @@ password = 'naP2tion'
26
  # Start a session to persist the login cookie across requests
27
  session = requests.Session()
28
 
29
-
30
  def compare_pages(url1, url2):
31
  #Compares 2 pages and returns True if they are the same
32
  return urllib.urlopen(url1).geturl() == urllib.urlopen(url2).geturl()
33
 
 
 
 
 
 
34
  def login(username=username, password=password, thread_url=thread_url):
35
  # Log-In to the forum and redirect to thread
36
 
@@ -61,6 +62,7 @@ def post(message=message, thread_url=thread_url, post_url=post_url, quoted_by=""
61
 
62
  if quoted_by:
63
  message = f'[QUOTE="{quoted_by}, post: {quote_source}"]{quote_text}[/QUOTE]{message}'
 
64
  # optionally add @{quoted_by} to indent the quoter
65
 
66
  # Retrieve the thread page HTML
@@ -92,7 +94,7 @@ def post(message=message, thread_url=thread_url, post_url=post_url, quoted_by=""
92
 
93
  print('Post submitted successfully.')
94
 
95
- def allQuotesFor(thread_url=thread_url, username=username, startingPage=1):
96
  # Returns all the quotes for #username in the specific multi-page thread url
97
  allquotes =[]
98
 
@@ -102,10 +104,13 @@ def allQuotesFor(thread_url=thread_url, username=username, startingPage=1):
102
  # Initial values for messangerName and the message ID
103
  messengerName = ""
104
  messageID = ""
 
105
 
106
  # Patterns to search in the last quote.
107
  namePattern = re.compile('data-lb-caption-desc="(.*?) ·')
108
  messageIDPattern = re.compile('data-lb-id="(.*?)"')
 
 
109
 
110
  while not lastPage:
111
  response = requests.get(thread_url + 'page-' + str(page))
@@ -118,30 +123,44 @@ def allQuotesFor(thread_url=thread_url, username=username, startingPage=1):
118
  soup = BeautifulSoup(html_content, 'html.parser')
119
 
120
  # Find all the message in the thread page
121
- messageData = soup.find_all('div', {'class': 'message-userContent'})
122
 
123
  for data in messageData:
124
- if username in data.text:
125
- try:
126
- # Get the messager username
127
- matchName = namePattern.search(str(data))
128
- if matchName:
129
- messengerName = matchName.group(1)
130
-
131
- # Get the message ID
132
- matchID = messageIDPattern.search(str(data))
133
- if matchID:
134
- messageID = matchID.group(1)
135
-
136
- if 'Click' in data.text:
137
- reply = data.text.split("Click to expand...")[-1].replace('\n', ' ').strip()
138
- elif "Нажмите" in data.text:
139
- reply = data.text.split("Нажмите для раскрытия...")[-1].replace('\n', ' ').strip()
140
-
141
- allquotes.append({'reply': reply, 'messengerName': messengerName, 'messageID': messageID})
142
-
143
- except:
144
- continue # There was no text in quote, move next
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  #check if that is not a last page
147
  if not compare_pages(thread_url + 'page-' + str(page), thread_url + 'page-' + str(page + 1)):
@@ -153,45 +172,67 @@ def allQuotesFor(thread_url=thread_url, username=username, startingPage=1):
153
 
154
  return allquotes
155
 
 
156
  def WarOnlineBot():
157
- # Get All Quotes
158
- quotes = allQuotesFor(thread_url=thread_url, username=username, startingPage=1)
159
- repliedMessageIDs = []
160
- for quote in quotes:
161
- replies = allQuotesFor(thread_url=thread_url, username=quote['messengerName'], startingPage=1)
162
- for reply in replies: # Remove all non-relevant and previously answered replies
163
-
164
- if reply['messengerName']!=username or reply['messageID'] in repliedMessageIDs:
165
- replies.remove(reply)
166
- else:
167
- repliedMessageIDs.append(reply['messageID'])
168
- repliedMessageIDs = list(set(repliedMessageIDs))
169
- # There is still some strange error. Will take a look later
170
 
171
- message = ""
172
- if not replies: # Meaning that there are no answers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  while not message:
174
- message = WarClient.getReply(message=quote['reply'])
175
- print('Quote: ', quote['reply'])
176
- print('Reply: ',message)
177
- post(message=message, thread_url=thread_url, post_url=post_url, quoted_by=quote['messengerName'],
178
- quote_text=quote['reply'])
179
- print('posted the message to the forum')
180
- time.sleep(5) # Standby time for server load release
 
 
 
181
 
182
 
183
  if __name__ == '__main__':
184
- timeout = 2 # min
185
- #post(message=message, thread_url=thread_url, post_url=post_url,quoted_by='Василий Пупкин',quote_text='Testing the XenForo response mechanism')
186
 
187
  # Start the scheduler
188
  while True:
189
- login(username=username, password=password, thread_url=thread_url)
190
- print("logged in")
191
  WarOnlineBot()
192
- p_bar = tqdm(range(60 * timeout))
193
 
194
- for i in p_bar:
195
- p_bar.update(1)
196
- p_bar.refresh()
197
- time.sleep(1)
 
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
 
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
  # Define the login credentials
20
  username = 'WarBot'
21
  password = 'naP2tion'
 
23
  # Start a session to persist the login cookie across requests
24
  session = requests.Session()
25
 
 
26
  def compare_pages(url1, url2):
27
  #Compares 2 pages and returns True if they are the same
28
  return urllib.urlopen(url1).geturl() == urllib.urlopen(url2).geturl()
29
 
30
+ def remove_non_english_russian_chars(s):
31
+ # Regular expression to match all characters that are not in English or Russian
32
+ pattern = '[^0-9A-Za-zА-Яа-яЁё(),.!?"\s-]'
33
+ # Replace all matched characters with an empty string
34
+ return re.sub(pattern, '', s)
35
  def login(username=username, password=password, thread_url=thread_url):
36
  # Log-In to the forum and redirect to thread
37
 
 
62
 
63
  if quoted_by:
64
  message = f'[QUOTE="{quoted_by}, post: {quote_source}"]{quote_text}[/QUOTE]{message}'
65
+ #message = f'[QUOTE="{quoted_by}, data-source=post: {quote_source}"]{quote_text}[/QUOTE]{message}'
66
  # optionally add @{quoted_by} to indent the quoter
67
 
68
  # Retrieve the thread page HTML
 
94
 
95
  print('Post submitted successfully.')
96
 
97
+ def getMessages(thread_url=thread_url, quotedUser="", startingPage=1):
98
  # Returns all the quotes for #username in the specific multi-page thread url
99
  allquotes =[]
100
 
 
104
  # Initial values for messangerName and the message ID
105
  messengerName = ""
106
  messageID = ""
107
+ quotedID = ""
108
 
109
  # Patterns to search in the last quote.
110
  namePattern = re.compile('data-lb-caption-desc="(.*?) ·')
111
  messageIDPattern = re.compile('data-lb-id="(.*?)"')
112
+ quotedIDPattern = re.compile('data-source="(.*?)"')
113
+ quotedNamePattern = re.compile('data-quote="(.*?)"')
114
 
115
  while not lastPage:
116
  response = requests.get(thread_url + 'page-' + str(page))
 
123
  soup = BeautifulSoup(html_content, 'html.parser')
124
 
125
  # Find all the message in the thread page
126
+ messageData = soup.find_all('div', {'class': 'message-userContent lbContainer js-lbContainer'})
127
 
128
  for data in messageData:
129
+ try:
130
+ # Get the messager username
131
+ matchName = namePattern.search(str(data))
132
+ if matchName:
133
+ messengerName = matchName.group(1)
134
+
135
+ # Get the quoted ID
136
+ matchID = quotedIDPattern.search(str(data))
137
+ if matchID:
138
+ quotedID = matchID.group(1)
139
+
140
+ # Get the message ID
141
+ matchID = messageIDPattern.search(str(data))
142
+ if matchID:
143
+ messageID = matchID.group(1)
144
+
145
+ matchQuotedName = quotedNamePattern.search(str(data))
146
+ if matchQuotedName:
147
+ quotedName = matchQuotedName.group(1)
148
+ if quotedUser and (quotedUser != quotedName):
149
+ continue
150
+
151
+ # Make sure that the messages have a quote inside
152
+ blockquote = data.find('blockquote')
153
+ if blockquote:
154
+ # Extract the text
155
+ text = data.find('div', {'class': 'bbWrapper'})
156
+ for bq in text.find_all('blockquote'):
157
+ bq.extract()
158
+ reply = text.get_text().replace('\n', ' ').strip()
159
+
160
+ allquotes.append({'reply': reply, 'messengerName': messengerName, 'messageID': messageID, 'quotedID': quotedID})
161
+
162
+ except:
163
+ continue # There was no text in quote, move next
164
 
165
  #check if that is not a last page
166
  if not compare_pages(thread_url + 'page-' + str(page), thread_url + 'page-' + str(page + 1)):
 
172
 
173
  return allquotes
174
 
175
+ # Core Engine of the Client
176
  def WarOnlineBot():
 
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
+ login(username=username, password=password, thread_url=thread_url)
179
+ #print("logged in")
180
+
181
+ # All messages (with quotes) by ALL users:
182
+ allMessages = getMessages(thread_url=thread_url, quotedUser='', startingPage=1)
183
+
184
+ # IDs of the quoted messages, replied by the bot:
185
+ messages_by_bot_IDs = []
186
+
187
+ for msg in allMessages:
188
+ # Set a list of replied messages IDs
189
+ if msg['messengerName'] == username: #message posted by the WarBot
190
+ messages_by_bot_IDs.append(msg['quotedID'].split(': ')[-1])
191
+ # remove empty and repeated elements
192
+ messages_by_bot_IDs = list(set([elem for elem in messages_by_bot_IDs if elem]))
193
+
194
+ # All messages (with quotes) sent _FOR_ the Bot:
195
+ messagesForBot = getMessages(thread_url=thread_url, quotedUser=username, startingPage=1)
196
+
197
+ # IDs of the messages, quoting the bot:
198
+ messages_for_bot_IDs = []
199
+
200
+ for msg in messagesForBot:
201
+ # Set a list of posted message IDs
202
+ messages_for_bot_IDs.append(msg['messageID'].split('-')[-1])
203
+ # remove empty elements
204
+ messages_for_bot_IDs = [elem for elem in messages_for_bot_IDs if elem]
205
+
206
+ # Filter to leave just the unanswered messages IDs:
207
+ messages_for_bot_IDs = [ID for ID in messages_for_bot_IDs if ID not in messages_by_bot_IDs]
208
+
209
+ # Reply the unanswered messages:
210
+ for msg in messagesForBot:
211
+ if msg['messageID'].split('-')[-1] in messages_for_bot_IDs:
212
+
213
+ quote = remove_non_english_russian_chars(msg['reply'])
214
+ message = ""
215
+
216
  while not message:
217
+ message = WarClient.getReply(message=quote)
218
+
219
+ print('Quote: ', quote)
220
+ print('Reply: ', message)
221
+
222
+ login(username=username, password=password, thread_url=thread_url)
223
+ time.sleep(1)
224
+ post(message=message, thread_url=thread_url, post_url=post_url, quoted_by=msg['messengerName'], quote_text=quote, quote_source=msg['messageID'])
225
+
226
+ time.sleep(10) # Standby time for server load release
227
 
228
 
229
  if __name__ == '__main__':
230
+ timeout = 5 # min
 
231
 
232
  # Start the scheduler
233
  while True:
 
 
234
  WarOnlineBot()
 
235
 
236
+ timer = range(60 * timeout)
237
+ for t in timer:
238
+ time.sleep(1)