#!/usr/bin/python # -*- coding: UTF-8 -*- # # Copyright (C) 2015 Zhuyifei1999 # # 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see # import os, sys sys.stderr = sys.stdout from shared import printheader, die, GallicaBase import subprocess class GallicaCore(GallicaBase): def __init__(self): super(GallicaCore, self).__init__() def run(self): self.retrieve() self.append() self.removetemp() print "Finished!" print '' def download(self, zoom, x, y): if self.fileexists(zoom, x, y): return 0 command = ["wget", self.wget_address(zoom, x, y), "-O", self.getfilename(zoom, x, y)] + ["--quiet"] return subprocess.call(command) def getzoom(self): sha1 = "" for zoom in range(5, 20): self.download(zoom, 0, 0) cursha1 = subprocess.check_output(["sha1sum", self.getfilename(zoom, 0, 0)]).split()[0].strip() if cursha1 != sha1: sha1 = cursha1 continue break print "Found zoom: " + str(zoom) return zoom def retrieve(self): self.zoom = self.getzoom() x = 0 y = 0 break_x = True break_y = True self.max_x = 0 self.max_y = 0 while break_y: test = False x = 0 break_x = True while break_x: address = self.wget_address(self.zoom, x, y) name = self.getfilename(self.zoom, x, y) ret = self.download(self.zoom, x, y) if (ret != 0) or (os.stat(name).st_size == 0) or (subprocess.check_output(['file', '-bi', name]).strip().split("/")[0] != "image"): os.remove(name) break_x = False else: self.max_x = max(x, self.max_x) print "Downloaded %02i-%02i...!" % (x, y) sys.stdout.flush() x = x + 1 test = True if test: self.max_y = max(y, self.max_y) y = y + 1 else: break_y = False def append(self): print "Appending files...!" for j in range(0, self.max_y + 1): gen = lambda n: [self.getfilename(self.zoom, i, j) for i in range(0, n+1)] imgs = gen(self.max_x) command = ["convert"] + imgs + ["+append", "-quality", "100", self.getfilename(self.zoom, None, j)] # + ["-debug", "All"] if subprocess.call(command) != 0: die("Convert fail") gen = lambda n: [self.getfilename(self.zoom, None, i) for i in range(0, n+1)] imgs = gen(self.max_y) command = ["convert"] + imgs + ["-append", "-quality", "100", self.getfilename(self.zoom, None, None)] # + ["-debug", "All"] if subprocess.call(command) != 0: die("Convert fail") def removetemp(self): print "Removing temp files...!" for j in range(0, self.max_y + 1): gen = lambda n: [self.getfilename(self.zoom, i, j) for i in range(0, n+1)] imgs = gen(self.max_x) for img in imgs: os.remove(img) gen = lambda n: [self.getfilename(self.zoom, None, i) for i in range(0, n+1)] imgs = gen(self.max_y) for img in imgs: os.remove(img) for zoom in range(5, self.zoom): os.remove(self.getfilename(zoom, 0, 0)) def main(): printheader() GallicaCore().run() if __name__ == "__main__": try: main() finally: pass