forked from milianw/springer_download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
springer_download.py
215 lines (168 loc) · 6.39 KB
/
springer_download.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
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import getopt
import urllib
import re
import tempfile
import shutil
import subprocess
# validate CLI arguments and start downloading
def main(argv):
if not findInPath("pdftk"):
error("You have to install pdftk.")
if not findInPath("iconv"):
error("You have to install iconv.")
try:
opts, args = getopt.getopt(argv, "hl:c:", ["help", "link=","content="])
except getopt.GetoptError:
error()
link = ""
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-c", "--content"):
if link != "":
error("-c and -l arguments are mutually exclusive")
link = "http://springerlink.com/content/" + arg
elif opt in ("-l", "--link"):
if link != "":
error("-c and -l arguments are mutually exclusive")
link = arg
if link == "":
error("You have to define a link.")
if not re.match("https?://(www\.)?springerlink.(com|de)/content/[a-z0-9\-]+(/\?[^/]*)?$", link):
error("Bad link given. See LINK below.")
# remove all arguments from link
link = re.sub(r"/?\?[^/]*$", "/", link)
#make sure the link ends on a slash
if link[-1] != "/":
link += "/"
baseLink = link
chapters = list()
hasFrontMatter = False
hasBackMatter = False
loader = urllib.FancyURLopener()
# Springer protects its ebooks via user-agent-filtering (wget or urllib or none user-agent won't work without spoofing)
loader.addheaders = [('User-Agent', 'Mozilla/4.0')]
bookTitle = ""
while True:
# download page source
try:
print "Please wait, link source is being downloaded...\n\t%s" % link
page = loader.open(link).read()
except IOError, e:
error("Bad link given (%s)" % e)
if bookTitle == "":
match = re.search(r'documentTitles : \["([^"]+)"', page)
if not match or match.group(1).strip() == "":
error("Could not evaluate book title - bad link?")
else:
bookTitle = match.group(1).strip()
print "\nThe book you are trying to download is called '%s'\n" % bookTitle
# get chapters
chapters = re.search(r'documentPdfDownloadUrls : \["([^]]+)"\]', page).group(1).split('","')
for key in chapters:
if key == '':
chapters.remove('')
break
if len(chapters) == 0:
error("No chapters found - bad link?")
print "found %d chapters" % len(chapters)
# setup
curDir = os.getcwd()
tempDir = tempfile.mkdtemp()
os.chdir(tempDir)
i = 1
fileList = list()
for chapterLink in chapters:
if chapterLink[0] == "/":
chapterLink = "http://springerlink.com" + chapterLink
else:
chapterLink = baseLink + chapterLink
print "downloading chapter %d/%d" % (i, len(chapters))
localFile, mimeType = geturl(chapterLink, "%d.pdf" % i)
if mimeType.gettype() != "application/pdf":
os.chdir(curDir)
shutil.rmtree(tempDir)
error("downloaded chapter %s has invalid mime type %s - are you allowed to download it?" % (chapterLink, mimeType.gettype()))
fileList.append(localFile)
i += 1
print "merging chapters"
p1 = subprocess.Popen(["echo", bookTitle], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["iconv", "-f", "UTF-8", "-t" ,"ASCII//TRANSLIT"], stdin=p1.stdout, stdout=subprocess.PIPE)
bookTitlePath = p2.communicate()[0]
bookTitlePath = bookTitlePath.strip()
if bookTitlePath == "":
os.chdir(curDir)
shutil.rmtree(tempDir)
error("could not transliterate book title %s" % bookTitle)
bookTitlePath = bookTitlePath.replace("/", "-")
bookTitlePath = re.sub("\s+", "_", bookTitlePath)
bookTitlePath = curDir + "/%s.pdf" % bookTitlePath
if len(fileList) == 1:
shutil.move(fileList[0], bookTitlePath)
else:
os.system("pdftk %s cat output '%s'" % (" ".join(fileList), bookTitlePath))
# cleanup
os.chdir(curDir)
shutil.rmtree(tempDir)
print "book %s was successfully downloaded, it was saved to %s" % (bookTitle, bookTitlePath)
sys.exit()
# give a usage message
def usage():
print """Usage:
%s [OPTIONS]
Options:
-h, --help Display this usage message
-l LINK, --link=LINK defines the link of the book you intend to download
-c HASH, --content=HASH builds the link from a given HASH (see below)
You have to set exactly one of these options.
LINK:
The link to your the detail page of the ebook of your choice on SpringerLink.
It lists book metadata and has a possibly paginated list of the chapters of the book.
It has the form:
http://springerlink.com/content/HASH/STUFF
Where: HASH is a string consisting of lower-case, latin chars and numbers.
It alone identifies the book you intent do download.
STUFF is optional and looks like ?p=...&p_o=... or similar. Will be stripped.
""" % os.path.basename(sys.argv[0])
# raise an error and quit
def error(msg=""):
if msg != "":
print "\nERROR: %s\n" % msg
usage()
sys.exit(2)
return None
# based on http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def findInPath(prog):
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, prog)
if os.path.exists(exe_file) and os.access(exe_file, os.X_OK):
return True
return False
# based on http://mail.python.org/pipermail/python-list/2005-April/319818.html
def _reporthook(numblocks, blocksize, filesize, url=None):
#XXX Should handle possible filesize=-1.
try:
percent = min((numblocks*blocksize*100)/filesize, 100)
except:
percent = 100
if numblocks != 0:
sys.stdout.write("\b"*70)
sys.stdout.write("%-66s%3d%%" % (url, percent))
def geturl(url, dst):
loader = urllib.FancyURLopener()
loader.addheaders = [('User-Agent', 'Mozilla/4.0')]
if sys.stdout.isatty():
response = loader.retrieve(url, dst,
lambda nb, bs, fs, url=url: _reporthook(nb,bs,fs,url))
sys.stdout.write("\n")
else:
response = loader.retrieve(url, dst)
return response
# start program
if __name__ == "__main__":
main(sys.argv[1:])