This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathripper.py
362 lines (267 loc) · 11.6 KB
/
ripper.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import csv
import requests
import os
import re
from time import time as timer
import threading
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor, as_completed
# ░█▀▀░█▀█░█▀█░█▀▀░▀█▀░█▀▀
# ░█░░░█░█░█░█░█▀▀░░█░░█░█
# ░▀▀▀░▀▀▀░▀░▀░▀░░░▀▀▀░▀▀▀
# configuration options
# see valid config in the README/ bottom of this file
LIBRARY_URL = 'http://10.40.4.17:8080/DLClient6'
USERNAME = 'cse1'
PASSWORD = 'cse1'
SEMESTER = 'EIGHT SEMESTER'
BRANCH = 'COMPUTER SCIENCE'
# 3 letter course short code eg.'TFC' for 16CS6DCTFC.pdf
# Leave empty to rip papers for every course
COURSE = ''
LAST_N_YEARS = 10
# Enable to include the entire course name in the files and folder
LONG_FILE_NAMES = True
# set webdriver to browser you intend to run this on
driver = webdriver.Chrome()
# driver = webdriver.Firefox()
# ░█▀▄░▀█▀░█▀█░█▀█░█▀▀░█▀▄
# ░█▀▄░░█░░█▀▀░█▀▀░█▀▀░█▀▄
# ░▀░▀░▀▀▀░▀░░░▀░░░▀▀▀░▀░▀
def setLinksTargetToSelf():
links = WebDriverWait(driver, 20).until(
EC.presence_of_all_elements_located((By.TAG_NAME, "a"))
)
for link in links:
driver.execute_script("arguments[0].target='_self';", link)
return
def addPdfLinksToList(exam_name, links_list, specific_course):
# function returns a dict where key(pdf file name) corresponds to value(pdf url and folder name)
table_rows = driver.find_elements_by_xpath('//*[@id="tab1"]/tbody/tr')
for table_row in table_rows:
table_columns = table_row.find_elements_by_tag_name('td')
course_name = table_columns[2].text.strip().replace(
' ', '_').replace('.', '')
course_code = table_columns[4].text.strip().replace(
' ', '').replace('_', '')[:-4]
if LONG_FILE_NAMES is True:
folder_name = '_'.join([course_code, course_name])
else:
folder_name = course_name
pdf_filename = '_'.join([exam_name, course_name, course_code])
pdf_link_url = table_columns[4].find_element_by_tag_name(
'a').get_attribute('href')
if (len(specific_course) > 0 and specific_course in course_code) or len(specific_course) == 0:
links_list[pdf_filename] = [pdf_link_url, folder_name]
return links_list
def screamErrorAndQuit(msg):
print(f'ERROR: {msg}')
driver.quit()
exit(1)
def ripper():
links_list = {}
driver.get(LIBRARY_URL)
username_field = driver.find_element_by_name('loginForm.userName')
username_field.send_keys(USERNAME)
password_field = driver.find_element_by_name('loginForm.userPassword')
password_field.send_keys(PASSWORD)
driver.find_element_by_tag_name('button').click()
# check if login was succesful
try:
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.CLASS_NAME, "logout"))
)
except:
screamErrorAndQuit('login details are incorrect!')
driver.get(LIBRARY_URL +
'/browse/browseSubCategory?link=YnJvd3Nl&catCode=27283950369')
try:
semester_category = driver.find_element_by_partial_link_text(SEMESTER)
print(f"Opening {semester_category.text}")
semester_category.click()
try:
branch_category = driver.find_element_by_partial_link_text(BRANCH)
print(f"Opening {branch_category.text}")
branch_category.click()
# find batch divs eg. '2019 and 2020', '2018 and 2019', etc
batches = driver.find_elements_by_class_name('cat-row-col')
batches = reversed(batches)
# save last N years batches links
batch_links = []
for batch, _ in zip(batches, range(LAST_N_YEARS)):
batch_btn = batch.find_element_by_class_name('btn')
batch_links.append(batch_btn.get_attribute('href'))
# iterate through last N years batches links
for batch_link in batch_links:
driver.get(batch_link)
# extract batch name from the breadcrumb
breadcrumb = driver.find_element_by_css_selector('li.active')
batch_name = breadcrumb.text.replace(' ', '_').strip()
# prevent links from opening in new tabs
setLinksTargetToSelf()
# exams -> main, supplementary, makeup, etc
exams = driver.find_elements_by_class_name('cat-row-col')
# save different exam type links
exam_links = []
for exam in exams:
exam_type = exam.find_element_by_xpath(
'./div/div/div[2]/div/h2/a')
exam_link_url = exam_type.get_attribute('href')
# minimize batch name '2018 AND 2019' to '2018-19'
batch_name = (batch_name[:4] +
'-' + batch_name[-2:]).strip()
# minimize exam name 'SEMESTER_END_MAIN_EXAMINATION' to 'MAIN'
exam_type_name = exam_type.text.replace(
'SEMESTER END', '').replace('EXAMINATIONS', '').strip()
exam_link_name = (batch_name + '_' +
exam_type_name).replace(' ', '_')
exam_links.append([exam_link_name, exam_link_url])
# iterate through exam type links
for exam_link in exam_links:
print(f'·· Adding {exam_link[0]} Papers')
driver.get(exam_link[1])
try:
pagination_exists = driver.find_element_by_class_name(
'pagination')
pagination_exists = True
except:
pagination_exists = False
cur_page = 1
# add links from the first page
print(f' ·· pages found: {cur_page}', end="")
addPdfLinksToList(exam_link[0], links_list, COURSE)
# if pagination exists, iterate, add links from the other pages
if pagination_exists:
while(True):
cur_page += 1
try:
next_page = driver.find_element_by_link_text(
str(cur_page))
next_page.click()
print(",", cur_page, end=" ")
addPdfLinksToList(
exam_link[0], links_list, COURSE)
except:
break
print('')
# no need to go back a page, urls to the exam types are saved
# above, we directly go to them
# go back to the list of batches
driver.back()
except:
screamErrorAndQuit(f'Could not find branch matching \"{BRANCH}\"!')
except:
screamErrorAndQuit(f'Could not find semester matching \"{SEMESTER}\"!')
# return cookies to prevent session expiration errors
# here, only the jsession id is required
cookies = driver.get_cookies()
jsession_id = cookies[0]['value']
driver.quit()
return links_list, jsession_id
def extractPublicLink(file_name, file_url, folder_name, cookies):
# Function doesn't need all the passed variables but it makes tracking
# the finished threads easier
r = requests.get(file_url, cookies=cookies)
# obtain public link from response
public_links = re.findall(r'http.+\.pdf', r.text)
public_file_url = public_links[0]
return {file_name: [public_file_url, folder_name]}
def convertToPublicLinks(data, session_id):
s_c = timer()
cookies = {'JSESSIONID': session_id}
print("Using Extracted Cookie ", session_id[:-6]+'XXXXXX')
print("Extracting PUBLIC LINKS")
public_data = {}
with ThreadPoolExecutor(max_workers=None) as executor:
futures = []
for e in data:
# e -> file name, data[e][0] -> file url, data[e][1] -> folder name to store it
futures.append(executor.submit(
extractPublicLink, e, data[e][0], data[e][1], cookies))
for future in as_completed(futures):
public_data.update(future.result())
print(f" ·· took {(timer() - s_c):4.3f}s")
return public_data
def exportToCSV(data):
print("Exporting CSV")
with open('pdf_links.csv', 'w') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',')
for e in data:
# e -> file name, data[e][0] -> file url, data[e][1] -> folder name to store it
csv_writer.writerow([e, data[e][0]])
return
def downloadFile(file_name, file_url, folder_name, download_dir):
download_path = os.path.join(download_dir, folder_name)
# make the subject folder if it doesnt exist already
os.makedirs(download_path, exist_ok=True)
filename = os.path.join(download_path, file_name)
r = requests.get(file_url)
with open(filename+'.pdf', 'wb') as f:
f.write(r.content)
return
def downloadPdfFiles(data):
s_d = timer()
print("Downloading PDFs")
dir = os.path.dirname(__file__)
download_dir = os.path.join(dir, 'RIPPED_PAPERS')
os.makedirs(download_dir, exist_ok=True)
with ThreadPoolExecutor(max_workers=None) as executor:
futures = []
# e -> file name, data[e][0] -> file url, data[e][1] -> folder name to store it
for e in data:
futures.append(executor.submit(
downloadFile, e, data[e][0], data[e][1], download_dir))
print(f" ·· took {(timer() - s_d):4.3f}s")
return
def main():
s_m = timer()
print("Starting Ripper")
data, session_id = ripper()
# Turns out the file link obtained from ripper() redirects to
# another page with the public file link (which does not require
# an authenticated session)
# This function extracts the public urls before saving to CSV
public_link_data = convertToPublicLinks(data, session_id)
exportToCSV(public_link_data)
downloadPdfFiles(public_link_data)
print(f'💀 Ripped {len(public_link_data)} files. \
\nTook {(timer() - s_m):4.3f}s.')
if __name__ == "__main__":
main()
# ░█▀▀░█░█░▀█▀░█▀▄░█▀█
# ░█▀▀░▄▀▄░░█░░█▀▄░█▀█
# ░▀▀▀░▀░▀░░▀░░▀░▀░▀░▀
# Valid branch parameters:
# ARCHITECTURE
# BIOTECHNOLOGY
# CHEMICAL ENGINEERING
# CIVIL ENGINEERING
# COMPUTER SCIENCE
# INFORMATION SCIENCE
# ELECTRICAL AND ELECTRONIC ENGINEERING
# ELECTRONICS AND COMMUNICATION ENGINEERING
# INDUSTRIAL ENGINEERING AND MANAGEMENT
# INSTRUMENTATION ENGINEERING
# MECHANICAL ENGINEERING
# MEDICAL ELECTRONICS
# TELECOMMUNICATION ENGINEERING
# Valid semester parameters:
# FIRST SEMESTER
# SECOND SEMESTER
# THIRD SEMESTER
# FOURTH SEMESTER
# FIFTH SEMESTER
# SIXTH SEMESTER
# SEVENTH SEMESTER
# EIGHT SEMESTER
# UNSUPPORTED semester parameters:
# MATHEMATICS QUESTION PAPERS
# MBA QUESTION PAPERS
# MCA AUTONOMOUS QUESTION PAPERS
# MCA VTU Question Papers
# M.Tech QUESTION PAPERS