2021-02-07 17:46:20 +13:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-03-18 00:04:37 +13:00
|
|
|
import re
|
2021-02-25 23:40:08 +13:00
|
|
|
from typing import Optional
|
2021-02-06 21:35:50 +13:00
|
|
|
|
2021-02-26 22:38:29 +13:00
|
|
|
import requests
|
2020-05-29 06:42:11 +12:00
|
|
|
from bs4 import BeautifulSoup
|
2021-02-11 12:10:40 +13:00
|
|
|
from praw.models import Submission
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-03-05 16:32:24 +13:00
|
|
|
from bulkredditdownloader.exceptions import NotADownloadableLinkError
|
2021-02-25 23:40:08 +13:00
|
|
|
from bulkredditdownloader.resource import Resource
|
2021-02-26 22:38:29 +13:00
|
|
|
from bulkredditdownloader.site_authenticator import SiteAuthenticator
|
2021-02-11 12:10:40 +13:00
|
|
|
from bulkredditdownloader.site_downloaders.base_downloader import BaseDownloader
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-07 14:33:19 +13:00
|
|
|
class GifDeliveryNetwork(BaseDownloader):
|
2021-02-15 18:12:27 +13:00
|
|
|
def __init__(self, post: Submission):
|
|
|
|
super().__init__(post)
|
2021-02-07 17:46:20 +13:00
|
|
|
|
2021-02-26 21:57:05 +13:00
|
|
|
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
|
2021-03-18 00:04:37 +13:00
|
|
|
media_url = self._get_link(self.post.url)
|
2021-02-26 22:45:03 +13:00
|
|
|
return [Resource(self.post, media_url, '.mp4')]
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
@staticmethod
|
2021-02-07 17:46:20 +13:00
|
|
|
def _get_link(url: str) -> str:
|
2021-03-18 00:04:37 +13:00
|
|
|
if re.match(r'https://.*\.(mp4|webm|gif)(\?.*)?$', url):
|
2020-05-29 06:42:11 +12:00
|
|
|
return url
|
|
|
|
|
2021-02-26 22:38:29 +13:00
|
|
|
page_source = requests.get(url).text
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-03-18 00:04:37 +13:00
|
|
|
soup = BeautifulSoup(page_source, 'html.parser')
|
|
|
|
content = soup.find('source', attrs={'id': 'mp4Source', 'type': 'video/mp4'})
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-03-18 00:04:37 +13:00
|
|
|
if content is None or content.get('src') is None:
|
|
|
|
raise NotADownloadableLinkError('Could not read the page source')
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-03-18 00:04:37 +13:00
|
|
|
return content.get('src')
|