base_downloader.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python3
  2. # coding=utf-8
  3. import logging
  4. from abc import ABC, abstractmethod
  5. from typing import Optional
  6. import requests
  7. from praw.models import Submission
  8. from bdfr.exceptions import ResourceNotFound, SiteDownloaderError
  9. from bdfr.resource import Resource
  10. from bdfr.site_authenticator import SiteAuthenticator
  11. logger = logging.getLogger(__name__)
  12. class BaseDownloader(ABC):
  13. def __init__(self, post: Submission, typical_extension: Optional[str] = None):
  14. self.post = post
  15. self.typical_extension = typical_extension
  16. @abstractmethod
  17. def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
  18. """Return list of all un-downloaded Resources from submission"""
  19. raise NotImplementedError
  20. @staticmethod
  21. def retrieve_url(url: str, cookies: dict = None, headers: dict = None) -> requests.Response:
  22. try:
  23. res = requests.get(url, cookies=cookies, headers=headers)
  24. except requests.exceptions.RequestException as e:
  25. logger.exception(e)
  26. raise SiteDownloaderError(f'Failed to get page {url}')
  27. if res.status_code != 200:
  28. raise ResourceNotFound(f'Server responded with {res.status_code} to {url}')
  29. return res