1# -*- coding: utf-8 -*- 2 3# Copyright(C) 2010-2011 Noé Rubinstein 4# 5# This file is part of weboob. 6# 7# weboob is free software: you can redistribute it and/or modify 8# it under the terms of the GNU Lesser General Public License as published by 9# the Free Software Foundation, either version 3 of the License, or 10# (at your option) any later version. 11# 12# weboob is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU Lesser General Public License for more details. 16# 17# You should have received a copy of the GNU Lesser General Public License 18# along with weboob. If not, see <http://www.gnu.org/licenses/>. 19 20from __future__ import print_function 21 22import os 23from re import search, sub 24 25from weboob.tools.application.repl import ReplApplication, defaultcount 26from weboob.capabilities.base import empty 27from weboob.capabilities.gallery import CapGallery, BaseGallery, BaseImage 28from weboob.tools.application.formatters.iformatter import PrettyFormatter 29 30 31__all__ = ['Galleroob'] 32 33 34class GalleryListFormatter(PrettyFormatter): 35 MANDATORY_FIELDS = ('id', 'title') 36 37 def get_title(self, obj): 38 s = obj.title 39 if hasattr(obj, 'cardinality') and not empty(obj.cardinality): 40 s += u' (%d pages)' % obj.cardinality 41 return s 42 43 def get_description(self, obj): 44 if hasattr(obj, 'description') and obj.description: 45 return obj.description 46 47 48class Galleroob(ReplApplication): 49 APPNAME = 'galleroob' 50 VERSION = '2.0' 51 COPYRIGHT = u'Copyright(C) 2011-2014 Noé Rubinstein' 52 DESCRIPTION = 'galleroob browses and downloads web image galleries' 53 SHORT_DESCRIPTION = 'browse and download web image galleries' 54 CAPS = CapGallery 55 EXTRA_FORMATTERS = {'gallery_list': GalleryListFormatter} 56 COMMANDS_FORMATTERS = {'search': 'gallery_list', 'ls': 'gallery_list'} 57 COLLECTION_OBJECTS = (BaseGallery, BaseImage, ) 58 59 def __init__(self, *args, **kwargs): 60 super(Galleroob, self).__init__(*args, **kwargs) 61 62 @defaultcount(10) 63 def do_search(self, pattern): 64 """ 65 search PATTERN 66 67 List galleries matching a PATTERN. 68 """ 69 if not pattern: 70 print('This command takes an argument: %s' % self.get_command_help('search', short=True), file=self.stderr) 71 return 2 72 73 self.start_format(pattern=pattern) 74 for gallery in self.do('search_galleries', pattern=pattern): 75 self.cached_format(gallery) 76 77 def do_download(self, line): 78 """ 79 download ID [FIRST [FOLDER]] 80 81 Download a gallery. 82 83 Begins at page FIRST (default: 0) and saves to FOLDER (default: title) 84 """ 85 _id, first, dest = self.parse_command_args(line, 3, 1) 86 87 if first is None: 88 first = 0 89 else: 90 first = int(first) 91 92 gallery = None 93 _id, backend = self.parse_id(_id) 94 for result in self.do('get_gallery', _id, backends=backend): 95 if result: 96 gallery = result 97 98 if not gallery: 99 print('Gallery not found: %s' % _id, file=self.stderr) 100 return 3 101 102 self.weboob[backend].fillobj(gallery, ('title',)) 103 if dest is None: 104 dest = sub('/', ' ', gallery.title) 105 106 print("Downloading to %s" % dest) 107 108 try: 109 os.mkdir(dest) 110 except OSError: 111 pass # ignore error on existing directory 112 os.chdir(dest) # fail here if dest couldn't be created 113 114 i = 0 115 for img in self.weboob[backend].iter_gallery_images(gallery): 116 i += 1 117 if i < first: 118 continue 119 120 self.weboob[backend].fillobj(img, ('url', 'data')) 121 if img.data is None: 122 self.weboob[backend].fillobj(img, ('url', 'data')) 123 if img.data is None: 124 print("Couldn't get page %d, exiting" % i, file=self.stderr) 125 break 126 127 ext = search(r"\.([^\.]{1,5})$", img.url) 128 if ext: 129 ext = ext.group(1) 130 else: 131 ext = "jpg" 132 133 name = '%03d.%s' % (i, ext) 134 print('Writing file %s' % name) 135 136 with open(name, 'wb') as f: 137 f.write(img.data) 138 139 os.chdir(os.path.pardir) 140 141 def do_info(self, line): 142 """ 143 info ID 144 145 Get information about a gallery. 146 """ 147 _id, = self.parse_command_args(line, 1, 1) 148 149 gallery = self.get_object(_id, 'get_gallery') 150 if not gallery: 151 print('Gallery not found: %s' % _id, file=self.stderr) 152 return 3 153 154 self.start_format() 155 self.format(gallery) 156