2010-03-10 12:13:27 +01:00
|
|
|
#!/usr/bin/python
|
2010-02-09 02:45:56 +01:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Copyright 2009, 2010 ryuslash
|
|
|
|
#
|
|
|
|
# This file is part of 4grab.
|
|
|
|
#
|
|
|
|
# 4grab is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 4grab is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with 4grab. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
######################################################################
|
|
|
|
|
2010-02-09 01:32:42 +01:00
|
|
|
import optparse
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import config
|
2010-02-09 02:10:17 +01:00
|
|
|
import download
|
2010-03-07 00:20:37 +01:00
|
|
|
import progressbar
|
2010-02-09 01:32:42 +01:00
|
|
|
|
2010-02-11 22:05:37 +01:00
|
|
|
base_url = "http://boards.4chan.org/"
|
2010-02-09 01:32:42 +01:00
|
|
|
parser = optparse.OptionParser()
|
2010-03-07 00:20:37 +01:00
|
|
|
downloader = download.Downloader(progressbar.Progress)
|
2010-02-09 01:32:42 +01:00
|
|
|
|
2010-02-11 23:46:23 +01:00
|
|
|
def walk_with_wizard(baseurl):
|
2010-02-12 00:34:36 +01:00
|
|
|
wzrd_msg = "Pilates! *SHAZAM* Here they come!"
|
2010-02-11 23:46:23 +01:00
|
|
|
print "Alright, let me put on my robe and wizard hat."
|
|
|
|
|
|
|
|
# Single or all
|
|
|
|
inp = None
|
|
|
|
prompt = "Would you like to download a single thread, or all? "
|
|
|
|
inp = raw_input(prompt)
|
|
|
|
while (inp != "single" and inp != "all"):
|
|
|
|
print "Please type single or all"
|
|
|
|
inp = raw_input(prompt)
|
|
|
|
|
|
|
|
if inp == "single":
|
|
|
|
inp = raw_input("Which thread would you like to download? ")
|
|
|
|
if inp[:7] == "http://":
|
2010-03-07 00:20:37 +01:00
|
|
|
t = downloader.get_image_links("", [inp])
|
2010-02-11 23:46:23 +01:00
|
|
|
else:
|
|
|
|
thread = inp
|
|
|
|
inp = raw_input("Which category is this thread in? ")
|
2010-02-12 00:34:36 +01:00
|
|
|
print wzrd_msg
|
2010-03-12 21:50:10 +01:00
|
|
|
t = downloader.get_image_links("%s%s/res/" % (baseurl, inp),
|
|
|
|
[thread])
|
2010-02-11 23:46:23 +01:00
|
|
|
else:
|
|
|
|
inp = raw_input("Which category would you like to download? ")
|
|
|
|
config.Configuration().set_category(inp)
|
|
|
|
baseurl = "%s%s/" % (baseurl, config.Configuration().get_category())
|
|
|
|
|
2010-02-12 00:34:36 +01:00
|
|
|
print wzrd_msg
|
2010-03-07 00:20:37 +01:00
|
|
|
t = downloader.get_thread_links(baseurl)
|
|
|
|
t = downloader.get_image_links(baseurl, t)
|
|
|
|
(skipped, failed, downloaded, total) = downloader.get_images(t)
|
2010-02-12 00:04:34 +01:00
|
|
|
print "Downloaded: ", downloaded
|
|
|
|
print "Skipped: ", skipped
|
|
|
|
print "Failed: ", failed
|
|
|
|
print "Total: ", total
|
2010-02-11 23:46:23 +01:00
|
|
|
|
2010-02-09 02:45:56 +01:00
|
|
|
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.""")
|
2010-03-07 00:20:37 +01:00
|
|
|
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",
|
2010-03-12 21:50:10 +01:00
|
|
|
help="Download only THREAD. If THREAD is only an ID, "
|
|
|
|
"CATEGORY must also be set. Otherwise, no problem :-)")
|
2010-03-07 00:20:37 +01:00
|
|
|
parser.add_option("-w",
|
|
|
|
"--wizard",
|
|
|
|
action="store_true",
|
|
|
|
dest="wizard",
|
2010-03-12 21:50:10 +01:00
|
|
|
help="I'll put on my robe and wizard hat and help you "
|
|
|
|
"get some of those pictures you like")
|
2010-02-09 01:32:42 +01:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2010-03-12 21:50:10 +01:00
|
|
|
if options.confval and (options.tempcat
|
|
|
|
or options.thread
|
|
|
|
or options.wizard):
|
2010-02-11 23:46:23 +01:00
|
|
|
print "Can't configure something and do something else too."
|
|
|
|
exit(1)
|
2010-03-12 21:50:10 +01:00
|
|
|
if options.wizard and (options.tempcat
|
|
|
|
or options.thread
|
|
|
|
or options.confval):
|
2010-02-11 23:46:23 +01:00
|
|
|
print "Can't take a walk with the wizard and do something else too."
|
2010-02-11 21:20:16 +01:00
|
|
|
exit(1)
|
|
|
|
|
2010-02-09 01:32:42 +01:00
|
|
|
if options.confval:
|
|
|
|
if not config.Configuration().option_exists(options.confval[0]):
|
2010-03-12 21:50:10 +01:00
|
|
|
print ("%s: error: %s is not a "
|
|
|
|
"valid configuration option") % (sys.argv[0],
|
|
|
|
options.confval[0])
|
2010-02-09 01:32:42 +01:00
|
|
|
exit(1)
|
|
|
|
print "Setting", options.confval[0], "to", options.confval[1]
|
2010-03-12 21:50:10 +01:00
|
|
|
config.Configuration().set_option(options.confval[0],
|
|
|
|
options.confval[1])
|
2010-02-09 02:10:17 +01:00
|
|
|
config.Configuration().save()
|
|
|
|
exit(0)
|
2010-03-12 21:50:10 +01:00
|
|
|
|
2010-02-11 23:46:23 +01:00
|
|
|
elif options.wizard:
|
2010-03-12 21:50:10 +01:00
|
|
|
try:
|
|
|
|
walk_with_wizard(base_url)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print
|
|
|
|
print "Alright, no more wizard hat and robe then. Goodbye"
|
2010-02-11 23:46:23 +01:00
|
|
|
exit(0)
|
2010-03-12 21:50:10 +01:00
|
|
|
|
2010-02-11 22:05:37 +01:00
|
|
|
elif options.thread:
|
2010-03-12 21:50:10 +01:00
|
|
|
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"
|
2010-02-11 22:05:37 +01:00
|
|
|
exit(0)
|
2010-03-12 21:50:10 +01:00
|
|
|
|
2010-02-11 21:20:16 +01:00
|
|
|
elif options.tempcat:
|
|
|
|
config.Configuration().set_category(options.tempcat)
|
2010-02-09 02:10:17 +01:00
|
|
|
|
2010-02-11 22:05:37 +01:00
|
|
|
base_url = "%s%s/" % (base_url, config.Configuration().get_category())
|
2010-02-09 02:10:17 +01:00
|
|
|
|
2010-03-12 21:50:10 +01:00
|
|
|
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."
|