-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patho_and_a_lv_qp_sdl.py
executable file
·175 lines (147 loc) · 6.42 KB
/
o_and_a_lv_qp_sdl.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
"""
This script downloads exam papers and mark schemes from the xtremepapers' website
for CAIE and Edexcel boards and organizes them into directories based on the
exam board and subject.
"""
import os
import re
import requests
from bs4 import BeautifulSoup
BASE_URL = 'https://papers.xtremepape.rs/'
def get_exam_board():
"""Prompt user to choose the examination board."""
while True:
print("\nChoose the examination board:")
print("1. Cambridge (CAIE)")
print("2. Edexcel")
choice = input("Enter your choice (1 or 2): ").strip()
if choice == '1':
return 'CAIE'
if choice == '2':
return 'Edexcel'
print("Invalid choice. Please enter 1 or 2.")
def get_exam_level(exam_board):
"""Prompt user to choose the examination level based on the selected board."""
while True:
print("\nChoose the examination level:")
if exam_board == 'CAIE':
print("1. O Level")
print("2. AS and A Level")
else: # Edexcel
print("1. International GCSE")
print("2. Advanced Level")
choice = input("Enter your choice (1 or 2): ").strip()
if choice == '1':
return 'O+Level' if exam_board == 'CAIE' else 'International+GCSE'
if choice == '2':
return 'AS+and+A+Level' if exam_board == 'CAIE' else 'Advanced+Level'
print("Invalid choice. Please enter 1 or 2.")
def get_subjects(exam_board, exam_level):
"""Fetch subjects for the selected exam board and level."""
if exam_board == 'CAIE':
url = f'{BASE_URL}index.php?dirpath=./CAIE/{exam_level}/&order=0'
else: # Edexcel
url = f'{BASE_URL}index.php?dirpath=./Edexcel/{exam_level}/&order=0'
response = requests.get(url, timeout=10)
soup = BeautifulSoup(response.text, 'html.parser')
subject_links = soup.find_all('a', class_='directory')
subjects = {}
for link in subject_links:
subject_name = link.text.strip('[]')
if subject_name != '..': # Skip the parent directory link
subjects[subject_name] = BASE_URL + link['href']
return subjects
def get_pdfs(subject_url, exam_board):
"""Fetch PDF links for the selected subject."""
response = requests.get(subject_url, timeout=10)
soup = BeautifulSoup(response.text, 'html.parser')
if exam_board == 'Edexcel':
return get_edexcel_pdfs(subject_url)
pdf_links = soup.find_all('a', class_='file', href=re.compile(r'\.pdf$'))
return {link.text.strip(): BASE_URL + link['href'] for link in pdf_links}
def get_edexcel_pdfs(subject_url):
"""Fetch PDF links for Edexcel subjects."""
pdfs = {}
response = requests.get(subject_url, timeout=10)
soup = BeautifulSoup(response.text, 'html.parser')
year_links = soup.find_all('a', class_='directory')
for year_link in year_links:
if year_link.text.strip('[]') != '..':
year_url = BASE_URL + year_link['href']
year_response = requests.get(year_url, timeout=10)
year_soup = BeautifulSoup(year_response.text, 'html.parser')
qp_link = year_soup.find('a', class_='directory', text='[Question-paper]')
ms_link = year_soup.find('a', class_='directory', text='[Mark-scheme]')
if qp_link:
qp_url = BASE_URL + qp_link['href']
qp_pdfs = get_pdfs_from_page(qp_url)
pdfs.update(qp_pdfs)
if ms_link:
ms_url = BASE_URL + ms_link['href']
ms_pdfs = get_pdfs_from_page(ms_url)
pdfs.update(ms_pdfs)
return pdfs
def get_pdfs_from_page(url):
"""Fetch all PDF links from a specific page."""
response = requests.get(url, timeout=10)
soup = BeautifulSoup(response.text, 'html.parser')
pdf_links = soup.find_all('a', class_='file', href=re.compile(r'\.pdf$'))
return {link.text.strip(): BASE_URL + link['href'] for link in pdf_links}
def download_pdf(url, filename, subject_dir, exam_board):
"""Download a PDF and save it in the appropriate directory."""
response = requests.get(url, timeout=10)
subdir = categorize_pdf(filename, exam_board)
dir_path = os.path.join(subject_dir, subdir)
os.makedirs(dir_path, exist_ok=True)
file_path = os.path.join(dir_path, filename)
with open(file_path, 'wb') as f:
f.write(response.content)
print(f"Downloaded: {filename}")
def categorize_pdf(filename, exam_board):
"""Categorize the PDF as question paper, mark scheme, or miscellaneous."""
if exam_board == 'CAIE':
if '_ms_' in filename:
return 'ms'
if '_qp_' in filename:
return 'qp'
return 'misc'
# Edexcel
if 'question' in filename.lower():
return 'qp'
if 'mark' in filename.lower() or 'ms' in filename.lower():
return 'ms'
return 'misc'
def print_subjects_in_columns(subjects):
"""Print the available subjects in multiple columns."""
terminal_width = os.get_terminal_size().columns
max_width = max(len(f"{i}. {subject}") for i, subject in enumerate(subjects, 1))
num_columns = max(1, terminal_width // (max_width + 2))
subject_list = [f"{i}. {subject}" for i, subject in enumerate(subjects, 1)]
for i in range(0, len(subject_list), num_columns):
row = subject_list[i:i + num_columns]
print(" ".join(item.ljust(max_width) for item in row))
def main():
"""Main function to run the script."""
exam_board = get_exam_board()
exam_level = get_exam_level(exam_board)
subjects = get_subjects(exam_board, exam_level)
print(f"\nAvailable subjects for {exam_board} {exam_level.replace('+', ' ')}:")
print_subjects_in_columns(subjects)
choices = input("\nEnter the numbers of the subjects you want to download (space-separated): ")
selected_indices = [int(x.strip()) for x in choices.split()]
selected_subjects = list(subjects.keys())
for index in selected_indices:
subject = selected_subjects[index - 1]
subject_url = subjects[subject]
print(f"\nProcessing {subject}...")
pdfs = get_pdfs(subject_url, exam_board)
subject_dir = os.path.join(
exam_board,
exam_level.replace('+', ' '),
subject.replace('/', '_').replace('&', 'and')
)
os.makedirs(subject_dir, exist_ok=True)
for filename, pdf_url in pdfs.items():
download_pdf(pdf_url, filename, subject_dir, exam_board)
if __name__ == "__main__":
main()