-
Notifications
You must be signed in to change notification settings - Fork 1
/
commitstrip.py
executable file
·73 lines (68 loc) · 2.71 KB
/
commitstrip.py
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
#! /usr/bin/python
# Copyright 2015 Balasankar C <[email protected]>
#
# This program 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.
#
# This program 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import urllib
import os
import argparse
import sys
import lxml.html
import urlparse
def encodeurl(url):
s, a, p, q, f = urlparse.urlsplit(url)
p = urllib.quote(p.encode('utf8'))
return str(urlparse.urlunsplit((s, a, p, q, f)))
parser = argparse.ArgumentParser(description='Download commistrip images',
formatter_class=lambda prog:
argparse.HelpFormatter(
prog, max_help_position=7))
parser.add_argument("language", help="Specify the language - en or fr")
parser.add_argument("outputdir", help="Specify the output directory")
parser.add_argument("-v", "--verbosity",
help="Print download url", action="store_true")
parser.add_argument("-ps", "--pagestart", help="Specify starting page number")
parser.add_argument("-pe", "--pageend", help="Sprcify ending page number")
args = parser.parse_args()
if args.language not in ["en", "fr"]:
print "Language should be either en or fr"
sys.exit()
baseurl = "http://www.commitstrip.com/" + args.language + "/page/"
pagestart = 1
pageend = 100
if args.pagestart:
pagestart = int(args.pagestart)
if args.pageend:
pageend = int(args.pageend)
if not os.path.exists(args.outputdir):
os.mkdir(args.outputdir)
for pagecount in range(pagestart, pageend + 1):
try:
page = baseurl + str(pagecount)
test = lxml.html.parse(page)
imageUrlset = test.xpath(
'//img[contains(@class, "size-full")]/@src')
imageurl = imageUrlset[0]
indexpos = imageurl[::-1].index('/')
filename = imageurl[-indexpos:]
if os.path.isfile(args.outputdir + "/" + filename):
continue
if args.verbosity:
print "Page #" + str(pagecount) + " : " + imageurl
imageurl = encodeurl(imageurl)
urllib.urlretrieve(
imageurl,
filename=args.outputdir + "/" + filename)
except:
continue