summaryrefslogtreecommitdiffstats
path: root/4grab.py
blob: 821adc120571728e681bf9de6433d449f2de807b (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python2

######################################################################
# 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 os

import config
from util import raw_input_with_default
import util

import download
import progressbar
import sorter
import backend

config._optioncreator = raw_input_with_default

base_url = "http://boards.4chan.org/"
parser = optparse.OptionParser()
downloader = download.Downloader(progressbar.Progress)

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("-s",
                      "--sort",
                      action="store_true",
                      dest="sort",
                      help="Sort downloaded images, most handy if you've used "
                      "older versions which didn't sort yet")
    parser.add_option("-l",
                      "--loglevel",
                      nargs=1,
                      dest="loglevel",
                      metavar="LEVEL",
                      help="Changes the default log level to LEVEL")
    (options, args) = parser.parse_args()

    if options.confval and (options.tempcat
                            or options.thread
                            or options.wizard
                            or options.sort):
        print "Can't configure something and do something else too."
        exit(1)

    if options.sort:
        sort = sorter.Sorter()
        for item in os.listdir(conf.get_download_location()):
            sort.act(item)
        exit(0)

    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)
        print "Setting", options.confval[0], "to", options.confval[1]
        conf.set_option(options.confval[0],
                        options.confval[1])
        conf.save()
        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])

    elif options.loglevel is not None:
        util.loglevel = util.LogType.from_int(options.loglevel)

if __name__ == "__main__":
    conf = config.Configuration()
    sort = sorter.Sorter()
    parse_commands()

    downloader.set_on_downloaded(sort.act)
    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:
            be = backend.Backend()
            be.save(True) # Make sure that the downloaded images are saved anyway

            print
            print "So you don't want these images? Fine! I'll stop then."
            util.log(util.LogType.Err, "Quit on user request")