summaryrefslogtreecommitdiffstats
path: root/4grab.py
blob: f8cc6af023ea957954cbc7bb8943dd68dd16d698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/env python

######################################################################
# 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/>.
######################################################################

import optparse
import sys

import config
import download

base_url = "http://boards.4chan.org/"
parser = optparse.OptionParser()

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 :-)""")

(options, args) = parser.parse_args()

if options.confval and options.tempcat:
    print "Cannot set a value and download"
    exit(1)

if options.confval:
    if not config.Configuration().option_exists(options.confval[0]):
        print "%s: error: %s is not a valid configuration option" % (sys.argv[0], options.confval[0])
        exit(1)
    print "Setting", options.confval[0], "to", options.confval[1]
    config.Configuration().set_option(options.confval[0], options.confval[1])
    config.Configuration().save()
    exit(0)
elif options.thread:
    if options.thread[:7] == "http://":
        t = download.get_image_links("", [options.thread])
    elif options.tempcat:
        url = "%s%s/res/" % (base_url, options.tempcat)
        t = download.get_image_links(url, [options.thread])
    else:
        print "if THREAD is not an absolute URL, CATEGORY must also be specified"
        exit(1)
    download.get_images(t)
    exit(0)
elif options.tempcat:
    config.Configuration().set_category(options.tempcat)

base_url = "%s%s/" % (base_url, config.Configuration().get_category())

t = download.get_thread_links(base_url)
t = download.get_image_links(base_url, t)
download.get_images(t)