kertser commited on
Commit
92b9c00
1 Parent(s): 8f30cba

Upload 4 files

Browse files
Files changed (2) hide show
  1. WarOnline_Chat.py +138 -125
  2. config.py +1 -1
WarOnline_Chat.py CHANGED
@@ -52,12 +52,14 @@ def remove_non_english_russian_chars(s):
52
  return re.sub(pattern, '', s)
53
 
54
  def remove_extra_spaces(s):
 
55
  s = re.sub(r"\s+", " ", s) # replace all sequences of whitespace with a single space
56
  s = re.sub(r"\s+([.,-])", r"\1", s) # remove spaces before period, dash or comma
57
  return(s)
58
 
59
  def getLastPage(thread_url=config.thread_url):
60
  # Returns the number of the last page
 
61
  page = 1 # Starting page
62
  lastPage = False
63
 
@@ -66,6 +68,7 @@ def getLastPage(thread_url=config.thread_url):
66
  page += 1
67
  else:
68
  lastPage = True
 
69
  return page
70
 
71
  def login(username=config.username, password=config.password, thread_url=config.thread_url):
@@ -90,6 +93,8 @@ def login(username=config.username, password=config.password, thread_url=config.
90
  if 'Invalid login' in response.text:
91
  print('Login failed!')
92
  exit()
 
 
93
 
94
  def post(message="", thread_url=config.thread_url, post_url=config.post_url, quoted_by="",quote_text="",quote_source="",img_url=""):
95
  #Post a message to the forum (with or without the quote
@@ -216,129 +221,133 @@ def getMessages(thread_url=config.thread_url, quotedUser="", startingPage=1):
216
 
217
  return allquotes
218
 
219
- # Core Engine of the Client
220
  def WarOnlineBot():
 
221
 
222
- lookUpPages = 5 # How many pages back to look in the thread
223
- startingPage = getLastPage(thread_url=config.thread_url) - lookUpPages
224
- if startingPage < 1:
225
- startingPage = 1 # Starting page cannot be less than 1
226
-
227
- login(username=config.username, password=config.password, thread_url=config.thread_url)
228
- #print("logged in")
229
-
230
- # All messages (with quotes) by ALL users:
231
- allMessages = getMessages(thread_url=config.thread_url, quotedUser='', startingPage=startingPage)
232
-
233
- # IDs of the quoted messages, replied by the bot:
234
- messages_by_bot_IDs = []
235
-
236
- # Initiate the direct messages
237
- direct_messages = []
238
-
239
- for msg in allMessages:
240
- # Direct message to the bot
241
- if msg['quotedID'].split(': ')[-1] == '0': #debug
242
- direct_messages.append(msg)
243
- # Set a list of replied messages IDs
244
- if msg['messengerName'] == config.username: #message posted by the WarBot
245
- messages_by_bot_IDs.append(msg['quotedID'].split(': ')[-1])
246
- # remove empty and repeated elements
247
- messages_by_bot_IDs = list(set([elem for elem in messages_by_bot_IDs if elem]))
248
-
249
- # All messages (with quotes) sent _FOR_ the Bot:
250
- messagesForBot = getMessages(thread_url=config.thread_url, quotedUser=config.username, startingPage=startingPage)
251
-
252
- # Append the direct messages to the messagesForBot:
253
- for msg in direct_messages:
254
- messagesForBot.append(msg)
255
-
256
- # IDs of the messages, quoting the bot:
257
- messages_for_bot_IDs = []
258
-
259
- for msg in messagesForBot:
260
- # Set a list of posted message IDs
261
- messages_for_bot_IDs.append(msg['messageID'].split('-')[-1])
262
- # remove empty elements
263
- messages_for_bot_IDs = [elem for elem in messages_for_bot_IDs if elem]
264
-
265
- # Filter to leave just the unanswered messages IDs:
266
- messages_for_bot_IDs = [ID for ID in messages_for_bot_IDs if ID not in messages_by_bot_IDs]
267
-
268
-
269
- # Reply the unanswered messages:
270
- for msg in messagesForBot:
271
- if msg['messageID'].split('-')[-1] in messages_for_bot_IDs:
272
-
273
- originalQuote = msg['reply']
274
- if originalQuote == "": # Just images, no text
275
- continue
276
- else:
277
- quote = remove_non_english_russian_chars(msg['reply'])
278
- quote = remove_extra_spaces(quote)
279
-
280
- message = "" #Initiating the reply message by Bot
281
- previous_dialogue = "" #Initiating the previous dialogue
282
-
283
- print('Quote: ', originalQuote)
284
-
285
- # Init Connection
286
- db = conversationDB.DataBase()
287
-
288
- if msg['quotedID'].split(': ')[-1] != '0': # It is dialogue. Look-up for the previous quotes
289
-
290
- # Get the previous dialogue from the database
291
- dbmessages = db.getmessages(msg['messengerName'])
292
- for dbmessage in dbmessages:
293
- previous_dialogue += dbmessage[0]+' '+dbmessage[1]+' '
294
- # Update the string and preprocess it
295
- quote = previous_dialogue + quote
296
- quote = remove_non_english_russian_chars(quote)
297
- quote = remove_extra_spaces(quote)
298
- # Truncate the quote to return only the last MaxWords of words:
299
- quote = " ".join(quote.split()[-config.MaxWords:])
300
-
301
- # Fix the quote string, to eliminate errors:
302
- quote = fixString(quote)
303
-
304
- FailureCounter = 0 # In case there is a bug in the model
305
- while (not message) and (FailureCounter<3):
306
- message = WarClient.getReply(message=quote)
307
- # Strange error in message if there is '02' in the message text.
308
- if '02' in message:
309
- message = ""
310
- FailureCounter+=1
311
-
312
- if FailureCounter == 3:
313
- continue # Skip that answer
314
-
315
- # Post-processing fixes:
316
- message = fixString(message)
317
- print('Reply: ', message)
318
-
319
- if message.endswith('.png'): # It is an image reply:
320
- # Post an image reply:
321
- login(username=config.username, password=config.password, thread_url=config.thread_url)
322
- time.sleep(1)
323
- post(message="", thread_url=config.thread_url, post_url=config.post_url, quoted_by=msg['messengerName'],
324
- quote_text=originalQuote, quote_source=msg['messageID'],
325
- img_url=message)
326
- # will not be added to the database, if image is a reply
327
-
328
- else:
329
-
330
- # Add the new conversation pair to the database
331
- db.setmessages(username=msg['messengerName'], message_text=originalQuote, bot_reply=message)
332
- # Clean up the excessive records, leaving only the remaining messages
333
- db.cleanup(username=msg['messengerName'], remaining_messages=config.remaining_messages)
334
- # Delete the duplicate records
335
- db.deleteDuplicates()
336
-
337
- login(username=config.username, password=config.password, thread_url=config.thread_url)
338
- time.sleep(1)
339
- post(message=message, thread_url=config.thread_url, post_url=config.post_url, quoted_by=msg['messengerName'], quote_text=originalQuote, quote_source=msg['messageID'])
340
 
341
- time.sleep(10) # Standby time for server load release
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
 
343
 
344
  if __name__ == '__main__':
@@ -346,7 +355,7 @@ if __name__ == '__main__':
346
  # Start the scheduler
347
  while True:
348
  print('Starting Session')
349
- WarOnlineBot()
350
 
351
  # Debug Only:
352
  #imgWord = 'как выглядит'
@@ -356,8 +365,12 @@ if __name__ == '__main__':
356
  post(message="", thread_url=config.thread_url, post_url=config.post_url, quoted_by='Test',
357
  quote_text="posting an image",img_url='https://replicate.delivery/pbxt/knKBiJt8DPZ0B1o25PaLJSZjgv3D5HcwLoBIn0JESbe3nISIA/out-0.png')
358
  """
359
- print('Session finished. Timeout...')
 
360
 
361
- timer = range(60 * config.timeout)
362
- for t in timer:
363
- time.sleep(1)
 
 
 
 
52
  return re.sub(pattern, '', s)
53
 
54
  def remove_extra_spaces(s):
55
+ # Removes extra whitespaces and unwanted characters
56
  s = re.sub(r"\s+", " ", s) # replace all sequences of whitespace with a single space
57
  s = re.sub(r"\s+([.,-])", r"\1", s) # remove spaces before period, dash or comma
58
  return(s)
59
 
60
  def getLastPage(thread_url=config.thread_url):
61
  # Returns the number of the last page
62
+ print('looking for the last page of the thread')
63
  page = 1 # Starting page
64
  lastPage = False
65
 
 
68
  page += 1
69
  else:
70
  lastPage = True
71
+ print('Last page of this thread is '+str(page))
72
  return page
73
 
74
  def login(username=config.username, password=config.password, thread_url=config.thread_url):
 
93
  if 'Invalid login' in response.text:
94
  print('Login failed!')
95
  exit()
96
+ else:
97
+ print('Login successful')
98
 
99
  def post(message="", thread_url=config.thread_url, post_url=config.post_url, quoted_by="",quote_text="",quote_source="",img_url=""):
100
  #Post a message to the forum (with or without the quote
 
221
 
222
  return allquotes
223
 
 
224
  def WarOnlineBot():
225
+ # Core Engine of the Client
226
 
227
+ try: # Try logging in
228
+ login(username=config.username, password=config.password, thread_url=config.thread_url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
 
230
+ lookUpPages = 5 # How many pages back to look in the thread
231
+ startingPage = getLastPage(thread_url=config.thread_url) - lookUpPages
232
+ if startingPage < 1:
233
+ startingPage = 1 # Starting page cannot be less than 1
234
+
235
+ # All messages (with quotes) by ALL users:
236
+ allMessages = getMessages(thread_url=config.thread_url, quotedUser='', startingPage=startingPage)
237
+
238
+ # IDs of the quoted messages, replied by the bot:
239
+ messages_by_bot_IDs = []
240
+
241
+ # Initiate the direct messages
242
+ direct_messages = []
243
+
244
+ for msg in allMessages:
245
+ # Direct message to the bot
246
+ if msg['quotedID'].split(': ')[-1] == '0': #debug
247
+ direct_messages.append(msg)
248
+ # Set a list of replied messages IDs
249
+ if msg['messengerName'] == config.username: #message posted by the WarBot
250
+ messages_by_bot_IDs.append(msg['quotedID'].split(': ')[-1])
251
+ # remove empty and repeated elements
252
+ messages_by_bot_IDs = list(set([elem for elem in messages_by_bot_IDs if elem]))
253
+
254
+ # All messages (with quotes) sent _FOR_ the Bot:
255
+ messagesForBot = getMessages(thread_url=config.thread_url, quotedUser=config.username, startingPage=startingPage)
256
+
257
+ # Append the direct messages to the messagesForBot:
258
+ for msg in direct_messages:
259
+ messagesForBot.append(msg)
260
+
261
+ # IDs of the messages, quoting the bot:
262
+ messages_for_bot_IDs = []
263
+
264
+ for msg in messagesForBot:
265
+ # Set a list of posted message IDs
266
+ messages_for_bot_IDs.append(msg['messageID'].split('-')[-1])
267
+ # remove empty elements
268
+ messages_for_bot_IDs = [elem for elem in messages_for_bot_IDs if elem]
269
+
270
+ # Filter to leave just the unanswered messages IDs:
271
+ messages_for_bot_IDs = [ID for ID in messages_for_bot_IDs if ID not in messages_by_bot_IDs]
272
+
273
+
274
+ # Reply the unanswered messages:
275
+ for msg in messagesForBot:
276
+ if msg['messageID'].split('-')[-1] in messages_for_bot_IDs:
277
+
278
+ originalQuote = msg['reply']
279
+ if originalQuote == "": # Just images, no text
280
+ continue
281
+ else:
282
+ quote = remove_non_english_russian_chars(msg['reply'])
283
+ quote = remove_extra_spaces(quote)
284
+
285
+ message = "" #Initiating the reply message by Bot
286
+ previous_dialogue = "" #Initiating the previous dialogue
287
+
288
+ print('Quote: ', originalQuote)
289
+
290
+ # Init Connection
291
+ db = conversationDB.DataBase()
292
+
293
+ if msg['quotedID'].split(': ')[-1] != '0': # It is dialogue. Look-up for the previous quotes
294
+
295
+ # Get the previous dialogue from the database
296
+ dbmessages = db.getmessages(msg['messengerName'])
297
+ for dbmessage in dbmessages:
298
+ previous_dialogue += dbmessage[0]+' '+dbmessage[1]+' '
299
+ # Update the string and preprocess it
300
+ quote = previous_dialogue + quote
301
+ quote = remove_non_english_russian_chars(quote)
302
+ quote = remove_extra_spaces(quote)
303
+ # Truncate the quote to return only the last MaxWords of words:
304
+ quote = " ".join(quote.split()[-config.MaxWords:])
305
+
306
+ # Fix the quote string, to eliminate errors:
307
+ quote = fixString(quote)
308
+
309
+ FailureCounter = 0 # In case there is a bug in the model
310
+ while (not message) and (FailureCounter<3):
311
+ message = WarClient.getReply(message=quote)
312
+ # Strange error in message if there is '02' in the message text.
313
+ if '02' in message:
314
+ message = ""
315
+ FailureCounter+=1
316
+
317
+ if FailureCounter == 3:
318
+ continue # Skip that answer
319
+
320
+ # Post-processing fixes:
321
+ message = fixString(message)
322
+ print('Reply: ', message)
323
+
324
+ if message.endswith('.png'): # It is an image reply:
325
+ # Post an image reply:
326
+ login(username=config.username, password=config.password, thread_url=config.thread_url)
327
+ time.sleep(1)
328
+ post(message="", thread_url=config.thread_url, post_url=config.post_url, quoted_by=msg['messengerName'],
329
+ quote_text=originalQuote, quote_source=msg['messageID'],
330
+ img_url=message)
331
+ # will not be added to the database, if image is a reply
332
+
333
+ else:
334
+
335
+ # Add the new conversation pair to the database
336
+ db.setmessages(username=msg['messengerName'], message_text=originalQuote, bot_reply=message)
337
+ # Clean up the excessive records, leaving only the remaining messages
338
+ db.cleanup(username=msg['messengerName'], remaining_messages=config.remaining_messages)
339
+ # Delete the duplicate records
340
+ db.deleteDuplicates()
341
+
342
+ login(username=config.username, password=config.password, thread_url=config.thread_url)
343
+ time.sleep(1)
344
+ post(message=message, thread_url=config.thread_url, post_url=config.post_url, quoted_by=msg['messengerName'], quote_text=originalQuote, quote_source=msg['messageID'])
345
+
346
+ time.sleep(10) # Standby time for server load release
347
+ return 0 # All is good
348
+ except:
349
+ print('Bad Connection')
350
+ return -1 # Error
351
 
352
 
353
  if __name__ == '__main__':
 
355
  # Start the scheduler
356
  while True:
357
  print('Starting Session')
358
+ result = WarOnlineBot()
359
 
360
  # Debug Only:
361
  #imgWord = 'как выглядит'
 
365
  post(message="", thread_url=config.thread_url, post_url=config.post_url, quoted_by='Test',
366
  quote_text="posting an image",img_url='https://replicate.delivery/pbxt/knKBiJt8DPZ0B1o25PaLJSZjgv3D5HcwLoBIn0JESbe3nISIA/out-0.png')
367
  """
368
+ if result == 0: # Good result
369
+ print('Session finished. Timeout...')
370
 
371
+ timer = range(60 * config.timeout)
372
+ for t in timer:
373
+ time.sleep(1)
374
+ else:
375
+ # Rerunning Session
376
+ time.sleep(10) # Standby time for server load release
config.py CHANGED
@@ -35,7 +35,7 @@ MaxWords = 50 # The server is relatively weak to fast-process the long messages
35
  remaining_messages = 2
36
 
37
  # Time between the reply sessions:
38
- timeout = 5 # min
39
 
40
  # Call for image generation:
41
  imgWord = 'как выглядит'
 
35
  remaining_messages = 2
36
 
37
  # Time between the reply sessions:
38
+ timeout = 3 # min
39
 
40
  # Call for image generation:
41
  imgWord = 'как выглядит'