From 4a9cc7e2b608332f3d41aa47f5f1a893a5eab529 Mon Sep 17 00:00:00 2001 From: ryuslash Date: Wed, 17 Mar 2010 23:11:18 +0100 Subject: optioncreator, extra options config now has an optioncreator property that will allow another module to select how to prompt for a property resolutions and archive options have been added to config file for sorting functionality --- 4grab.py | 201 +++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 104 insertions(+), 97 deletions(-) (limited to '4grab.py') diff --git a/4grab.py b/4grab.py index f2b8da8..2cb8a3f 100755 --- a/4grab.py +++ b/4grab.py @@ -23,6 +23,7 @@ import optparse import sys import config + import download import progressbar @@ -31,15 +32,14 @@ def raw_input_with_default(default, prompt): if inp == "": return default return inp +config._optioncreator = raw_input_with_default base_url = "http://boards.4chan.org/" parser = optparse.OptionParser() downloader = download.Downloader(progressbar.Progress) -conf = config.Configuration() - -conf.set_optioncreator(raw_input_with_default) def walk_with_wizard(baseurl): + conf = config.Configuration() wzrd_msg = "Pilates! *SHAZAM* Here they come!" print "Alright, let me put on my robe and wizard hat." @@ -63,8 +63,8 @@ def walk_with_wizard(baseurl): [thread]) else: inp = raw_input("Which category would you like to download? ") - conf.set_category(inp) - baseurl = "%s%s/" % (baseurl, conf.get_category()) + conf.set_categories([inp]) + baseurl = "%s%s/" % (baseurl, conf.get_categories()[0]) print wzrd_msg t = downloader.get_thread_links(baseurl) @@ -75,102 +75,109 @@ def walk_with_wizard(baseurl): print "Failed: ", failed print "Total: ", total -parser.set_usage( -"""%prog [options] +def parse_commands(): + conf = config.Configuration() + parser.set_usage( + """%prog [options] 4grab Copyright (C) 2009-2010 ryuslash This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.""") -parser.add_option("-e", - nargs=2, - dest="confval", - metavar="CONF VALUE", - help="Set configuration option CONF to be VALUE") -parser.add_option("-c", - "--category", - dest="tempcat", - metavar="CATEGORY", - help="Set the category to CATEGORY only for this run") -parser.add_option("-t", - "--thread", - dest="thread", - metavar="THREAD", - help="Download only THREAD. If THREAD is only an ID, " - "CATEGORY must also be set. Otherwise, no problem :-)") -parser.add_option("-w", - "--wizard", - action="store_true", - dest="wizard", - help="I'll put on my robe and wizard hat and help you " - "get some of those pictures you like") -(options, args) = parser.parse_args() - -if options.confval and (options.tempcat - or options.thread - or options.wizard): - print "Can't configure something and do something else too." - exit(1) -if options.wizard and (options.tempcat - or options.thread - or options.confval): - print "Can't take a walk with the wizard and do something else too." - exit(1) - -if options.confval: - if not conf.option_exists(options.confval[0]): - print ("%s: error: %s is not a " - "valid configuration option") % (sys.argv[0], - options.confval[0]) + parser.add_option("-e", + nargs=2, + dest="confval", + metavar="CONF VALUE", + help="Set configuration option CONF to be VALUE") + parser.add_option("-c", + "--category", + dest="tempcat", + metavar="CATEGORY", + help="Set the category to CATEGORY only for this run") + parser.add_option("-t", + "--thread", + dest="thread", + metavar="THREAD", + help="Download only THREAD. If THREAD is only an ID, " + "CATEGORY must also be set. Otherwise, no problem :-)") + parser.add_option("-w", + "--wizard", + action="store_true", + dest="wizard", + help="I'll put on my robe and wizard hat and help you " + "get some of those pictures you like") + (options, args) = parser.parse_args() + + if options.confval and (options.tempcat + or options.thread + or options.wizard): + print "Can't configure something and do something else too." exit(1) - print "Setting", options.confval[0], "to", options.confval[1] - conf.set_option(options.confval[0], - options.confval[1]) - conf.save() - exit(0) - -elif options.wizard: - try: - walk_with_wizard(base_url) - except KeyboardInterrupt: - print - print "Alright, no more wizard hat and robe then. Goodbye" - exit(0) - -elif options.thread: - try: - if options.thread[:7] == "http://": - t = downloader.get_image_links("", [options.thread]) - elif options.tempcat: - url = "%s%s/res/" % (base_url, options.tempcat) - t = downloader.get_image_links(url, [options.thread]) - else: - print ("if THREAD is not an absolute URL, " - "CATEGORY must also be specified") + + if options.wizard and (options.tempcat + or options.thread + or options.confval): + print "Can't take a walk with the wizard and do something else too." + exit(1) + + if options.confval: + if not conf.option_exists(options.confval[0]): + print ("%s: error: %s is not a valid configuration option" + % (sys.argv[0], options.confval[0])) exit(1) - (skipped, failed, downloaded, total) = downloader.get_images(t) - print "Downloaded: ", downloaded - print "Skipped: ", skipped - print "Failed: ", failed - print "Total: ", total - except KeyboardInterrupt: - print - print "Goodbye" - exit(0) - -elif options.tempcat: - conf.set_category(options.tempcat) - -base_url = "%s%s/" % (base_url, conf.get_category()) - -try: - t = downloader.get_thread_links(base_url) - t = downloader.get_image_links(base_url, t) - (skipped, failed, downloaded, total) = downloader.get_images(t) - print "Downloaded: ", downloaded - print "Skipped: ", skipped - print "Failed: ", failed - print "Total: ", total -except KeyboardInterrupt: - print - print "So you don't want these images? Fine! I'll stop then." + print "Setting", options.confval[0], "to", options.confval[1] + conf.set_option(options.confval[0], + options.confval[1]) + conf.save() + exit(0) + + elif options.wizard: + try: + walk_with_wizard(base_url) + except KeyboardInterrupt: + print + print "Alright, no more wizard hat and robe then. Goodbye" + exit(0) + + elif options.thread: + try: + if options.thread[:7] == "http://": + t = downloader.get_image_links("", [options.thread]) + elif options.tempcat: + url = "%s%s/res/" % (base_url, options.tempcat) + t = downloader.get_image_links(url, [options.thread]) + else: + print ("if THREAD is not an absolute URL, " + "CATEGORY must also be specified") + exit(1) + (skipped, failed, downloaded, total) = downloader.get_images(t) + print "Downloaded: ", downloaded + print "Skipped: ", skipped + print "Failed: ", failed + print "Total: ", total + except KeyboardInterrupt: + print + print "Goodbye" + exit(0) + + elif options.tempcat: + conf.set_categories([options.tempcat]) + +#base_url = "%s%s/" % (base_url, conf.get_categories()) + +if __name__ == "__main__": + conf = config.Configuration() + parse_commands() + for category in conf.get_categories(): + base_url = "%s%s/" % (base_url, category) + try: + t = downloader.get_thread_links(base_url) + t = downloader.get_image_links(base_url, t) + (skipped, failed, downloaded, total) = downloader.get_images(t) + print "Downloaded: ", downloaded + print "Skipped: ", skipped + print "Failed: ", failed + print "Total: ", total + except KeyboardInterrupt: + print + print "So you don't want these images? Fine! I'll stop then." -- cgit v1.2.3-54-g00ecf