137 lines
4.0 KiB
Python
Executable File
137 lines
4.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
import os
|
|
import json
|
|
import shutil
|
|
import filetype
|
|
import PIL.Image
|
|
from .system import subredditTools
|
|
|
|
def readAllFile(d):
|
|
with open(d) as f:
|
|
return f.read()
|
|
|
|
proportion = (
|
|
5/4,
|
|
21/9
|
|
)
|
|
minPixels = 1000**2
|
|
|
|
wdir = os.path.abspath('.')
|
|
|
|
idir = os.path.join(wdir, 'i')
|
|
pdir = os.path.join(wdir, 'w')
|
|
rdir = os.path.join(wdir, 'r')
|
|
|
|
def main():
|
|
subreddits = (
|
|
map(
|
|
lambda sr: json.loads(readAllFile(os.path.join(rdir,sr,'subreddit.json'))),
|
|
sorted(
|
|
filter(
|
|
lambda sr:
|
|
os.path.isdir(os.path.join(rdir,sr))
|
|
and
|
|
os.path.isfile(os.path.join(rdir,sr,'subreddit.json'))
|
|
and
|
|
os.path.isfile(os.path.join(rdir,sr,'wallpaper.flag')),
|
|
os.listdir(rdir)
|
|
)
|
|
)
|
|
))
|
|
pass
|
|
|
|
copyfiles = list()
|
|
|
|
linksDown = 0
|
|
linksNotDown = 0
|
|
linksErr = 0
|
|
|
|
print('Listing files...')
|
|
|
|
for subreddit in subreddits:
|
|
sdir = pdir
|
|
for link in subreddit['links']:
|
|
imgd = os.path.join(idir, link['datakey'])
|
|
meta = os.path.join(imgd, 'meta.json')
|
|
if not os.path.exists(meta):
|
|
linksNotDown+=1
|
|
else:
|
|
files = json.loads(readAllFile(meta))
|
|
if len(files)<=0:
|
|
linksErr+=1
|
|
else:
|
|
linksDown+=1
|
|
for seq, file in enumerate(files):
|
|
imgfrom = os.path.join(imgd, file['dname'])
|
|
ext = filetype.guess_extension(imgfrom)
|
|
if ext is None: ext = file['ext']
|
|
imgfn = subredditTools.assembleFileName(
|
|
subreddit['subreddit'],
|
|
link,
|
|
seq,
|
|
ext
|
|
)
|
|
nsfwsafe = 'nsfw' if link['nsfw'] else 'safe'
|
|
imgto = os.path.join(sdir,nsfwsafe,imgfn)
|
|
copyfiles.append((imgfrom,imgto))
|
|
|
|
print('Creating folders...')
|
|
|
|
lcf = len(copyfiles)
|
|
for (cnt, (src, dst)) in enumerate(copyfiles):
|
|
container = os.path.dirname(os.path.abspath(dst))
|
|
if not os.path.exists(container):
|
|
os.makedirs(container)
|
|
|
|
print('Ensuring minimum resolution and proportion...')
|
|
ignored=0
|
|
kept=0
|
|
|
|
lcf = len(copyfiles)
|
|
print('\r'+' '*79+'\r'+'%03d%% processed: %05d of %05d'%(0, 0, lcf),end='')
|
|
for (cnt, (src, dst)) in reversed(list(enumerate(copyfiles))):
|
|
if os.path.exists(dst): continue
|
|
print('\r'+' '*79+'\r'+'%03d%% processed: %05d of %05d'%((((lcf-cnt)/lcf)*100)//1, lcf-cnt, lcf),end='')
|
|
img = None
|
|
try: img = PIL.Image.open(src)
|
|
except:
|
|
ignored+=1
|
|
continue
|
|
width, height = img.size
|
|
prop = width/height
|
|
pxls = width*height
|
|
if not (pxls >= minPixels and prop >= proportion[0] and prop <= proportion[1]):
|
|
ignored+=1
|
|
del copyfiles[cnt]
|
|
else:
|
|
kept+=1
|
|
img.close()
|
|
print()
|
|
|
|
print('Copying files...')
|
|
|
|
lcf = len(copyfiles)
|
|
print('\r'+' '*79+'\r'+'%03d%% copied: %05d of %05d'%(0, 0, lcf),end='')
|
|
for (cnt, (src, dst)) in enumerate(copyfiles):
|
|
if os.path.exists(dst): continue
|
|
print('\r'+' '*79+'\r'+'%03d%% copied: %05d of %05d'%((((cnt+1)/lcf)*100)//1, cnt+1, lcf),end='')
|
|
try:
|
|
shutil.copyfile(src, dst)
|
|
except KeyboardInterrupt as e:
|
|
print()
|
|
print('\r'+' '*79+'\r'+'Deleting interrupted file...',end='')
|
|
os.remove(dst)
|
|
print('\r'+' '*79+'\r'+'Aborted safely',end='')
|
|
print()
|
|
raise e
|
|
print()
|
|
print()
|
|
print('{0:>5} files were kept'.format(kept))
|
|
print('{0:>5} files were ignored'.format(ignored))
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|