1
0
Fork 0
mirror of synced 2024-07-04 05:50:34 +12:00
bulk-downloader-for-reddit/bulkredditdownloader/site_downloaders/gif_delivery_network.py

47 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2021-02-25 23:40:08 +13:00
from typing import Optional
2021-02-26 22:38:29 +13:00
import requests
from bs4 import BeautifulSoup
2021-02-11 12:10:40 +13:00
from praw.models import Submission
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-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-26 21:57:05 +13:00
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
try:
2021-02-11 12:10:40 +13:00
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')]
@staticmethod
def _get_link(url: str) -> str:
2021-02-25 23:40:08 +13:00
"""Extract direct link to the video from page's source and return it"""
if '.webm' in url.split('/')[-1] or '.mp4' in url.split('/')[-1] or '.gif' in url.split('/')[-1]:
return url
if url[-1:] == '/':
url = url[:-1]
url = "https://www.gifdeliverynetwork.com/" + url.split('/')[-1]
2021-02-26 22:38:29 +13:00
page_source = requests.get(url).text
soup = BeautifulSoup(page_source, "html.parser")
attributes = {"id": "mp4Source", "type": "video/mp4"}
content = soup.find("source", attrs=attributes)
if content is None:
raise NotADownloadableLinkError("Could not read the page source")
return content["src"]