~mdkcore/dotbins

dotbins/imgurme -rwxr-xr-x 6.8 KiB
2559c86bRodrigo Oliveira [power] Use loginctl instead of pm-* 1 year, 9 months ago
                                                                                
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python2
"""Parse and download images from an Imgur gallery.

The creation of this script was inspired by Methos25's rice [1]. I loved the
wallpaper he was using, and looking on the Imgur gallery [2] he told us he got
the image, I had the idea to create a script to download all images from there
and randomly pick one to set as wallpaper.

Well, this script do the first part, following the KISS philosophy. The second
part will be done later on.

Thanks to:
    - Baba_Puh, for pointing me out the correct spell of Imgur :p
    - ecterovachor, for suggesting using the python version on the bang line

[1] https://www.reddit.com/r/unixporn/comments/4mtww2/i3gaps_a_little_materialistic/  # noqa
[2] https://imgur.com/gallery/SwhjO
"""

import os
import re
import sys

from urllib2 import urlopen
from HTMLParser import HTMLParser
from argparse import ArgumentParser

__author__ = "Rodrigo Oliveira"
__version__ = "1.0.0"
__maintainer__ = "Rodrigo Oliveira"
__email__ = "rodrigo@deadbodyoutline.com"


class Arguments():
    url = None
    path = None

    def __init__(self):
        self._parse_args()

    def _parse_args(self):
        parser = ArgumentParser()

        parser.add_argument("url",
                            type=str,
                            metavar='N',
                            nargs=1,
                            help="Imgur gallery URL")
        parser.add_argument("--path",
                            "-p",
                            type=str,
                            default=os.getcwd(),
                            help="Path to save images (default: current)")
        parser.add_argument("--limit",
                            "-l",
                            type=int,
                            default=0,
                            help="Limit the number of images to download\
                            (default: 0, all images)")
        parser.add_argument("--print-to-file",
                            "-f",
                            action="store_true",
                            default=False,
                            help="Log messages to '~/.imgurme.log'. Note that\
                            this file will be replaced at each run\
                            (default: false)")

        args = parser.parse_args()

        self.url = args.url[0].strip()
        self.path = args.path.strip()
        self.limit = args.limit
        self.log = args.print_to_file


class ImgurParser(HTMLParser):

    def __init__(self):
        # disclaimer: HTMLParser is old style class, do not support 'super'
        # initialization style :/
        HTMLParser.__init__(self)

        self._images = []
        self._scheme = None

    def _validate_url(self, url):
        regex = ur'http[s]?://(?:w{3}\.)?imgur.com/(?:[a-zA-Z]|[0-9]|[$-_@.&+]\
                |[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))*'  # neat

        return re.match(regex, url)

    def _validate_image(self, image):
        """Check if parsed image name has one of the supported extensions

        Currently supporting JPG and PNG. Do Imgur support any other?

        :param image: the image string to validate
        """
        regex = ur'.*(?:.png|.jpg)$'

        return re.match(regex, image)

    def parse(self, url):
        p = PrintOut.instance()

        if not self._validate_url(url):
            p.out("URL '%s' doesn't seems to be a valid Imgur address" % url)
            return

        self._scheme = url.split(":")[0]
        p.out("Parsing %s" % url)

        try:
            f = urlopen(url)
            html = f.read()
            f.close()
        except Exception as e:
            p.out(">> Error; is it a valid URL? (%s)" % e)
            return

        try:
            html = self.unescape(html)
        except Exception as e:
            p.out(">> Error: %s" % e)
            return

        try:
            self.feed(html)
        except Exception as e:
            p.out(">> Error: %s" % e)
            return

        p.out("Found %s images" % len(self._images), 4)

    def handle_starttag(self, tag, attrs):
        if tag == "a":
            for attr, value in attrs:
                if attr == "href" and self._validate_image(value):
                    self._images.append("%s:%s" % (self._scheme, value))

    def images(self):
        return self._images


class Downloader():

    def __init__(self, path, name):
        self._path = path if path != str() else "./"
        self._name = name

        self._download_path = os.path.join(path, dir_name)

    def download(self, images=[], limit=0):
        p = PrintOut.instance()

        if not images:
            return

        if not os.path.exists(self._path):
            p.out("> Path '%s' doesn't exist" % self._path)
            return

        if not os.path.exists(self._download_path):
            p.out("Creating %s/ on %s" % (self._name, self._path))
            try:
                os.mkdir(self._download_path)
            except Exception as e:
                p.out(">> Error: %s" % e)
                return

        p.out("Downloading images to %s/" % self._download_path)
        if limit > 0:
            images = images[:limit]
            p.out("(limiting download to %s images)" % limit, 4)

        num_images = len(images)
        for index, image in enumerate(images):
            image_file = image.split('/')[-1]

            p.out("(%s/%s) %s..." % (index + 1, num_images, image_file), 4)

            image_file = os.path.join(self._download_path, image_file)
            if os.path.isfile(image_file):
                p.out("File already exists, skipping...", 8)
                continue

            try:
                f = open(image_file, 'wb')
                f.write(urlopen(image).read())
                f.close()
            except Exception as e:
                p.out(">> Error: %s" % e)
                return


class PrintOut(object):
    _instance = None
    _where = sys.stdout

    def __del__(self):
        if type(self._where) is file:
            self._where.close()

    @classmethod
    def instance(self):
        if self._instance is None:
            self._instance = self()

        return self._instance

    def set_output(self, file=None):
        if file is None:
            self._where == sys.stdout
            return

        self._where = open(file, 'w+')

    def out(self, str, indent=0):
        print >> self._where, str.rjust(len(str) + indent)

        if type(self._where) is file:
            self._where.flush()

if __name__ == '__main__':
    arguments = Arguments()

    log_file = os.path.join(os.path.expanduser('~'), 'imgurme.log')
    p = PrintOut.instance()
    p.set_output(log_file if arguments.log else None)

    url = arguments.url

    parser = ImgurParser()
    parser.parse(url)

    dir_name = url.split('/')[-1]

    downloader = Downloader(arguments.path, dir_name)
    downloader.download(parser.images(), arguments.limit)