2021-02-07 17:46:20 +13:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
import json
|
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.gif_delivery_network import GifDeliveryNetwork
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-07 17:46:20 +13:00
|
|
|
class Redgifs(GifDeliveryNetwork):
|
2021-02-15 18:12:27 +13:00
|
|
|
def __init__(self, post: Submission):
|
|
|
|
super().__init__(post)
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-26 21:57:05 +13:00
|
|
|
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
|
2021-02-25 23:40:08 +13:00
|
|
|
return super().find_resources(authenticator)
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-25 22:32:06 +13:00
|
|
|
@staticmethod
|
2021-02-07 17:46:20 +13:00
|
|
|
def _get_link(url: str) -> str:
|
2021-02-11 12:10:40 +13:00
|
|
|
"""Extract direct link to the video from page's source and return it"""
|
2020-05-29 06:42:11 +12:00
|
|
|
if '.webm' in url or '.mp4' in url or '.gif' in url:
|
|
|
|
return url
|
|
|
|
|
|
|
|
if url[-1:] == '/':
|
|
|
|
url = url[:-1]
|
|
|
|
|
2021-02-26 22:38:29 +13:00
|
|
|
url = "https://redgifs.com/watch/" + url.split('/')[-1]
|
2021-01-18 10:58:51 +13:00
|
|
|
|
2021-02-27 11:26:42 +13:00
|
|
|
headers = {'User-Agent':
|
|
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
|
|
|
|
' Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.64'
|
|
|
|
}
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-26 22:38:29 +13:00
|
|
|
page_source = requests.get(url, headers=headers).text
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
soup = BeautifulSoup(page_source, "html.parser")
|
|
|
|
attributes = {"data-react-helmet": "true", "type": "application/ld+json"}
|
2021-02-25 22:32:06 +13:00
|
|
|
content = soup.find("script", attrs=attributes)
|
2020-05-29 06:42:11 +12:00
|
|
|
|
|
|
|
if content is None:
|
|
|
|
raise NotADownloadableLinkError("Could not read the page source")
|
|
|
|
|
2021-01-18 10:58:51 +13:00
|
|
|
return json.loads(content.contents[0])["video"]["contentUrl"]
|