pferd/PFERD/ffm.py

61 lines
1.7 KiB
Python
Raw Normal View History

# Fakultät für Mathematik (FfM)
import logging
import pathlib
import re
import bs4
import requests
from .organizer import Organizer
from .utils import stream_to_path
__all__ = ["FfM"]
logger = logging.getLogger(__name__)
class FfM:
2019-04-24 14:34:20 +02:00
BASE_URL = "http://www.math.kit.edu/"
LINK_RE = re.compile(r"^https?://www.math.kit.edu/.*/(.*\.pdf)$")
def __init__(self, base_path):
self.base_path = base_path
self._session = requests.Session()
2019-04-24 14:34:20 +02:00
def synchronize(self, urlpart, to_dir, transform=lambda x: x):
2019-04-24 14:34:20 +02:00
logging.info(f" Synchronizing {urlpart} to {to_dir} using the FfM synchronizer.")
sync_path = pathlib.Path(self.base_path, to_dir)
orga = Organizer(self.base_path, sync_path)
2019-04-24 14:34:20 +02:00
orga.clean_temp_dir()
self._crawl(orga, urlpart, transform)
2019-04-24 14:34:20 +02:00
orga.clean_sync_dir()
orga.clean_temp_dir()
def _crawl(self, orga, urlpart, transform):
2019-04-24 14:34:20 +02:00
url = self.BASE_URL + urlpart
r = self._session.get(url)
soup = bs4.BeautifulSoup(r.text, "html.parser")
2019-04-24 14:34:20 +02:00
for found in soup.find_all("a", href=self.LINK_RE):
url = found["href"]
filename = re.match(self.LINK_RE, url).group(1).replace("/", ".")
logger.debug(f"Found file {filename} at {url}")
old_path = pathlib.PurePath(filename)
new_path = transform(old_path)
if new_path is None:
continue
logger.debug(f"Transformed from {old_path} to {new_path}")
temp_path = orga.temp_file()
self._download(url, temp_path)
2019-04-24 14:34:20 +02:00
orga.add_file(temp_path, new_path)
def _download(self, url, to_path):
with self._session.get(url, stream=True) as r:
stream_to_path(r, to_path)