Upload 2 files
Browse filesUpdated algorithmics - candidate
- WarClient.py +14 -10
- WarOnline_Chat_test.py +59 -32
WarClient.py
CHANGED
|
@@ -11,14 +11,18 @@ def getReply(message):
|
|
| 11 |
message = message.rstrip(".")
|
| 12 |
|
| 13 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
|
| 14 |
-
client_socket.connect((HOST, PORT))
|
| 15 |
-
client_socket.sendall(message.encode())
|
| 16 |
-
print('Wait...')
|
| 17 |
-
data = client_socket.recv(1024)
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
print('
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
message = message.rstrip(".")
|
| 12 |
|
| 13 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
+
client_socket.connect((HOST, PORT))
|
| 16 |
+
client_socket.sendall(message.encode())
|
| 17 |
+
print('Wait...')
|
| 18 |
+
data = client_socket.recv(1024)
|
| 19 |
+
try:
|
| 20 |
+
return data.decode('utf-8')
|
| 21 |
+
except: #sometimes there is a problem with the decoding
|
| 22 |
+
print('Decoding Error')
|
| 23 |
+
return ""
|
| 24 |
+
finally:
|
| 25 |
+
client_socket.shutdown(socket.SHUT_RDWR)
|
| 26 |
+
client_socket.close()
|
| 27 |
+
except:
|
| 28 |
+
return ""
|
WarOnline_Chat_test.py
CHANGED
|
@@ -26,11 +26,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 |
|
|
@@ -72,6 +76,7 @@ def post(message=message, thread_url=thread_url, post_url=post_url, quoted_by=""
|
|
| 72 |
|
| 73 |
# Extract the _xfToken value from the hidden form field
|
| 74 |
xf_token = soup.find('input', {'name': '_xfToken'}).get('value')
|
|
|
|
| 75 |
|
| 76 |
# Construct the message data for the POST request
|
| 77 |
message_data = {
|
|
@@ -170,49 +175,71 @@ def getMessages(thread_url=thread_url, quotedUser="", startingPage=1):
|
|
| 170 |
lastPage = True
|
| 171 |
|
| 172 |
return allquotes
|
|
|
|
|
|
|
| 173 |
def WarOnlineBot():
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
for msg in allMessages:
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
message = ""
|
| 185 |
-
if not replies: # Meaning that there are no answers
|
| 186 |
while not message:
|
| 187 |
-
message = WarClient.getReply(message=quote
|
| 188 |
-
|
| 189 |
-
print('
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
time.sleep(
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
if __name__ == '__main__':
|
| 196 |
timeout = 2 # min
|
| 197 |
|
| 198 |
-
|
| 199 |
-
print("logged in")
|
| 200 |
-
|
| 201 |
-
### Disabled ###
|
| 202 |
-
#post(message=message, thread_url=thread_url, post_url=post_url, quoted_by='Василий Пупкин', quote_text='quoted message', quote_source='3926006')
|
| 203 |
|
| 204 |
-
#
|
| 205 |
-
|
| 206 |
-
repliedMessagesIDs = []
|
| 207 |
|
| 208 |
-
for msg in allMessages:
|
| 209 |
-
# Set a list of replied IDs
|
| 210 |
-
repliedMessagesIDs.append(msg['quotedID'].split(': ')[-1])
|
| 211 |
-
print(msg)
|
| 212 |
-
# remove empty elements
|
| 213 |
-
repliedMessagesIDs = [elem for elem in repliedMessagesIDs if elem]
|
| 214 |
|
| 215 |
-
print(repliedMessagesIDs)
|
| 216 |
"""
|
| 217 |
# Start the scheduler
|
| 218 |
while True:
|
|
|
|
| 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 |
|
|
|
|
| 76 |
|
| 77 |
# Extract the _xfToken value from the hidden form field
|
| 78 |
xf_token = soup.find('input', {'name': '_xfToken'}).get('value')
|
| 79 |
+
print(xf_token)
|
| 80 |
|
| 81 |
# Construct the message data for the POST request
|
| 82 |
message_data = {
|
|
|
|
| 175 |
lastPage = True
|
| 176 |
|
| 177 |
return allquotes
|
| 178 |
+
|
| 179 |
+
# Core Engine of the Client
|
| 180 |
def WarOnlineBot():
|
| 181 |
+
|
| 182 |
+
login(username=username, password=password, thread_url=thread_url)
|
| 183 |
+
#print("logged in")
|
| 184 |
+
|
| 185 |
+
# All messages (with quotes) by ALL users:
|
| 186 |
+
allMessages = getMessages(thread_url=thread_url, quotedUser='', startingPage=1)
|
| 187 |
+
|
| 188 |
+
# IDs of the quoted messages, for messages posted by bot:
|
| 189 |
+
messages_by_bot_IDs = []
|
| 190 |
+
|
| 191 |
for msg in allMessages:
|
| 192 |
+
# Set a list of replied IDs
|
| 193 |
+
if msg['messengerName'] == username:
|
| 194 |
+
messages_by_bot_IDs.append(msg['quotedID'].split(': ')[-1])
|
| 195 |
+
# remove empty elements
|
| 196 |
+
messages_by_bot_IDs = [elem for elem in messages_by_bot_IDs if elem]
|
| 197 |
+
|
| 198 |
+
# All messages (with quotes) sent _FOR_ the Bot:
|
| 199 |
+
messagesForBot = getMessages(thread_url=thread_url, quotedUser=username, startingPage=1)
|
| 200 |
+
|
| 201 |
+
# IDs of the messages, quoting the bot:
|
| 202 |
+
messages_for_bot_IDs = []
|
| 203 |
+
|
| 204 |
+
for msg in messagesForBot:
|
| 205 |
+
# Set a list of posted message IDs
|
| 206 |
+
messages_for_bot_IDs.append(msg['messageID'].split('-')[-1])
|
| 207 |
+
# remove empty elements
|
| 208 |
+
messages_for_bot_IDs = [elem for elem in messages_for_bot_IDs if elem]
|
| 209 |
+
|
| 210 |
+
# Filter the unanswered messages
|
| 211 |
+
for msgID in messages_for_bot_IDs:
|
| 212 |
+
if msgID in messages_by_bot_IDs:
|
| 213 |
+
messages_for_bot_IDs.remove(msgID)
|
| 214 |
|
| 215 |
+
# print unanswered messages
|
| 216 |
+
for msg in messagesForBot:
|
| 217 |
+
if msg['messageID'].split('-')[-1] in messages_for_bot_IDs:
|
| 218 |
+
|
| 219 |
+
quote = remove_non_english_russian_chars(msg['reply'])
|
| 220 |
+
message = ""
|
| 221 |
|
|
|
|
|
|
|
| 222 |
while not message:
|
| 223 |
+
message = WarClient.getReply(message=quote)
|
| 224 |
+
|
| 225 |
+
print('Quote: ', quote)
|
| 226 |
+
print('Reply: ', message)
|
| 227 |
+
|
| 228 |
+
login(username=username, password=password, thread_url=thread_url)
|
| 229 |
+
time.sleep(1)
|
| 230 |
+
#post(message=message, thread_url=thread_url, post_url=post_url, quoted_by=msg['messengerName'], quote_text=quote, quote_source=msg['messageID'])
|
| 231 |
+
|
| 232 |
+
time.sleep(200) # Standby time for server load release
|
| 233 |
|
| 234 |
if __name__ == '__main__':
|
| 235 |
timeout = 2 # min
|
| 236 |
|
| 237 |
+
WarOnlineBot()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
+
#login(username=username, password=password, thread_url=thread_url)
|
| 240 |
+
#post(message=message, thread_url=thread_url, post_url=post_url, quoted_by='WarBot', quote_text='quoted message', quote_source='12345')
|
|
|
|
| 241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
|
|
|
| 243 |
"""
|
| 244 |
# Start the scheduler
|
| 245 |
while True:
|