kertser commited on
Commit
00d90ae
1 Parent(s): 5193efa

Upload WarOnline_Chat.py

Browse files
Files changed (1) hide show
  1. WarOnline_Chat.py +28 -4
WarOnline_Chat.py CHANGED
@@ -1,6 +1,7 @@
1
  # This is an quote and post library for a specific thread in the WarOnline forum.
2
 
3
  import requests
 
4
  from bs4 import BeautifulSoup
5
  import urllib.request as urllib
6
  import warnings
@@ -105,14 +106,20 @@ def post(message=message, thread_url=thread_url, post_url=post_url, quoted_by=""
105
 
106
  def readQuote(thread_url=thread_url, username=username):
107
  # Retrieve the content of the specific thread
 
108
  response = session.get(thread_url)
109
  html_content = response.content
110
 
 
 
 
 
111
  # Parse the HTML content using BeautifulSoup
112
  soup = BeautifulSoup(html_content, 'html.parser')
113
 
114
  # Find all the quotes in the thread
115
  quotes = soup.find_all('div', {'class': 'bbWrapper'})
 
116
 
117
  # Check each quote if it contains your name and keep track of the latest one
118
  latest_quote = None
@@ -121,15 +128,32 @@ def readQuote(thread_url=thread_url, username=username):
121
  latest_quote = quote
122
 
123
  if latest_quote:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  reply = latest_quote.text.split("Click to expand...")[-1].replace('\n', ' ').strip()
125
- print(reply)
126
 
127
 
128
  if __name__ == '__main__':
129
  login(username=username, password=password, thread_url=thread_url)
130
- post(message=message, thread_url=thread_url, post_url=post_url,quoted_by='Василий Пупкин',quote_text='Testing the XenForo response mechanism')
131
- #readQuote(thread_url=get_last_page_of_thread(thread_url,1))
132
- #! readQuote shall also return the quoted message number. TBD.
 
133
  # The task is to fine the last unanswered quote
134
  # Answered quote = there is a post with reply by WarBot
135
  # The answer shall be with blockQuote
 
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
6
  import urllib.request as urllib
7
  import warnings
 
106
 
107
  def readQuote(thread_url=thread_url, username=username):
108
  # Retrieve the content of the specific thread
109
+
110
  response = session.get(thread_url)
111
  html_content = response.content
112
 
113
+ # Initial values for messangerName and the message ID
114
+ messengerName = ""
115
+ messageID = ""
116
+
117
  # Parse the HTML content using BeautifulSoup
118
  soup = BeautifulSoup(html_content, 'html.parser')
119
 
120
  # Find all the quotes in the thread
121
  quotes = soup.find_all('div', {'class': 'bbWrapper'})
122
+ messageData = soup.find_all('div', {'class': 'message-userContent'})
123
 
124
  # Check each quote if it contains your name and keep track of the latest one
125
  latest_quote = None
 
128
  latest_quote = quote
129
 
130
  if latest_quote:
131
+ for data in messageData:
132
+ if latest_quote.text in data.text:
133
+ # Patterns to search in the last quote.
134
+ namePattern = re.compile('data-lb-caption-desc="(.*?) ·')
135
+ messageIDPattern = re.compile('data-lb-id="(.*?)"')
136
+
137
+ # Get the messager username
138
+ matchName = namePattern.search(str(data))
139
+ if matchName:
140
+ messengerName = matchName.group(1)
141
+
142
+ # Get the message ID
143
+ matchID = messageIDPattern.search(str(data))
144
+ if matchID:
145
+ messageID = matchID.group(1)
146
+
147
  reply = latest_quote.text.split("Click to expand...")[-1].replace('\n', ' ').strip()
148
+ return {'reply':reply,'messengerName':messengerName,'messageID':messageID}
149
 
150
 
151
  if __name__ == '__main__':
152
  login(username=username, password=password, thread_url=thread_url)
153
+ #post(message=message, thread_url=thread_url, post_url=post_url,quoted_by='Василий Пупкин',quote_text='Testing the XenForo response mechanism')
154
+ quote = readQuote(thread_url=get_last_page_of_thread(thread_url,1))
155
+ print(quote)
156
+
157
  # The task is to fine the last unanswered quote
158
  # Answered quote = there is a post with reply by WarBot
159
  # The answer shall be with blockQuote