134 lines
4.0 KiB
Python
134 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
import json
|
|
import os
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
import filetype
|
|
import PIL.Image
|
|
|
|
from .system import subredditTools
|
|
from .system.cmdline_parser import parse_cmdline
|
|
|
|
|
|
def readAllFile(d):
|
|
with open(d) as f:
|
|
return f.read()
|
|
|
|
|
|
proportion = (
|
|
5/4,
|
|
21/9
|
|
)
|
|
minPixels = 1440*900
|
|
|
|
|
|
def cmdline(encoded_args: str = None):
|
|
if encoded_args is None:
|
|
return run_with_config()
|
|
else:
|
|
return parse_cmdline(run_with_config, encoded_args)
|
|
|
|
|
|
def run_with_config():
|
|
return main()
|
|
|
|
|
|
def main():
|
|
subreddits_name = set(map(lambda a: a[0],
|
|
filter(lambda a: a[1],
|
|
json.loads(Path('rf.json').read_text())['wallpaper'].items())))
|
|
reddit_posts = json.loads(Path('r_gdl_p.json').read_text())
|
|
link_to_files = json.loads(Path('i_gdl_ffl.json').read_text())
|
|
ids_to_ignore = list(filter(len, map(str.strip, Path(
|
|
'not_wallpaper.txt').read_text().splitlines())))
|
|
copyfiles = list()
|
|
|
|
print('Listing files...')
|
|
|
|
for reddit_post in reddit_posts.values():
|
|
if len(subreddits_name.intersection((posted_on_subreddits := reddit_post['subreddits']))) > 0:
|
|
album_files = list()
|
|
for link in reddit_post['links']:
|
|
for file in link_to_files.get(link, []):
|
|
if file not in album_files:
|
|
album_files.append(file)
|
|
for index, file in enumerate(album_files):
|
|
img_fn = subredditTools.assembleFileName(
|
|
next(iter(posted_on_subreddits)),
|
|
reddit_post,
|
|
index,
|
|
Path(file).suffix.replace('.', '')
|
|
)
|
|
nsfwsafe = 'nsfw' if reddit_post['nsfw'] else 'safe'
|
|
file_to = Path('w').joinpath(nsfwsafe).joinpath(img_fn)
|
|
if reddit_post['datakey'] not in ids_to_ignore:
|
|
copyfiles.append((file, file_to))
|
|
elif file_to.exists():
|
|
file_to.unlink()
|
|
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 exc:
|
|
print()
|
|
print('\r'+' '*79+'\r'+'Deleting interrupted file...', end='')
|
|
os.remove(dst)
|
|
print('\r'+' '*79+'\r'+'Aborted safely', end='')
|
|
print()
|
|
raise exc
|
|
print()
|
|
print()
|
|
print('{0:>5} files were kept'.format(kept))
|
|
print('{0:>5} files were ignored'.format(ignored))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|