├── .gitignore ├── LICENSE ├── README.md ├── constants.py ├── download_category.py ├── download_file.py ├── download_highschool.py ├── download_semestr.py ├── download_subject.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | .idea 3 | *.pyc 4 | __pycache__ 5 | try 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright © 2018 Oleg Morozenkov 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # studizba tools 2 | 3 | Скачивает и распаковывает файлы с сайта studizba.com (ранее baumanki.net). 4 | 5 | ## Как установить 6 | 7 | ```sh 8 | git clone https://github.com/reo7sp/baumanki-net-tools 9 | cd baumanki-net-tools 10 | pip3 install -r requirements.txt 11 | ``` 12 | 13 | ## Как пользоваться 14 | 15 | Скачать категорию: 16 | ```sh 17 | ./download_category.py ссылка_на_категорию ~/Desktop/studizba 18 | ``` 19 | 20 | Скачать один файл: 21 | ```sh 22 | ./download_file.py ссылка_на_файл ~/Desktop/studizba 23 | ``` 24 | -------------------------------------------------------------------------------- /constants.py: -------------------------------------------------------------------------------- 1 | ROOT_URL = 'https://studizba.com' -------------------------------------------------------------------------------- /download_category.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import os 3 | import requests 4 | from bs4 import BeautifulSoup 5 | 6 | from constants import ROOT_URL 7 | from download_file import download_file 8 | from tqdm import tqdm 9 | 10 | 11 | def _check_is_valid_file_url(link): 12 | parts = link.split('-') 13 | if len(parts) == 0: 14 | return False 15 | return parts[0].isdigit() 16 | 17 | 18 | def download_category(url, dest): 19 | try: 20 | os.makedirs(dest, exist_ok=True) 21 | 22 | html_str = requests.get(url).text 23 | html = BeautifulSoup(html_str, 'html.parser') 24 | 25 | result_table = html.find(id='subject-table') 26 | if result_table is None: 27 | return 28 | for link_el in tqdm(result_table.find_all('a'), desc=url): 29 | link_str_relative = link_el['href'] 30 | if not _check_is_valid_file_url(link_str_relative): 31 | continue 32 | link_str_absolute = url + '/' + link_str_relative 33 | name = link_el.string 34 | download_file(link_str_absolute, os.path.join(dest, name)) 35 | except: 36 | print('Failed at category', url) 37 | raise 38 | 39 | 40 | if __name__ == '__main__': 41 | try: 42 | assert len(os.sys.argv) >= 3 43 | url = os.sys.argv[1] 44 | dest = os.sys.argv[2] 45 | except AssertionError: 46 | print('USAGE: python3 download_category.py URL DEST') 47 | exit(1) 48 | 49 | download_category(url, dest) 50 | 51 | -------------------------------------------------------------------------------- /download_file.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import os 3 | import re 4 | import shutil 5 | import traceback 6 | import time 7 | import requests 8 | from bs4 import BeautifulSoup 9 | from pyunpack import Archive 10 | 11 | from constants import ROOT_URL 12 | 13 | 14 | def download_file(url, dest): 15 | for i in range(3): 16 | try: 17 | _download_file(url, dest) 18 | return 19 | except: 20 | print('Failing at file {}. Retry {}/3...'.format(url, i+1)) 21 | if i == 0: 22 | traceback.print_exc() 23 | time.sleep(10) 24 | print('Failed at file {}. Skip. Reported to log.txt'.format(url)) 25 | _report_fail(url) 26 | 27 | 28 | def _download_file(url, dest): 29 | try: 30 | os.makedirs(dest) 31 | except: 32 | return # already downloaded 33 | 34 | html_str = requests.get(url).text 35 | html = BeautifulSoup(html_str, 'html.parser') 36 | 37 | link_el = html.find('a', class_='fa-file-download-bt') 38 | link_str_relative = link_el['href'].replace('file-download', 'start-download') 39 | link_str_absolute = ROOT_URL + link_str_relative 40 | 41 | _save_file(link_str_absolute, dest) 42 | 43 | 44 | def _save_file(url, dest): 45 | with requests.get(url, stream=True) as r: 46 | filename = re.findall('filename="(.+?)"', r.headers['content-disposition'])[0] 47 | filepath = os.path.join(dest, filename) 48 | with open(filepath, 'wb') as f: 49 | shutil.copyfileobj(r.raw, f) 50 | 51 | filepath_without_ext = '.'.join(filepath.split('.')[:-1]) 52 | os.makedirs(filepath_without_ext, exist_ok=True) 53 | Archive(filepath).extractall(filepath_without_ext) 54 | 55 | os.remove(filepath) 56 | 57 | 58 | def _report_fail(url): 59 | try: 60 | with open('log.txt', mode='a') as f: 61 | f.write(url) 62 | f.write('\n') 63 | except: 64 | print('Cannot report fail to log.txt') 65 | traceback.print_exc() 66 | 67 | 68 | if __name__ == '__main__': 69 | try: 70 | assert len(os.sys.argv) >= 3 71 | url = os.sys.argv[1] 72 | dest = os.sys.argv[2] 73 | except AssertionError: 74 | print('USAGE: python3 download_file.py URL DEST') 75 | exit(1) 76 | 77 | download_file(url, dest) 78 | 79 | -------------------------------------------------------------------------------- /download_highschool.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import os 3 | import requests 4 | from bs4 import BeautifulSoup 5 | 6 | from constants import ROOT_URL 7 | from download_semestr import download_semestr 8 | from tqdm import tqdm 9 | 10 | 11 | def download_highschool(url, dest): 12 | try: 13 | os.makedirs(dest, exist_ok=True) 14 | 15 | html_str = requests.get(url).text 16 | html = BeautifulSoup(html_str, 'html.parser') 17 | 18 | result_table = html.find(id='subject-table') 19 | if result_table is None: 20 | return 21 | for link_el in tqdm(result_table.find_all('a'), desc=url): 22 | link_str_relative = link_el['href'] 23 | if not 'filearray' in link_str_relative: 24 | continue 25 | link_str_absolute = ROOT_URL + link_str_relative 26 | name = link_el.string 27 | download_semestr(link_str_absolute, os.path.join(dest, name)) 28 | except: 29 | print('Failed at semestr', url) 30 | raise 31 | 32 | 33 | if __name__ == '__main__': 34 | try: 35 | assert len(os.sys.argv) >= 3 36 | url = os.sys.argv[1] 37 | dest = os.sys.argv[2] 38 | except AssertionError: 39 | print('USAGE: python3 download_highschool.py URL DEST') 40 | exit(1) 41 | 42 | download_highschool(url, dest) 43 | 44 | -------------------------------------------------------------------------------- /download_semestr.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import os 3 | import requests 4 | from bs4 import BeautifulSoup 5 | 6 | from constants import ROOT_URL 7 | from download_subject import download_subject 8 | from tqdm import tqdm 9 | 10 | 11 | def download_semestr(url, dest): 12 | try: 13 | os.makedirs(dest, exist_ok=True) 14 | 15 | html_str = requests.get(url).text 16 | html = BeautifulSoup(html_str, 'html.parser') 17 | 18 | result_table = html.find(id='subject-table') 19 | if result_table is None: 20 | return 21 | for link_el in tqdm(result_table.find_all('a'), desc=url): 22 | link_str_relative = link_el['href'] 23 | if not 'filearray' in link_str_relative: 24 | continue 25 | link_str_absolute = ROOT_URL + link_str_relative 26 | name = link_el.string 27 | download_subject(link_str_absolute, os.path.join(dest, name)) 28 | except: 29 | print('Failed at semestr', url) 30 | raise 31 | 32 | 33 | if __name__ == '__main__': 34 | try: 35 | assert len(os.sys.argv) >= 3 36 | url = os.sys.argv[1] 37 | dest = os.sys.argv[2] 38 | except AssertionError: 39 | print('USAGE: python3 download_semestr.py URL DEST') 40 | exit(1) 41 | 42 | download_semestr(url, dest) 43 | 44 | -------------------------------------------------------------------------------- /download_subject.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import os 3 | import requests 4 | from bs4 import BeautifulSoup 5 | 6 | from constants import ROOT_URL 7 | from download_category import download_category 8 | from tqdm import tqdm 9 | 10 | 11 | def download_subject(url, dest): 12 | try: 13 | os.makedirs(dest, exist_ok=True) 14 | 15 | html_str = requests.get(url).text 16 | html = BeautifulSoup(html_str, 'html.parser') 17 | 18 | result_table = html.find(id='subject-table') 19 | if result_table is None: 20 | return 21 | for link_el in tqdm(result_table.find_all('a'), desc=url): 22 | link_str_relative = link_el['href'] 23 | if not 'filearray' in link_str_relative: 24 | continue 25 | link_str_absolute = ROOT_URL + link_str_relative 26 | name = link_el.string 27 | download_category(link_str_absolute, os.path.join(dest, name)) 28 | except: 29 | print('Failed at subject', url) 30 | raise 31 | 32 | 33 | if __name__ == '__main__': 34 | try: 35 | assert len(os.sys.argv) >= 3 36 | url = os.sys.argv[1] 37 | dest = os.sys.argv[2] 38 | except AssertionError: 39 | print('USAGE: python3 download_subject.py URL DEST') 40 | exit(1) 41 | 42 | download_subject(url, dest) 43 | 44 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | beautifulsoup4 2 | requests 3 | pyunpack 4 | patool 5 | tqdm --------------------------------------------------------------------------------