kertser commited on
Commit
4c3086f
1 Parent(s): c981d80

Upload 2 files

Browse files

Changed several methods. Ready v0

Files changed (2) hide show
  1. WarClient.py +18 -11
  2. 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 = "Если посмотреть на небо, можно увидеть как улетает башня от Т-72"
 
7
 
8
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
9
- client_socket.connect((HOST, PORT))
10
- client_socket.sendall(message.encode())
11
- print('Wait...')
12
- data = client_socket.recv(1024)
13
- try:
14
- received_string = data.decode('utf-8')
15
- print(f'Received string from server: {received_string}')
16
- except: #sometimes there is a problem with the decoding
17
- print('decoding error, please try again')
 
 
 
 
 
 
 
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 readQuote(thread_url=thread_url, username=username):
109
- # Retrieve the content of the specific thread
 
110
 
111
- response = session.get(thread_url)
112
- html_content = response.content
113
 
114
  # Initial values for messangerName and the message ID
115
  messengerName = ""
116
  messageID = ""
117
 
118
- # Parse the HTML content using BeautifulSoup
119
- soup = BeautifulSoup(html_content, 'html.parser')
 
120
 
121
- # Find all the quotes in the thread
122
- quotes = soup.find_all('div', {'class': 'bbWrapper'})
123
- messageData = soup.find_all('div', {'class': 'message-userContent'})
124
 
125
- # Check each quote if it contains your name and keep track of the latest one
126
- latest_quote = None
127
- for quote in quotes:
128
- if username in quote.text:
129
- latest_quote = quote
 
130
 
131
- if latest_quote:
132
- for data in messageData:
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
- # Get the messager username
139
- matchName = namePattern.search(str(data))
140
- if matchName:
141
- messengerName = matchName.group(1)
 
 
 
142
 
143
- # Get the message ID
144
- matchID = messageIDPattern.search(str(data))
145
- if matchID:
146
- messageID = matchID.group(1)
147
 
148
- reply = latest_quote.text.split("Click to expand...")[-1].replace('\n', ' ').strip()
149
- return {'reply':reply,'messengerName':messengerName,'messageID':messageID}
 
 
 
 
 
 
 
 
 
 
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
- quote = readQuote(thread_url=get_last_page_of_thread(thread_url,1), username=username)
156
- messangerQuote = readQuote(thread_url=get_last_page_of_thread(thread_url,1),username=quote['messengerName'])
157
- print(quote)
158
- print(messangerQuote)
159
- # The task is to fine all the unanswered quotes
 
 
 
 
 
 
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']))