1
0
Fork 0
mirror of synced 2024-10-01 01:30:52 +13:00
bulk-downloader-for-reddit/bdfr/site_downloaders/gif_delivery_network.py

37 lines
1.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2021-02-25 23:40:08 +13:00
from typing import Optional
import json
from bs4 import BeautifulSoup
2021-02-11 12:10:40 +13:00
from praw.models import Submission
2021-04-12 19:58:32 +12:00
from bdfr.exceptions import NotADownloadableLinkError, SiteDownloaderError
from bdfr.resource import Resource
from bdfr.site_authenticator import SiteAuthenticator
from bdfr.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]:
2021-03-18 00:04:37 +13:00
media_url = self._get_link(self.post.url)
return [Resource(self.post, media_url, '.mp4')]
@staticmethod
def _get_link(url: str) -> str:
2021-04-06 12:48:21 +12:00
page = GifDeliveryNetwork.retrieve_url(url)
2021-04-05 19:21:04 +12:00
soup = BeautifulSoup(page.text, 'html.parser')
content = soup.find('script', attrs={'data-react-helmet': 'true', 'type': 'application/ld+json'})
try:
content = json.loads(content.string)
out = content['video']['contentUrl']
except (json.JSONDecodeError, KeyError, TypeError):
raise SiteDownloaderError('Could not find source link')
return out