-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathscrapers.py
333 lines (285 loc) · 14.4 KB
/
scrapers.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import asyncio
import sys
from operator import itemgetter
from re import compile
import aiohttp
import validators
from lxml import html
from tqdm.asyncio import tqdm
class Error(Exception):
pass
class NoCourseInfoException(Error):
def __init__(self, message="No information about the course has been given to the Scraper class"):
self.message = message
super().__init__(self.message)
class Scraper:
def __init__(self, course):
if course is None:
raise NoCourseInfoException
self.__useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' \
'AppleWebKit/537.36 (KHTML, like Gecko) ' \
'Chrome/86.0.4240.75 Safari/537.36'
self.__common_updated_xpath = "//strong[contains(text(),'Last updated ')]"
self.__regex = compile("magnet:\?xt=urn:btih:[a-zA-Z0-9]*")
if sys.platform == "win32":
self.loop = asyncio.ProactorEventLoop()
else:
self.loop = asyncio.SelectorEventLoop()
asyncio.set_event_loop(self.loop)
if validators.url(course):
self._course_url = course
self.course_name = self.loop.run_until_complete(self.get_course_name())
else:
print("The value entered is not a link, taking it as the course name.\n")
self.course_name = course
self.__mod_name = self.course_name.replace("-", "")
self.__scraping_funcs = [self.__freecoursesite,
self.__getfreecourses,
self.__freecourseudemy,
self.__paidcoursesforfree,
self.__desirecourse,
self.__tutorialsplanet,
self.__myfreecourses,
self.__udemy24,
self.__freeallcourse,
self.__freecoursesdownload,
self.__freetutorialsudemy]
async def get_course_name(self):
async with aiohttp.ClientSession(headers={"User-Agent": self.__useragent}, timeout=aiohttp.ClientTimeout(10)) \
as session:
async with session.get(self._course_url) as response:
response = await response.read()
doc = html.fromstring(response)
try:
course_name = doc.xpath("//h1[contains(@class,'clp-lead__title')]")[0].text.strip()
except IndexError:
course_name = None
return course_name
def search_course(self):
results = self.loop.run_until_complete(self.__search_course())
if results:
results = sorted(results, key=itemgetter("year", "month"), reverse=True)
return results
async def __search_course(self):
results = []
async with aiohttp.ClientSession(headers={"User-Agent": self.__useragent}, timeout=aiohttp.ClientTimeout(10)) \
as session:
tasks = tuple(func(session) for func in self.__scraping_funcs)
for func in tqdm.as_completed(tasks, loop=self.loop):
result = await func
if result:
last_updated = [int(i) for i in result['last_updated'].split('Last updated ')[1].split('/')]
if len(str(last_updated[0])) < len(str(last_updated[1])):
last_updated = last_updated[::-1]
result['year'], result['month'] = last_updated
results.append(result)
return results
async def __freecoursesite(self, session):
async with session.get(f'https://freecoursesite.com/?s={self.__mod_name}') as response:
response = await response.read()
doc = html.fromstring(response)
search_results = doc.xpath(f"//a[contains(text(),'{self.__mod_name}')]")
if not search_results:
return
try:
async with session.get(search_results[0].get("href")) as response:
response = await response.read()
doc = html.fromstring(response)
download_link = doc.xpath("//strong[starts-with(text(),'Download Now')]/ancestor::a")[0].get('href')
last_updated = doc.xpath(self.__common_updated_xpath)[0].text
except (IndexError, KeyError):
return
magnet_links = self.__regex.findall(download_link)
if magnet_links:
download_link = magnet_links[0]
return {"link": download_link, "last_updated": last_updated, "website": 'freecoursesite.com'}
async def __getfreecourses(self, session):
async with session.get(f'https://getfreecourses.co/?s={self.__mod_name}') as response:
response = await response.read()
doc = html.fromstring(response)
search_results = doc.xpath(f"//a[contains(text(),'{self.__mod_name}')]")
if not search_results:
return
try:
async with session.get(search_results[0].get("href")) as response:
response = await response.read()
doc = html.fromstring(response)
download_link = doc.xpath("//a[starts-with(text(),'DOWNLOAD TUTORIAL')]")[0].get('href')
last_updated = doc.xpath("//div[starts-with(text(),'Last updated ')]")[0].text
except (IndexError, KeyError):
return
magnet_links = self.__regex.findall(download_link)
if magnet_links:
download_link = magnet_links[0]
return {"link": download_link, "last_updated": last_updated, "website": 'getfreecourses.co'}
async def __freecourseudemy(self, session):
async with session.get(f'https://freecourseudemy.com/?s={self.__mod_name}') as response:
response = await response.read()
doc = html.fromstring(response)
search_results = doc.xpath(f"//a[contains(text(),'{self.__mod_name}')]")
if not search_results:
return
try:
async with session.get(search_results[0].get("href")) as response:
response = await response.read()
doc = html.fromstring(response)
download_link = doc.xpath("//a[starts-with(text(),'DOWNLOAD COURSE')]")[0].get('href')
last_updated = doc.xpath(self.__common_updated_xpath)[0].text
except (IndexError, KeyError):
return
magnet_links = self.__regex.findall(download_link)
if magnet_links:
download_link = magnet_links[0]
return {"link": download_link, "last_updated": last_updated, "website": 'freecourseudemy.com'}
async def __paidcoursesforfree(self, session):
async with session.get(f'https://paidcoursesforfree.com/?s={self.__mod_name}') as response:
response = await response.read()
doc = html.fromstring(response)
search_results = doc.xpath(f"//a[contains(text(),'{self.__mod_name}')]")
if not search_results:
return
try:
async with session.get(search_results[0].get("href")) as response:
response = await response.read()
doc = html.fromstring(response)
download_link = doc.xpath("//h1/a[@href]")[0].get('href')
last_updated = doc.xpath(self.__common_updated_xpath)[0].text
except (IndexError, KeyError):
return
magnet_links = self.__regex.findall(download_link)
if magnet_links:
download_link = magnet_links[0]
return {"link": download_link, "last_updated": last_updated, "website": 'paidcoursesforfree.com'}
async def __desirecourse(self, session):
async with session.get(f'https://desirecourse.net/?s={self.__mod_name}') as response:
response = await response.read()
doc = html.fromstring(response)
search_results = doc.xpath(f"//a[contains(text(),'{self.__mod_name}')]")
if not search_results:
return
try:
async with session.get(search_results[0].get("href")) as response:
response = await response.read()
doc = html.fromstring(response)
download_link = doc.xpath("//a[contains(text(),'DOWNLOAD COURSE')]")[0].get('href')
last_updated = doc.xpath(self.__common_updated_xpath)[0].text
except (IndexError, KeyError):
return
magnet_links = self.__regex.findall(download_link)
if magnet_links:
download_link = magnet_links[0]
return {"link": download_link, "last_updated": last_updated, "website": 'desirecourse.net'}
async def __tutorialsplanet(self, session):
async with session.get(f'https://tutorialsplanet.net/?s={self.__mod_name}') as response:
response = await response.read()
doc = html.fromstring(response)
search_results = doc.xpath(f"//a[contains(text(),'{self.__mod_name}')]")
if not search_results:
return
try:
async with session.get(search_results[0].get("href")) as response:
response = await response.read()
doc = html.fromstring(response)
download_link = doc.xpath("//div[@data-purpose='course-audience']/p/a")[0].get('href')
last_updated = doc.xpath(self.__common_updated_xpath)[0].text
except (IndexError, KeyError):
return
magnet_links = self.__regex.findall(download_link)
if magnet_links:
download_link = magnet_links[0]
return {"link": download_link, "last_updated": last_updated, "website": 'tutorialsplanet.net'}
async def __myfreecourses(self, session):
async with session.get(f'https://myfreecourses.com/?s={self.__mod_name}') as response:
response = await response.read()
doc = html.fromstring(response)
search_results = doc.xpath(f"//a[contains(text(),'{self.__mod_name}')]")
if not search_results:
return
try:
async with session.get(search_results[0].get("href")) as response:
response = await response.read()
doc = html.fromstring(response)
download_link = doc.xpath("//a[@id='download']")[0].get('href')
last_updated = doc.xpath(self.__common_updated_xpath)[0].text
except (IndexError, KeyError):
return
magnet_links = self.__regex.findall(download_link)
if magnet_links:
download_link = magnet_links[0]
return {"link": download_link, "last_updated": last_updated, "website": 'myfreecourses.com'}
async def __udemy24(self, session):
async with session.get(f'https://udemy24.com/?s={self.__mod_name}') as response:
response = await response.read()
doc = html.fromstring(response)
search_results = doc.xpath(f"//a[contains(text(),'{self.__mod_name}')]")
if not search_results:
return
try:
async with session.get(search_results[0].get("href")) as response:
response = await response.read()
doc = html.fromstring(response)
download_link = doc.xpath("//strong[contains(text(),'Download Course')]//ancestor::a")[0].get('href')
last_updated = doc.xpath("//strong[contains(text(),'Last updated : ')]")[0].text
except (IndexError, KeyError):
return
magnet_links = self.__regex.findall(download_link)
if magnet_links:
download_link = magnet_links[0]
return {"link": download_link, "last_updated": last_updated, "website": 'udemy24.com'}
async def __freeallcourse(self, session):
async with session.get(f'https://freeallcourse.com/?s={self.__mod_name}') as response:
response = await response.read()
doc = html.fromstring(response)
search_results = doc.xpath(f"//a[contains(text(),'{self.__mod_name}')]")
if not search_results:
return
try:
async with session.get(search_results[0].get("href")) as response:
response = await response.read()
doc = html.fromstring(response)
download_link = doc.xpath("//a[contains(text(),'Download Course')]")[0].get('href')
last_updated = doc.xpath("//strong/br")[0].tail
except (IndexError, KeyError):
return
magnet_links = self.__regex.findall(download_link)
if magnet_links:
download_link = magnet_links[0]
return {"link": download_link, "last_updated": last_updated, "website": 'freeallcourse.com'}
async def __freecoursesdownload(self, session):
async with session.get(f'https://freecoursesdownload.com/?s={self.__mod_name}') as response:
response = await response.read()
doc = html.fromstring(response)
search_results = doc.xpath(f"//a[contains(text(),'{self.__mod_name}')]")
if not search_results:
return
try:
async with session.get(search_results[0].get("href")) as response:
response = await response.read()
doc = html.fromstring(response)
download_link = doc.xpath("//a[contains(text(),'DOWNLOAD COURSE')]")[0].get('href')
last_updated = doc.xpath(self.__common_updated_xpath)[0].text
except (IndexError, KeyError):
return
magnet_links = self.__regex.findall(download_link)
if magnet_links:
download_link = magnet_links[0]
return {"link": download_link, "last_updated": last_updated, "website": 'freecoursesdownload.com'}
async def __freetutorialsudemy(self, session):
async with session.get(f'https://freetutorialsudemy.com/?s={self.__mod_name}') as response:
response = await response.read()
doc = html.fromstring(response)
search_results = doc.xpath(f"//a[contains(text(),'{self.__mod_name}')]")
if not search_results:
return
try:
async with session.get(search_results[0].get("href")) as response:
response = await response.read()
doc = html.fromstring(response)
download_link = doc.xpath("//a[contains(text(),'Download link')]")[0].get('href')
last_updated = doc.xpath("//strong/text()[contains(.,'Last updated ')]")[0].text
except (IndexError, KeyError):
return
magnet_links = self.__regex.findall(download_link)
if magnet_links:
download_link = magnet_links[0]
return {"link": download_link, "last_updated": last_updated, "website": 'freetutorialsudemy.com'}