kertser commited on
Commit
3291ddb
1 Parent(s): 10a95dd

Upload WarOnline_Chat.py

Browse files

Updated the Quote and Post methods. Added last-page find function to search for the quotes.

Files changed (1) hide show
  1. WarOnline_Chat.py +40 -6
WarOnline_Chat.py CHANGED
@@ -2,14 +2,17 @@
2
 
3
  import requests
4
  from bs4 import BeautifulSoup
 
 
 
5
 
6
  # Define the login URL and the thread URL
7
  login_url = 'https://waronline.org/fora/index.php?login/login'
8
  thread_url = 'https://waronline.org/fora/index.php?threads/warbot-playground.17636/'
9
  post_url = "https://waronline.org/fora/index.php?threads/warbot-playground.17636/add-reply"
10
 
11
- # Sending the message
12
- message = "Hello World"
13
 
14
  # Define the login credentials
15
  username = 'WarBot'
@@ -18,6 +21,28 @@ password = 'naP2tion'
18
  # Start a session to persist the login cookie across requests
19
  session = requests.Session()
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def login(username=username, password=password, thread_url=thread_url):
22
  # Log-In to the forum and redirect to thread
23
 
@@ -41,8 +66,13 @@ def login(username=username, password=password, thread_url=thread_url):
41
  print('Login failed!')
42
  exit()
43
 
44
- def post(message=message, thread_url=thread_url, post_url=post_url):
45
- #Post a message to the forum
 
 
 
 
 
46
 
47
  # Retrieve the thread page HTML
48
  response = session.get(thread_url)
@@ -97,5 +127,9 @@ def readQuote(thread_url=thread_url, username=username):
97
 
98
  if __name__ == '__main__':
99
  login(username=username, password=password, thread_url=thread_url)
100
- #post(message=message, thread_url=thread_url, post_url=post_url)
101
- readQuote(thread_url=thread_url)
 
 
 
 
 
2
 
3
  import requests
4
  from bs4 import BeautifulSoup
5
+ import urllib.request as urllib
6
+ import warnings
7
+ warnings.filterwarnings("ignore")
8
 
9
  # Define the login URL and the thread URL
10
  login_url = 'https://waronline.org/fora/index.php?login/login'
11
  thread_url = 'https://waronline.org/fora/index.php?threads/warbot-playground.17636/'
12
  post_url = "https://waronline.org/fora/index.php?threads/warbot-playground.17636/add-reply"
13
 
14
+ # Sending the message (change that!)
15
+ message = "Test"
16
 
17
  # Define the login credentials
18
  username = 'WarBot'
 
21
  # Start a session to persist the login cookie across requests
22
  session = requests.Session()
23
 
24
+
25
+ def compare_pages(url1, url2):
26
+ #Compares 2 pages and returns True if they are the same
27
+ return urllib.urlopen(url1).geturl() == urllib.urlopen(url2).geturl()
28
+
29
+ def get_last_page_of_thread(thread_url=thread_url, startingPage=1):
30
+ #Returns a link to the last page of the thread
31
+ page = startingPage #Counter
32
+ lastPage = False
33
+ while not lastPage:
34
+ response = requests.get(thread_url + 'page-' + str(page))
35
+ if response.status_code == 200:
36
+ pass # do something (optional)
37
+ if not compare_pages(thread_url + 'page-' + str(page), thread_url + 'page-' + str(page + 1)):
38
+ page += 1
39
+ else:
40
+ lastPage = True
41
+ else:
42
+ lastPage = True
43
+
44
+ return (thread_url + 'page-' + str(page))
45
+
46
  def login(username=username, password=password, thread_url=thread_url):
47
  # Log-In to the forum and redirect to thread
48
 
 
66
  print('Login failed!')
67
  exit()
68
 
69
+ def post(message=message, thread_url=thread_url, post_url=post_url, quoted_by="",quote_text=""):
70
+ #Post a message to the forum (with or without the quote
71
+ quote_source = '3920987' #quoted message source (TBD)
72
+
73
+ if quoted_by:
74
+ message = f'[QUOTE="{quoted_by}, post: {quote_source}"]{quote_text}[/QUOTE]{message}'
75
+ # optionally add @{quoted_by} to indent the quoter
76
 
77
  # Retrieve the thread page HTML
78
  response = session.get(thread_url)
 
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