-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathforums.py
247 lines (201 loc) · 8.21 KB
/
forums.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import http.cookiejar
from urllib import request, parse
from bs4 import BeautifulSoup
#Forum object, created for mybb forum software.
class forum(object):
def __init__(self, ip, ssl = False, port = None):
self._ip = ip
if ssl:
self._protocal = 'https'
else:
self._protocal = 'http'
self._port = port
cj = http.cookiejar.CookieJar()
self._cproc = request.HTTPCookieProcessor(cj)
self._opener = request.build_opener(self._cproc)
@property
def ip(self):
return self._ip
#executes an http POST; returns http response
def _open(self, url, data = {}):
self.lastRequest = url
if data == {}:
p_data = None
else:
p_data = parse.urlencode(data).encode()
return self._opener.open("{}://{}{}".format(self._protocal, self._ip, url), data = p_data)
def openPage(self, url):
return self._open(url)
def login(self, username, password, url):
self._login = True
self.username = username
DATA = {
"url": "{}://{}{}".format(self._protocal, self._ip, url),
"action": "do_login",
"submit": "Login",
"quick_login": "1",
"quick_username": username,
"quick_password": password
}
return self._open('/index.php', DATA)
#searches the given string on the forums. Requeres body & header, resturn a named tuple of the results
def search(self, searchUrl, params, header = None):
#Search from search page
header = header or self._defaultHeader
try:
self.resp = self._open(searchUrl, params)
except request.HTTPError:
raise self.NoPageFound('Bad response status')
#get URL from redirect
soup = BeautifulSoup(self.resp.read(), 'html.parser')
url = soup.find('a').get('href')
if url is None:
raise self.NoRedirect('Redirect link not found.')
#load search results
try:
self.resp = self._open('/{}'.format(url))
except request.HTTPError:
raise self.NoPageFound('Bad responsen status')
self.searchResults = self._parseResults(self.resp.read())
#Parses the html from the result page; returns named tuple of the results
def _parseResults(self, html):
results = []
#tuple = namedtuple('searchResults', ['thread', 'forum', 'author', 'lastreplier', 'lastreplytime'])
soup = BeautifulSoup(html, 'html.parser')
rows = soup.findAll('tr', class_ = 'inline_row')
for row in rows:
lines = row.findAll('td')
line = lines[2].find(class_ = ' subject_old')
if line is None: line = lines[2].find('span', class_ = " subject_editable subject_old")
title = self._parseHref(line)
line = lines[2].find(class_ = 'author smalltext').find('a')
author = self._parseHref(line)
line = lines[3].find('a')
forum_ = self._parseHref(line)
line = lines[6].findAll('a')[-1]
lastreplier = self._parseHref(line)
lastreplytime = lines[6].find('span').getText().split()[:2]
view_count = lines[5].getText()
reply_count = lines[4].getText()
results.append(ThreadList(forum_, title, author, reply_count, view_count, lastreplier, lastreplytime))
return results
def parseThreadList(self, html):
results = []
#tuple = namedtuple('searchResults', ['thread', 'forum', 'author', 'lastreplier', 'lastreplytime'])
soup = BeautifulSoup(html, 'html.parser')
forum_ = soup.find('div', class_ = 'navigation').find('span').getText()
rows = soup.findAll('tr', class_ = 'inline_row')
for row in rows:
lines = row.findAll('td')
line = lines[2]
line = line.find('span', class_ = " subject_new")
if line is None: line = line.find('span', class_ = " subject_old")
if line is None: line = line.find('span', class_ = " subject_editable subject_new")
if line is None: line = line.find('span', class_ = " subject_editable subject_old")
line = line.find('a')
title = self._parseHref(line)
line = lines[2+offset].find(class_ = 'author smalltext').find('a')
author = self._parseHref(line)
line = lines[5].findAll('a')[-1]
lastreplier = self._parseHref(line)
lastreplytime = lines[5].find('span').getText().split()[:2]
view_count = lines[4].getText()
reply_count = lines[3].getText()
results.append(ThreadList((forum_, self.lastRequest), title, author, reply_count, view_count, lastreplier, lastreplytime))
return results
def parseThreadPage(self, html):
results = []
soup = BeautifulSoup(html, 'html.parser')
posts = soup.findAll('div', class_ = 'post ')
for post in posts:
line = post.find('div', class_ = 'author_information')
poster = [line.findChild().getText()]
poster.append(line.find('a').get('href'))
time = post.find('span', class_ = 'post_date').getText().split('(')[0].strip()
text = post.find('div', class_ = 'post_body scaleimages').getText().strip()
signature = post.find('div', class_ = 'signature scaleimages').getText().strip()
results.append(Post(poster, time, text, signature))
#Parse html, returns tuple containing readable text and the URL
def _parseHref(self, line):
return (line.getText(), line.get('href'))
#Generates the default search parameters.
def genSearchParams(self, keywords):
params = self._defaultParams
params['keywords'] = keywords
return params
#Default search paramters, without the searchterm
_defaultParams = {'submit': 'Search',
'sortordr': 'desc',
'sortby': 'lastpost',
'showresults': 'threads',
'postthread': '1',
'postdate': '0',
'pddir': '1',
'keywords': None,
'forum[]': '1',
'findthreadst': '1',
'action': 'do_search' }
#Default header
_defaultHeader = {"Content-type": "application/x-www-form-urlencoded",
"submit": "text/plain"}
#Exception raised when http response does not have status 200
class NoPageFound(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
#Exception raised when there is no URL found on the redirect page.
#This occurs when searching
class NoRedirect(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class Post(object):
def __init__(self, poster, time, text, signature):
self._poster = poster
self._time = time
self._text = text
self._signature = signature
@property
def poster(self):
return self._poster
@property
def signature(self):
return self._signature
@property
def text(self):
return self._text
@property
def time(self):
return self._time
class ThreadList(object):
def __init__(self, forum, title, author, reply_count, view_count, last_poster, last_post_time):
self._title = title
self._author = author
self._forum = forum
self._replc = reply_count
self._viewc = reply_count
self._lastpr = last_poster
self._lastpt = last_post_time
@property
def forum(self):
return self._forum
@property
def title(self):
return self._title
@property
def author(self):
return self._author
@property
def reply_count(self):
return self._replc
@property
def view_count(self):
return self._viewc
@property
def last_replier(self):
return self._lastpr
@property
def last_reply_time(self):
return self._lastpt