Refactor downloader
This commit is contained in:
parent
a54a1c6dfa
commit
f13e029e47
1 changed files with 8 additions and 17 deletions
|
@ -1,5 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import re
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
@ -17,30 +18,20 @@ class GifDeliveryNetwork(BaseDownloader):
|
||||||
super().__init__(post)
|
super().__init__(post)
|
||||||
|
|
||||||
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
|
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
|
||||||
try:
|
|
||||||
media_url = self._get_link(self.post.url)
|
media_url = self._get_link(self.post.url)
|
||||||
except IndexError:
|
|
||||||
raise NotADownloadableLinkError("Could not read the page source")
|
|
||||||
|
|
||||||
return [Resource(self.post, media_url, '.mp4')]
|
return [Resource(self.post, media_url, '.mp4')]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_link(url: str) -> str:
|
def _get_link(url: str) -> str:
|
||||||
"""Extract direct link to the video from page's source and return it"""
|
if re.match(r'https://.*\.(mp4|webm|gif)(\?.*)?$', url):
|
||||||
if '.webm' in url.split('/')[-1] or '.mp4' in url.split('/')[-1] or '.gif' in url.split('/')[-1]:
|
|
||||||
return url
|
return url
|
||||||
|
|
||||||
if url[-1:] == '/':
|
|
||||||
url = url[:-1]
|
|
||||||
|
|
||||||
url = "https://www.gifdeliverynetwork.com/" + url.split('/')[-1]
|
|
||||||
page_source = requests.get(url).text
|
page_source = requests.get(url).text
|
||||||
|
|
||||||
soup = BeautifulSoup(page_source, "html.parser")
|
soup = BeautifulSoup(page_source, 'html.parser')
|
||||||
attributes = {"id": "mp4Source", "type": "video/mp4"}
|
content = soup.find('source', attrs={'id': 'mp4Source', 'type': 'video/mp4'})
|
||||||
content = soup.find("source", attrs=attributes)
|
|
||||||
|
|
||||||
if content is None:
|
if content is None or content.get('src') is None:
|
||||||
raise NotADownloadableLinkError("Could not read the page source")
|
raise NotADownloadableLinkError('Could not read the page source')
|
||||||
|
|
||||||
return content["src"]
|
return content.get('src')
|
||||||
|
|
Loading…
Reference in a new issue