reddit-image-wall-getter/reddit_imgs/runner.py

216 lines
7.1 KiB
Python
Raw Normal View History

2017-12-29 22:54:22 +00:00
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import reddit_imgs.sync
import reddit_imgs.fetch
import reddit_imgs.fetch2
2017-12-29 22:54:22 +00:00
import reddit_imgs.reorganize
2018-01-07 03:57:39 +00:00
import reddit_imgs.wallpapers
2020-05-13 21:07:05 +00:00
import reddit_imgs.thumbnailize
import reddit_imgs.hashit
2020-07-20 01:54:26 +00:00
import reddit_imgs.hashit2
2020-06-01 03:20:23 +00:00
import reddit_imgs.normalizetobmp
2020-05-13 21:07:05 +00:00
import reddit_imgs.cachedhash
2020-07-20 01:54:26 +00:00
import reddit_imgs.download_pruner
import reddit_imgs.suggest_subreddits_from_links
import reddit_imgs.condensate_hashes
2017-12-29 22:54:22 +00:00
import os
2018-01-07 03:57:39 +00:00
import sys
2017-12-29 22:54:22 +00:00
import shutil
wdir = os.path.abspath('.')
2020-07-20 01:54:26 +00:00
2017-12-29 22:54:22 +00:00
def ensureFolderAvailability():
2020-07-20 01:54:26 +00:00
if not os.path.exists(os.path.join(wdir, 'w')):
os.makedirs(os.path.join(wdir, 'w'))
if not os.path.exists(os.path.join(wdir, 'd')):
os.makedirs(os.path.join(wdir, 'd'))
if not os.path.exists(os.path.join(wdir, 'i')):
os.makedirs(os.path.join(wdir, 'i'))
if not os.path.exists(os.path.join(wdir, 'r')):
os.makedirs(os.path.join(wdir, 'r'))
2017-12-29 22:54:22 +00:00
def managesubreddits():
i = ''
2020-07-20 01:54:26 +00:00
while i != '0':
2017-12-29 22:54:22 +00:00
print('\n'*100)
print('----------------------------------------------')
print(' Subreddit Manager ')
print('----------------------------------------------')
print('1) List monitored subreddits')
print('2) Add monitored subreddit')
print('3) Remove monitored subreddit')
2018-01-07 03:57:39 +00:00
print('4) Set as wallpaper source')
print('5) Unset as wallpaper source')
2017-12-29 22:54:22 +00:00
print()
print('0) Back')
print('----------------------------------------------')
print()
print('Enter your choice:')
i = input()
i = i.strip()
print()
print()
2020-07-20 01:54:26 +00:00
subreddits_dir = os.path.join(wdir, 'r')
def subreddits_isfolder(sr): return os.path.isdir(
os.path.join(subreddits_dir, sr))
subreddits = sorted(
filter(subreddits_isfolder, os.listdir(subreddits_dir)))
2018-01-07 03:57:39 +00:00
if i in ['1', '3', '4', '5']:
2017-12-29 22:54:22 +00:00
print('Subreddits monitored:')
for sr in subreddits:
2020-07-20 01:54:26 +00:00
print('/r/{0}'.format(sr), end='')
if os.path.isfile(os.path.join(subreddits_dir, sr, 'wallpaper.flag')):
2018-01-07 03:57:39 +00:00
print('\t\t(wallpaper)')
else:
print()
2017-12-29 22:54:22 +00:00
print()
2020-07-20 01:54:26 +00:00
if i == '1':
2017-12-29 22:54:22 +00:00
print('Press enter to continue')
input()
2020-07-20 01:54:26 +00:00
if i == '3':
2017-12-29 22:54:22 +00:00
print('Enter the subreddit you want to get rid of:')
rem = input('/r/')
2020-07-20 01:54:26 +00:00
try:
shutil.rmtree(os.path.join(subreddits_dir, rem))
except:
pass
2017-12-29 22:54:22 +00:00
print()
print('Done.')
print('Press enter to continue')
input()
2020-07-20 01:54:26 +00:00
elif i == '2':
2017-12-29 22:54:22 +00:00
print('Enter the subreddit you want to add:')
add = input('/r/')
2020-07-20 01:54:26 +00:00
try:
os.makedirs(os.path.join(subreddits_dir, add))
except:
pass
2017-12-29 22:54:22 +00:00
print()
print('Done.')
print('Press enter to continue')
input()
2020-07-20 01:54:26 +00:00
elif i == '4':
2018-01-07 03:57:39 +00:00
print('Enter the subreddit you want to set as wallpaper source:')
add = input('/r/')
try:
2020-07-20 01:54:26 +00:00
dd = os.path.join(subreddits_dir, add)
2018-01-07 03:57:39 +00:00
if not os.path.exists(dd):
os.makedirs(dd)
2020-07-20 01:54:26 +00:00
f = open(os.path.join(dd, 'wallpaper.flag'), 'w')
2018-01-07 03:57:39 +00:00
f.write('')
f.close()
2020-07-20 01:54:26 +00:00
except:
pass
2018-01-07 03:57:39 +00:00
print()
print('Done.')
print('Press enter to continue')
input()
2020-07-20 01:54:26 +00:00
elif i == '5':
2018-01-07 03:57:39 +00:00
print('Enter the subreddit you want to unset as wallpaper source:')
add = input('/r/')
try:
2020-07-20 01:54:26 +00:00
dd = os.path.join(subreddits_dir, add)
2018-01-07 03:57:39 +00:00
if not os.path.exists(dd):
os.makedirs(dd)
2020-07-20 01:54:26 +00:00
f = open(os.path.join(dd, 'wallpaper.flag'), 'w')
2018-01-07 03:57:39 +00:00
f.write('')
f.close()
os.remove(os.path.join(dd, 'wallpaper.flag'))
2020-07-20 01:54:26 +00:00
except:
pass
2018-01-07 03:57:39 +00:00
print()
print('Done.')
print('Press enter to continue')
input()
2017-12-29 22:54:22 +00:00
2020-07-20 01:54:26 +00:00
2017-12-29 22:54:22 +00:00
def mainmenu():
i = ''
2020-07-20 01:54:26 +00:00
while i != '0':
2017-12-29 22:54:22 +00:00
print('\n'*100)
print('----------------------------------------------')
print(' Reddit Image Downloader ')
print('----------------------------------------------')
print('1) Manage subreddits')
print('2) Get link list to be downloaded from reddit')
print('3) Download grabbed links')
2020-05-13 21:07:05 +00:00
print('4) Organize by hashes')
print('5) Generate thumbnails')
print('6) Group and put nice names on downloaded data')
print('7) Sepparate wallpapers')
2017-12-29 22:54:22 +00:00
print()
print('0) Quit')
print('----------------------------------------------')
print()
print('Enter your choice:')
i = input()
i = i.strip()
2020-07-20 01:54:26 +00:00
if i == '1':
2017-12-29 22:54:22 +00:00
managesubreddits()
2020-07-20 01:54:26 +00:00
elif i == '2':
2017-12-29 22:54:22 +00:00
reddit_imgs.sync.main()
2020-07-20 01:54:26 +00:00
elif i == '3':
2017-12-29 22:54:22 +00:00
reddit_imgs.fetch.main()
2020-07-20 01:54:26 +00:00
elif i == '4':
2020-05-13 21:07:05 +00:00
reddit_imgs.hashit.main()
2020-07-20 01:54:26 +00:00
elif i == '5':
2020-05-13 21:07:05 +00:00
reddit_imgs.thumbnailize.main()
2020-07-20 01:54:26 +00:00
elif i == '6':
2020-05-13 21:07:05 +00:00
reddit_imgs.reorganize.main()
2020-07-20 01:54:26 +00:00
elif i == '7':
2018-01-07 03:57:39 +00:00
reddit_imgs.wallpapers.main()
2017-12-29 22:54:22 +00:00
2020-07-20 01:54:26 +00:00
2017-12-29 22:54:22 +00:00
def main():
2020-07-20 01:54:26 +00:00
# ensureFolderAvailability()
if len(sys.argv) > 1:
2018-01-07 03:57:39 +00:00
cmdline()
else:
mainmenu()
2020-07-20 01:54:26 +00:00
2018-01-07 03:57:39 +00:00
def cmdline():
2020-04-01 03:53:16 +00:00
cmds = sys.argv[1:]
2020-05-13 21:07:05 +00:00
available_commands = ((
2020-07-20 01:54:26 +00:00
('sync', reddit_imgs.sync.cmdline),
('fetch', reddit_imgs.fetch2.cmdline),
('suggest_subreddits_from_links', reddit_imgs.suggest_subreddits_from_links.cmdline),
('prune_downloads', reddit_imgs.download_pruner.cmdline),
('hashit', reddit_imgs.hashit2.cmdline),
('condensate_hashes', reddit_imgs.condensate_hashes.cmdline),
# ('cachedhash', reddit_imgs.cachedhash.main),
# ('hashit', reddit_imgs.hashit.main),
# ('normalizetobmp', reddit_imgs.normalizetobmp.main),
# ('thumbnailize', reddit_imgs.thumbnailize.main),
# ('reorganize', reddit_imgs.reorganize.main),
# ('wallpapers', reddit_imgs.wallpapers.main),
2020-05-13 21:07:05 +00:00
))
2020-07-20 01:54:26 +00:00
available_commands_names = tuple(
list(map(lambda a: a[0], available_commands)))
2020-04-01 03:53:16 +00:00
for cmd in cmds:
2020-07-20 01:54:26 +00:00
if cmd.split(':', 1)[0] not in available_commands_names:
print('Usage {0} [{1}]'.format(sys.argv[0],
'/'.join(available_commands_names)))
2020-04-01 03:53:16 +00:00
return
for cmd in cmds:
2020-05-13 21:07:05 +00:00
command_ran = False
for acmd in available_commands:
2020-07-20 01:54:26 +00:00
if cmd.split(':', 1)[0] == acmd[0]:
x = cmd.split(':', 1)
2020-05-13 21:07:05 +00:00
command_ran = True
2020-07-20 01:54:26 +00:00
fcmd = acmd[1]
if len(x) == 1:
fcmd()
else:
fcmd(encoded_args=x[1])
2020-05-13 21:07:05 +00:00
if not command_ran:
2020-07-20 01:54:26 +00:00
print('Usage {0} [{1}]'.format(sys.argv[0],
'/'.join(available_commands_names)))
2017-12-29 22:54:22 +00:00
if __name__ == '__main__':
main()