-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
146 lines (129 loc) · 4.29 KB
/
main.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
import os
import requests
import bs4
import sys
arguments = sys.argv
# -- GLOBAL VARIABLES --
optionList = [
'Allemand',
'Espagnol',
'Russe',
'chinois',
'Turque',
'Italien',
'Ã\x89ducation Musicale',
'Arts & Plastiques',
'Théâtre'
]
sections_g = [
'math',
'science',
'economie',
'technique',
'lettres',
'sport',
'info',
]
bacDir = os.path.join(os.getcwd(),'bac')
def main():
sectionNum = menu()
subjectList = getSubjectList()
if sectionNum == 8:
for i in range(7):
getSection(subjectList, i+1)
else:
getSection(subjectList, sectionNum)
def menu():
print('Choose section(s) to download:')
print('[1] Math')
print('[2] Science')
print('[3] Economie')
print('[4] Technique')
print('[5] Lettres')
print('[6] Sport')
print('[7] Info')
print('[8] ALL')
while True:
ans = input('--> ')
if ans in ['1', '2', '3', '4', '5', '6', '7', '8']:
return int(ans)
else:
print('You must pick a number from the menu!')
continue
def getSubjectList():
mainPageSource = requests.get('http://www.bacweb.tn/section.htm')
soup = bs4.BeautifulSoup(mainPageSource.text, 'lxml')
return soup.find_all('tbody')[0].find_all('tr')
def getProjectDir(section):
#create bac folder if it dosent exist and chdir into it
projectDir = os.path.join(bacDir, f'bac-{section}')
if os.path.exists(projectDir) == False :
os.makedirs(projectDir)
os.chdir(projectDir)
return projectDir
def getSection(subjectList, sectionNum):
sectionName = sections_g[sectionNum-1]
global projectDir
projectDir = getProjectDir(sectionName)
print(f'\n~~~Downloading "{sectionName}" section:~~~')
for subject in subjectList:
sectionList = subject.find_all('td')
try:
subjectName = sectionList[0].text
except:
pass
else:
sectionSubject = sectionList[sectionNum].select('a')
if len(sectionSubject) != 0:
linkToSubject = 'http://www.bacweb.tn/'+sectionSubject[0]['href']
if subjectName in optionList:
# print('OPTION : '+subjectName)
pass
else:
getSubject(linkToSubject, subjectName)
def getSubject(linkToSubject, subjectName):
print(f'Downloading all of "{subjectName}" exams of current section.')
subjectPageSource = requests.get(linkToSubject)
soup = bs4.BeautifulSoup(subjectPageSource.text, 'lxml')
yearsList = soup.find_all('tr')
for year in yearsList:
subjectsByYear = year.find_all('td')
try:
yearNumber = int(subjectsByYear[0].text)
except:
pass
else:
getYear(yearNumber, subjectsByYear)
def getYear(yearNumber, subjectsByYear):
yearNumberDir = os.path.join(projectDir, str(yearNumber))
if os.path.exists(yearNumberDir) == False :
os.makedirs(yearNumberDir)
os.chdir(yearNumberDir)
sessionDir_P = os.path.join(yearNumberDir, 'principale')
if os.path.exists(sessionDir_P) == False :
os.makedirs(sessionDir_P)
sessionDir_C = os.path.join(yearNumberDir, 'controle')
if os.path.exists(sessionDir_C) == False :
os.makedirs(sessionDir_C)
principale_sujet = subjectsByYear[1].find_all('a')
getSujet(principale_sujet, yearNumberDir, 'principale')
principale_corrige = subjectsByYear[2].find_all('a')
getSujet(principale_corrige, yearNumberDir, 'principale')
controle_sujet = subjectsByYear[3].find_all('a')
getSujet(controle_sujet, yearNumberDir, 'controle')
controle_corrige = subjectsByYear[4].find_all('a')
getSujet(controle_corrige, yearNumberDir, 'controle')
os.chdir(projectDir)
def getSujet(sujet, yearNumberDir, promotion):
if len(sujet) != 0:
sujetLink = 'http://www.bacweb.tn/'+sujet[0]['href']
p = sujetLink.rindex('/')
sujetName = sujetLink[p+1:]
promotionDir = os.path.join(yearNumberDir, promotion)
os.chdir(promotionDir)
sujetDir = os.path.join(promotionDir, sujetName)
if os.path.exists(sujetDir) == False:
os.system(f'wget "{sujetLink}" &> /dev/null')
os.chdir(projectDir)
if __name__ == '__main__':
main()