Changes to riddle2.py

- progress bar is only half the width
- modified test option
- added option to save everything in one zipfile
pull/7/head
Trivernis 6 years ago
parent 633f84202a
commit bfec3157a5

@ -21,11 +21,12 @@ hdr = { # request header
errors = {} errors = {}
def print_progress(iteration, total, prefix='', suffix='', decimals=1, length=100, fill=''): def print_progress(iteration, total, prefix='', suffix='', decimals=1, length=50, fill=''):
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total))) percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filled_length = int(length * iteration // total) filled_length = int(length * iteration // total)
bar = fill * filled_length + '-' * (length - filled_length) bar = fill * filled_length + '-' * (length - filled_length)
print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end='\r') print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end='\r')
sys.stdout.flush()
# Print New Line on Complete # Print New Line on Complete
if iteration == total: if iteration == total:
print() print()
@ -185,13 +186,16 @@ def download_images(imgs, zfile):
print('[+] Added %s files to the zipfile' % added) print('[+] Added %s files to the zipfile' % added)
def download_subreddit(sub, count=-1): def download_subreddit(sub, count=-1, out=None):
mode = 'w' mode = 'w'
if os.path.isfile(sub + '.zip'): zname = sub + '.zip'
if out:
zname = out
if os.path.isfile(zname):
mode = 'a' mode = 'a'
url = 'https://old.reddit.com/r/%s/' % sub url = 'https://old.reddit.com/r/%s/' % sub
imgs = get_img4sub(url, length=count) imgs = get_img4sub(url, length=count)
zfile = zipfile.ZipFile('%s.zip' % sub, mode) zfile = zipfile.ZipFile(zname, mode)
download_images(imgs, zfile) download_images(imgs, zfile)
zfile.close() zfile.close()
@ -207,6 +211,9 @@ def parser_init():
parser.add_option('-c', '--count', dest='count', parser.add_option('-c', '--count', dest='count',
type='int', default=-1, type='int', default=-1,
help='The number of images to download.') help='The number of images to download.')
parser.add_option('-o', '--output', dest='output',
type='str', default=None,
help='The name of the output zipfile. If none is specified, it\'s the subreddits name.')
parser.add_option('-t', '--test', dest='test', parser.add_option('-t', '--test', dest='test',
action='store_true', default=False, action='store_true', default=False,
help='Tests the functions of the script') help='Tests the functions of the script')
@ -215,17 +222,19 @@ def parser_init():
def main(): def main():
options, subreddits = parser_init() options, subreddits = parser_init()
if options.count:
count = options.count count = options.count
else: output = options.output
count = -1
if options.test: if options.test:
count = 1 count = 1
subreddits = ['python'] subreddits = ['python']
output = 'test.zip'
for sub in subreddits: for sub in subreddits:
print('[~] Downloading %s' % sub) print('[~] Downloading %s' % sub)
download_subreddit(sub, count=count) download_subreddit(sub, count=count, out=output)
cleanup() cleanup()
if options.test:
os.remove(output)
if len(errors.keys()) > 0:
print(errors) print(errors)

Loading…
Cancel
Save