Move to inheritance system for downloaders

This commit is contained in:
Serene-Arc
2021-02-07 14:46:20 +10:00
committed by Ali Parlakci
parent 69e21e46a2
commit f573038a21
11 changed files with 253 additions and 280 deletions

View File

@@ -1,43 +1,32 @@
#!/usr/bin/env python3
import json
import os
import pathlib
import re
import urllib.request
from bs4 import BeautifulSoup
from bulkredditdownloader.downloaders.base_downloader import BaseDownloader
from bulkredditdownloader.downloaders.gif_delivery_network import GifDeliveryNetwork
from bulkredditdownloader.errors import NotADownloadableLinkError
from bulkredditdownloader.utils import GLOBAL
import pathlib
class Gfycat(BaseDownloader):
class Gfycat(GifDeliveryNetwork):
def __init__(self, directory: pathlib.Path, post: dict):
super().__init__(directory, post)
try:
post['MEDIAURL'] = self.getLink(post['CONTENTURL'])
except IndexError:
raise NotADownloadableLinkError("Could not read the page source")
self.download()
post['EXTENSION'] = self.getExtension(post['MEDIAURL'])
if not os.path.exists(directory):
os.makedirs(directory)
filename = GLOBAL.config['filename'].format(**post) + post["EXTENSION"]
short_filename = post['POSTID'] + post['EXTENSION']
self.getFile(filename, short_filename, directory, post['MEDIAURL'])
def download(self):
super().download()
@staticmethod
def getLink(url: str) -> str:
def _get_link(url: str) -> str:
"""Extract direct link to the video from page's source
and return it
"""
if '.webm' in url or '.mp4' in url or '.gif' in url:
if re.match(r'\.(webm|mp4|gif)$', url):
return url
if url[-1:] == '/':
if url.endswith('/'):
url = url[:-1]
url = "https://gfycat.com/" + url.split('/')[-1]
@@ -49,6 +38,6 @@ class Gfycat(BaseDownloader):
content = soup.find("script", attrs=attributes)
if content is None:
return GifDeliveryNetwork.getLink(url)
return super()._get_link(url)
return json.loads(content.contents[0])["video"]["contentUrl"]