2020-05-29 06:42:11 +12:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import urllib.request
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
from src.downloaders.downloaderUtils import getFile, getExtension
|
|
|
|
from src.errors import (FileNameTooLong, AlbumNotDownloadedCompletely,
|
|
|
|
NotADownloadableLinkError, FileAlreadyExistsError)
|
2020-06-02 00:05:02 +12:00
|
|
|
from src.utils import GLOBAL
|
2020-05-29 06:42:11 +12:00
|
|
|
from src.utils import printToFile as print
|
|
|
|
|
|
|
|
class Redgifs:
|
|
|
|
def __init__(self,directory,POST):
|
|
|
|
try:
|
2020-06-02 00:05:02 +12:00
|
|
|
POST['MEDIAURL'] = self.getLink(POST['CONTENTURL'])
|
2020-05-29 06:42:11 +12:00
|
|
|
except IndexError:
|
|
|
|
raise NotADownloadableLinkError("Could not read the page source")
|
|
|
|
|
2020-06-02 00:05:02 +12:00
|
|
|
POST['EXTENSION'] = getExtension(POST['MEDIAURL'])
|
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
if not os.path.exists(directory): os.makedirs(directory)
|
|
|
|
|
2020-06-02 00:05:02 +12:00
|
|
|
filename = GLOBAL.config['filename'].format(**POST)+POST["EXTENSION"]
|
|
|
|
shortFilename = POST['POSTID']+POST['EXTENSION']
|
|
|
|
|
|
|
|
getFile(filename,shortFilename,directory,POST['MEDIAURL'])
|
2020-05-29 06:42:11 +12:00
|
|
|
|
|
|
|
def getLink(self, url):
|
|
|
|
"""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:
|
|
|
|
return url
|
|
|
|
|
|
|
|
if url[-1:] == '/':
|
|
|
|
url = url[:-1]
|
|
|
|
|
2021-01-18 10:58:51 +13:00
|
|
|
url = urllib.request.Request("https://redgifs.com/watch/" + url.split('/')[-1])
|
|
|
|
|
|
|
|
url.add_header('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
|
|
|
|
|
|
|
pageSource = (urllib.request.urlopen(url).read().decode())
|
|
|
|
|
|
|
|
soup = BeautifulSoup(pageSource, "html.parser")
|
|
|
|
attributes = {"data-react-helmet":"true","type":"application/ld+json"}
|
|
|
|
content = soup.find("script",attrs=attributes)
|
|
|
|
|
|
|
|
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"]
|