2019-10-15 15:34:59 +02:00
|
|
|
# TGI Lecture slides
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import pathlib
|
|
|
|
import re
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
import bs4
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from .organizer import Organizer
|
|
|
|
from .utils import rename, stream_to_path, PrettyLogger
|
|
|
|
|
|
|
|
__all__ = ["TGI"]
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
pretty = PrettyLogger(logger)
|
|
|
|
|
|
|
|
class TGI:
|
2019-10-15 15:37:52 +02:00
|
|
|
CRAWL_URL = "https://i11www.iti.kit.edu/teaching/{year}/tgi/index"
|
2019-10-15 15:34:59 +02:00
|
|
|
BASE_URL = "https://i11www.iti.kit.edu"
|
|
|
|
|
2019-10-15 15:37:52 +02:00
|
|
|
def __init__(self, base_path, year="winter2019"):
|
2019-10-15 15:34:59 +02:00
|
|
|
self.base_path = base_path
|
|
|
|
|
|
|
|
self._session = requests.Session()
|
2019-10-15 15:37:52 +02:00
|
|
|
self.year = year
|
2019-10-15 15:34:59 +02:00
|
|
|
|
|
|
|
def synchronize(self, to_dir, transform=lambda x: x):
|
|
|
|
pretty.starting_synchronizer(to_dir, "TGI")
|
|
|
|
|
|
|
|
sync_path = pathlib.Path(self.base_path, to_dir)
|
|
|
|
orga = Organizer(self.base_path, sync_path)
|
|
|
|
|
|
|
|
orga.clean_temp_dir()
|
|
|
|
|
|
|
|
files = self._crawl()
|
|
|
|
self._download(orga, files, transform)
|
|
|
|
|
|
|
|
orga.clean_sync_dir()
|
|
|
|
orga.clean_temp_dir()
|
|
|
|
|
|
|
|
def _crawl(self):
|
2019-10-15 15:37:52 +02:00
|
|
|
url = self.CRAWL_URL.replace("{year}", self.year)
|
2019-10-15 15:34:59 +02:00
|
|
|
r = self._session.get(url)
|
|
|
|
|
|
|
|
text = r.text
|
|
|
|
soup = bs4.BeautifulSoup(text, "html.parser")
|
|
|
|
|
|
|
|
files = []
|
|
|
|
|
2019-10-17 22:14:32 +02:00
|
|
|
for found in soup.select("a.mediafile.mf_pdf"):
|
2019-10-15 15:34:59 +02:00
|
|
|
url = found["href"]
|
|
|
|
full_url = self.BASE_URL + url
|
|
|
|
|
2019-10-17 22:14:32 +02:00
|
|
|
filename = re.search(r"\d+(/tgi)?/(.+.pdf)", url).group(2)
|
2019-10-15 15:34:59 +02:00
|
|
|
path = pathlib.PurePath(filename)
|
|
|
|
|
|
|
|
logger.debug(f"Found file {filename} at {full_url}")
|
|
|
|
files.append((path, full_url))
|
|
|
|
|
|
|
|
return files
|
|
|
|
|
|
|
|
def _download(self, orga, files, transform):
|
|
|
|
for path, url in sorted(files):
|
|
|
|
logger.debug(f"Downloading {path}")
|
|
|
|
|
|
|
|
new_path = transform(path)
|
|
|
|
if new_path is not None:
|
|
|
|
temp_file = orga.temp_file()
|
|
|
|
self._download_file(url, temp_file)
|
|
|
|
orga.add_file(temp_file, new_path)
|
|
|
|
|
|
|
|
def _download_file(self, url, to_path):
|
|
|
|
with self._session.get(url, stream=True) as r:
|
|
|
|
stream_to_path(r, to_path)
|