2017-12-29 22:54:22 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
2018-01-07 03:57:39 +00:00
|
|
|
import os
|
2017-12-29 22:54:22 +00:00
|
|
|
import re
|
|
|
|
import json
|
2018-01-07 03:57:39 +00:00
|
|
|
import shutil
|
2017-12-29 22:54:22 +00:00
|
|
|
import filetype
|
|
|
|
from ..downloadedData import DownloadedData
|
|
|
|
from ... import simpleDownloader
|
|
|
|
|
|
|
|
def works_on(domain):
|
|
|
|
return domain in ['i.imgur.com', 'imgur.com', 'm.imgur.com', 'www.imgur.com']
|
|
|
|
|
|
|
|
class ImgurCom(object):
|
|
|
|
def recognizes(self, link):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def download(self, link):
|
|
|
|
dd = DownloadedData()
|
|
|
|
simpleDownloader.cleanCookies()
|
|
|
|
bts = b''
|
|
|
|
if '/a/' not in link and '.gifv' not in link and '.webm' not in link:
|
|
|
|
bts = simpleDownloader.getUrlBytes(link)
|
|
|
|
if bts is not None:
|
|
|
|
ext = filetype.guess_extension(bts)
|
|
|
|
if ext is None:
|
|
|
|
if '.gifv' in link or '.webm' in link:
|
|
|
|
bts=None
|
|
|
|
print(' '*50,end='')
|
|
|
|
print('\r',end='')
|
|
|
|
print(' `--> It wasn\'t a single image...',end='')
|
|
|
|
print('\r',end='')
|
|
|
|
match = re.match(
|
|
|
|
"(https?)://(www\.)?(i\.|m\.|www\.)?imgur\.com/(?:(a|gallery|r)/)?(\w*)/?(\w*)(#[0-9]+)?(\.\w*)?",
|
|
|
|
link
|
|
|
|
)
|
|
|
|
tp = match.group(4)
|
|
|
|
ky = None
|
|
|
|
if tp != 'r':
|
|
|
|
ky = match.group(5)
|
|
|
|
else:
|
|
|
|
ky = match.group(6)
|
|
|
|
if not ky:
|
|
|
|
ky = match.group(5)
|
|
|
|
link2 = 'https://imgur.com/a/'+str(ky)+'/all'
|
|
|
|
if tp is None or tp=='' or tp=='r':
|
|
|
|
link2=link2.replace('/a/','/')[:-4]
|
|
|
|
print(' '*50,end='')
|
|
|
|
print('\r',end='')
|
|
|
|
if link2.endswith('/all') or bts is None:
|
|
|
|
print(' `--> Fetching album image list...',end='')
|
|
|
|
bts = simpleDownloader.getUrlBytes(link2)
|
|
|
|
else:
|
|
|
|
print(' `--> Album image list already fetched...',end='')
|
|
|
|
print('\r',end='')
|
|
|
|
if bts is None:
|
|
|
|
print(' '*50,end='')
|
|
|
|
print('\r',end='')
|
|
|
|
print(' `--> Gallery not found')
|
|
|
|
return DownloadedData()
|
|
|
|
html = bts.decode('utf-8')
|
|
|
|
albnfo = json.loads(list(filter(lambda f: f.startswith('image'), map(str.strip, list(filter(lambda f: f.startswith("('gallery', {"), html.split('widgetFactory.mergeConfig')))[0].strip().splitlines())))[0][6:-1].strip()[1:].strip())
|
|
|
|
imgs = [albnfo]
|
|
|
|
if 'album_images' in albnfo:
|
|
|
|
imgs = albnfo['album_images']['images']
|
2018-01-07 03:57:39 +00:00
|
|
|
intermediarySaves = len(imgs)>=10
|
|
|
|
if intermediarySaves:
|
|
|
|
if not os.path.isdir('tmp'):
|
|
|
|
os.makedirs('tmp')
|
|
|
|
if os.path.isfile('tmp/link.url'):
|
|
|
|
with open('tmp/link.url') as f:
|
|
|
|
svdlnk = f.read()
|
|
|
|
if svdlnk == link:
|
|
|
|
dd.loadfrom('tmp')
|
|
|
|
else:
|
|
|
|
shutil.rmtree('tmp')
|
|
|
|
os.makedirs('tmp')
|
|
|
|
with open('tmp/link.url', 'w') as f:
|
|
|
|
f.write(link)
|
2017-12-29 22:54:22 +00:00
|
|
|
for seq, img in enumerate(imgs):
|
|
|
|
print(' '*50,end='')
|
|
|
|
print('\r',end='')
|
|
|
|
print(' `--> Album image #%03d of %03d'%(seq+1,len(imgs)),end='')
|
|
|
|
print('\r',end='')
|
|
|
|
if img['ext'] == '.gifv':
|
|
|
|
img['ext'] = '.mp4'
|
|
|
|
durl = 'http://i.imgur.com/'+img['hash']+img['ext']
|
2018-01-07 03:57:39 +00:00
|
|
|
if durl in dd.storedLinks():
|
|
|
|
continue
|
2017-12-29 22:54:22 +00:00
|
|
|
imb = simpleDownloader.getUrlBytes(durl)
|
|
|
|
if imb is None:
|
|
|
|
print()
|
|
|
|
print('Album part failed')
|
|
|
|
print()
|
|
|
|
simpleDownloader.cleanCookies()
|
|
|
|
return None
|
|
|
|
dd.put(durl, imb, img['ext'][1:])
|
2018-01-07 03:57:39 +00:00
|
|
|
if intermediarySaves and seq%10 == 0:
|
|
|
|
dd.into('tmp')
|
2017-12-29 22:54:22 +00:00
|
|
|
print('\r',end='')
|
2018-01-07 03:57:39 +00:00
|
|
|
if os.path.isdir('tmp'):
|
|
|
|
shutil.rmtree('tmp')
|
2017-12-29 22:54:22 +00:00
|
|
|
else:
|
|
|
|
dd.put(link, bts, ext)
|
|
|
|
simpleDownloader.cleanCookies()
|
|
|
|
return dd
|
|
|
|
|
|
|
|
def get_class():
|
|
|
|
return ImgurCom
|