46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
|
#!/usr/bin/env python3
|
||
|
# -*- encoding: utf-8 -*-
|
||
|
|
||
|
import re
|
||
|
import filetype
|
||
|
from bs4 import BeautifulSoup as _BS
|
||
|
from ..downloadedData import DownloadedData
|
||
|
from ... import simpleDownloader
|
||
|
|
||
|
def BeautifulSoup(data): return _BS(data, 'html5lib')
|
||
|
|
||
|
def matchRedditUploads(link):
|
||
|
return re.match(r'https?://i\.reddituploads\.com/([0-9a-fA-F]+)\??',link)
|
||
|
|
||
|
def works_on(domain):
|
||
|
return domain in ['i.redd.it','i.reddituploads.com']
|
||
|
|
||
|
class IReddIt(object):
|
||
|
def recognizes(self, link):
|
||
|
return link.startswith('/r/') or matchRedditUploads(link) is not None
|
||
|
|
||
|
def download(self, link):
|
||
|
dd = DownloadedData()
|
||
|
simpleDownloader.setCookies({'over18':1})
|
||
|
if link.startswith('/r/'):
|
||
|
redditlink = 'https://www.reddit.com'+link
|
||
|
threadBytes = simpleDownloader.getUrlBytes(redditlink)
|
||
|
if threadBytes is not None:
|
||
|
threadbs = BeautifulSoup(threadBytes)
|
||
|
linkbs = threadbs.find('a', class_='title')
|
||
|
if linkbs is not None:
|
||
|
imgloc = linkbs['href']
|
||
|
data = simpleDownloader.getUrlBytes(imgloc)
|
||
|
if data is not None:
|
||
|
dd.put(imgloc, data, filetype.guess_extension(data))
|
||
|
elif matchRedditUploads(link):
|
||
|
data = simpleDownloader.getUrlBytes(link)
|
||
|
if data is not None:
|
||
|
dd.put(link, data, filetype.guess_extension(data))
|
||
|
else:
|
||
|
return None
|
||
|
return dd
|
||
|
|
||
|
def get_class():
|
||
|
return IReddIt
|