Upload 2 files
Browse filesChanged several methods. Ready v0
- WarClient.py +18 -11
- WarOnline_Chat.py +54 -52
WarClient.py
CHANGED
|
@@ -3,15 +3,22 @@ import socket
|
|
| 3 |
HOST = '129.159.146.88'
|
| 4 |
PORT = 5000
|
| 5 |
|
| 6 |
-
message
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
print(
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
HOST = '129.159.146.88'
|
| 4 |
PORT = 5000
|
| 5 |
|
| 6 |
+
def getReply(message):
|
| 7 |
+
# Returns a reply from the server
|
| 8 |
|
| 9 |
+
# Remove the finel "." - somehow it stucks the model
|
| 10 |
+
if message.endswith("."):
|
| 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 |
+
return data.decode('utf-8')
|
| 20 |
+
except: #sometimes there is a problem with the decoding
|
| 21 |
+
print('decoding error, please try again')
|
| 22 |
+
finally:
|
| 23 |
+
client_socket.shutdown(socket.SHUT_RDWR)
|
| 24 |
+
client_socket.close()
|
WarOnline_Chat.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
# This is an quote and post library for a specific thread in the WarOnline forum.
|
| 2 |
|
|
|
|
| 3 |
import requests
|
| 4 |
import re
|
| 5 |
from bs4 import BeautifulSoup
|
|
@@ -27,23 +28,6 @@ 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 get_last_page_of_thread(thread_url=thread_url, startingPage=1):
|
| 31 |
-
#Returns a link to the last page of the thread
|
| 32 |
-
page = startingPage #Counter
|
| 33 |
-
lastPage = False
|
| 34 |
-
while not lastPage:
|
| 35 |
-
response = requests.get(thread_url + 'page-' + str(page))
|
| 36 |
-
if response.status_code == 200:
|
| 37 |
-
pass # do something (optional)
|
| 38 |
-
if not compare_pages(thread_url + 'page-' + str(page), thread_url + 'page-' + str(page + 1)):
|
| 39 |
-
page += 1
|
| 40 |
-
else:
|
| 41 |
-
lastPage = True
|
| 42 |
-
else:
|
| 43 |
-
lastPage = True
|
| 44 |
-
|
| 45 |
-
return (thread_url + 'page-' + str(page))
|
| 46 |
-
|
| 47 |
def login(username=username, password=password, thread_url=thread_url):
|
| 48 |
# Log-In to the forum and redirect to thread
|
| 49 |
|
|
@@ -105,55 +89,73 @@ def post(message=message, thread_url=thread_url, post_url=post_url, quoted_by=""
|
|
| 105 |
|
| 106 |
print('Post submitted successfully.')
|
| 107 |
|
| 108 |
-
def
|
| 109 |
-
#
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
|
| 114 |
# Initial values for messangerName and the message ID
|
| 115 |
messengerName = ""
|
| 116 |
messageID = ""
|
| 117 |
|
| 118 |
-
#
|
| 119 |
-
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
if latest_quote.text in data.text:
|
| 134 |
-
# Patterns to search in the last quote.
|
| 135 |
-
namePattern = re.compile('data-lb-caption-desc="(.*?) ·')
|
| 136 |
-
messageIDPattern = re.compile('data-lb-id="(.*?)"')
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
|
|
|
| 151 |
|
| 152 |
if __name__ == '__main__':
|
| 153 |
login(username=username, password=password, thread_url=thread_url)
|
| 154 |
#post(message=message, thread_url=thread_url, post_url=post_url,quoted_by='Василий Пупкин',quote_text='Testing the XenForo response mechanism')
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# This is an quote and post library for a specific thread in the WarOnline forum.
|
| 2 |
|
| 3 |
+
import WarClient2
|
| 4 |
import requests
|
| 5 |
import re
|
| 6 |
from bs4 import BeautifulSoup
|
|
|
|
| 28 |
#Compares 2 pages and returns True if they are the same
|
| 29 |
return urllib.urlopen(url1).geturl() == urllib.urlopen(url2).geturl()
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def login(username=username, password=password, thread_url=thread_url):
|
| 32 |
# Log-In to the forum and redirect to thread
|
| 33 |
|
|
|
|
| 89 |
|
| 90 |
print('Post submitted successfully.')
|
| 91 |
|
| 92 |
+
def allQuotesFor(thread_url=thread_url, username=username, startingPage=1):
|
| 93 |
+
# Returns all the quotes for #username in the specific multi-page thread url
|
| 94 |
+
allquotes =[]
|
| 95 |
|
| 96 |
+
page = startingPage # Counter
|
| 97 |
+
lastPage = False
|
| 98 |
|
| 99 |
# Initial values for messangerName and the message ID
|
| 100 |
messengerName = ""
|
| 101 |
messageID = ""
|
| 102 |
|
| 103 |
+
# Patterns to search in the last quote.
|
| 104 |
+
namePattern = re.compile('data-lb-caption-desc="(.*?) ·')
|
| 105 |
+
messageIDPattern = re.compile('data-lb-id="(.*?)"')
|
| 106 |
|
| 107 |
+
while not lastPage:
|
| 108 |
+
response = requests.get(thread_url + 'page-' + str(page))
|
| 109 |
+
if response.status_code == 200:
|
| 110 |
|
| 111 |
+
# Payload of the function
|
| 112 |
+
response = session.get(thread_url)
|
| 113 |
+
html_content = response.content
|
| 114 |
+
|
| 115 |
+
# Parse the HTML content using BeautifulSoup
|
| 116 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
| 117 |
|
| 118 |
+
# Find all the message in the thread page
|
| 119 |
+
messageData = soup.find_all('div', {'class': 'message-userContent'})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
for data in messageData:
|
| 122 |
+
if username in data.text:
|
| 123 |
+
try:
|
| 124 |
+
# Get the messager username
|
| 125 |
+
matchName = namePattern.search(str(data))
|
| 126 |
+
if matchName:
|
| 127 |
+
messengerName = matchName.group(1)
|
| 128 |
|
| 129 |
+
# Get the message ID
|
| 130 |
+
matchID = messageIDPattern.search(str(data))
|
| 131 |
+
if matchID:
|
| 132 |
+
messageID = matchID.group(1)
|
| 133 |
|
| 134 |
+
reply = data.text.split("Click to expand...")[-1].replace('\n', ' ').strip()
|
| 135 |
+
allquotes.append({'reply': reply, 'messengerName': messengerName, 'messageID': messageID})
|
| 136 |
+
except:
|
| 137 |
+
continue # There was no text in quote, move next
|
| 138 |
+
|
| 139 |
+
#check if that is not a last page
|
| 140 |
+
if not compare_pages(thread_url + 'page-' + str(page), thread_url + 'page-' + str(page + 1)):
|
| 141 |
+
page += 1
|
| 142 |
+
else:
|
| 143 |
+
lastPage = True
|
| 144 |
+
else:
|
| 145 |
+
lastPage = True
|
| 146 |
|
| 147 |
+
return allquotes
|
| 148 |
|
| 149 |
if __name__ == '__main__':
|
| 150 |
login(username=username, password=password, thread_url=thread_url)
|
| 151 |
#post(message=message, thread_url=thread_url, post_url=post_url,quoted_by='Василий Пупкин',quote_text='Testing the XenForo response mechanism')
|
| 152 |
+
|
| 153 |
+
# Get All Quotes
|
| 154 |
+
quotes = allQuotesFor(thread_url=thread_url, username=username, startingPage=1)
|
| 155 |
+
|
| 156 |
+
# Search for quotes withou the relpy and answer them
|
| 157 |
+
for quote in quotes:
|
| 158 |
+
reply = allQuotesFor(thread_url=thread_url, username=quote['messengerName'], startingPage=1)
|
| 159 |
+
if not reply:
|
| 160 |
+
print('Quote: ',quote['reply'])
|
| 161 |
+
print(WarClient2.getReply(message=quote['reply']))
|