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

186 lines
6.3 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.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-06-01 03:20:23 +00:00
import reddit_imgs.normalizetobmp
2020-05-13 21:07:05 +00:00
import reddit_imgs.cachedhash
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('.')
def ensureFolderAvailability():
2018-01-07 03:57:39 +00:00
if not os.path.exists(os.path.join(wdir,'w')):
os.makedirs(os.path.join(wdir,'w'))
2017-12-29 22:54:22 +00:00
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'))
def managesubreddits():
i = ''
while i!='0':
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()
subreddits_dir = os.path.join(wdir,'r')
subreddits_isfolder = lambda sr: 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:
2018-01-07 03:57:39 +00:00
print('/r/{0}'.format(sr),end='')
if os.path.isfile(os.path.join(subreddits_dir,sr,'wallpaper.flag')):
print('\t\t(wallpaper)')
else:
print()
2017-12-29 22:54:22 +00:00
print()
if i=='1':
print('Press enter to continue')
input()
if i=='3':
print('Enter the subreddit you want to get rid of:')
rem = input('/r/')
try: shutil.rmtree(os.path.join(subreddits_dir,rem))
except: pass
print()
print('Done.')
print('Press enter to continue')
input()
elif i=='2':
print('Enter the subreddit you want to add:')
add = input('/r/')
try: os.makedirs(os.path.join(subreddits_dir,add))
except: pass
print()
print('Done.')
print('Press enter to continue')
input()
2018-01-07 03:57:39 +00:00
elif i=='4':
print('Enter the subreddit you want to set as wallpaper source:')
add = input('/r/')
try:
dd = os.path.join(subreddits_dir,add)
if not os.path.exists(dd):
os.makedirs(dd)
f = open(os.path.join(dd, 'wallpaper.flag'),'w')
f.write('')
f.close()
except: pass
print()
print('Done.')
print('Press enter to continue')
input()
elif i=='5':
print('Enter the subreddit you want to unset as wallpaper source:')
add = input('/r/')
try:
dd = os.path.join(subreddits_dir,add)
if not os.path.exists(dd):
os.makedirs(dd)
f = open(os.path.join(dd, 'wallpaper.flag'),'w')
f.write('')
f.close()
os.remove(os.path.join(dd, 'wallpaper.flag'))
except: pass
print()
print('Done.')
print('Press enter to continue')
input()
2017-12-29 22:54:22 +00:00
def mainmenu():
i = ''
while i!='0':
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()
if i=='1':
managesubreddits()
elif i=='2':
reddit_imgs.sync.main()
elif i=='3':
reddit_imgs.fetch.main()
elif i=='4':
2020-05-13 21:07:05 +00:00
reddit_imgs.hashit.main()
2018-01-07 03:57:39 +00:00
elif i=='5':
2020-05-13 21:07:05 +00:00
reddit_imgs.thumbnailize.main()
elif i=='6':
reddit_imgs.reorganize.main()
elif i=='7':
2018-01-07 03:57:39 +00:00
reddit_imgs.wallpapers.main()
2017-12-29 22:54:22 +00:00
def main():
ensureFolderAvailability()
2018-01-07 03:57:39 +00:00
if len(sys.argv)>1:
cmdline()
else:
mainmenu()
def cmdline():
2020-04-01 03:53:16 +00:00
cmds = sys.argv[1:]
2020-05-13 21:07:05 +00:00
available_commands = ((
('sync', reddit_imgs.sync.main),
('fetch', reddit_imgs.fetch.main),
('fetchretryingemptyalbuns', reddit_imgs.fetch.retry),
('cachedhash', reddit_imgs.cachedhash.main),
('hashit', reddit_imgs.hashit.main),
2020-06-01 03:20:23 +00:00
('normalizetobmp', reddit_imgs.normalizetobmp.main),
2020-05-13 21:07:05 +00:00
('thumbnailize', reddit_imgs.thumbnailize.main),
('reorganize', reddit_imgs.reorganize.main),
('wallpapers', reddit_imgs.wallpapers.main),
))
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-05-13 21:07:05 +00:00
if cmd 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:
if cmd == acmd[0]:
command_ran = True
acmd[1]()
if not command_ran:
print('Usage {0} [{1}]'.format(sys.argv[0], '/'.join(available_commands_names)))
2017-12-29 22:54:22 +00:00
if __name__ == '__main__':
main()