diff options
author | GamaC0de <nitzo2001@yahoo.com> | 2016-04-20 22:21:50 +0200 |
---|---|---|
committer | GamaC0de <nitzo2001@yahoo.com> | 2016-04-20 22:21:50 +0200 |
commit | c846b70db53242f355ebe35c26c28dffea61de36 (patch) | |
tree | d914045811b3c5b39cbbaf2d7369af748323ecf4 | |
parent | Merge pull request #2432 from Nippey/patch-2 (diff) | |
download | pyload-c846b70db53242f355ebe35c26c28dffea61de36.tar.xz |
[ImgurCom] Code cleanup
-rw-r--r-- | module/plugins/crypter/ImgurCom.py | 76 |
1 files changed, 43 insertions, 33 deletions
diff --git a/module/plugins/crypter/ImgurCom.py b/module/plugins/crypter/ImgurCom.py index 8b224cdaa..e30b47a8b 100644 --- a/module/plugins/crypter/ImgurCom.py +++ b/module/plugins/crypter/ImgurCom.py @@ -8,7 +8,7 @@ from module.plugins.internal.misc import uniqify class ImgurCom(SimpleCrypter): __name__ = "ImgurCom" __type__ = "crypter" - __version__ = "0.56" + __version__ = "0.57" __status__ = "testing" __pattern__ = r'https?://(?:www\.|m\.)?imgur\.com/(a|gallery|)/?\w{5,7}' @@ -19,7 +19,7 @@ class ImgurCom(SimpleCrypter): __description__ = """Imgur.com decrypter plugin""" __license__ = "GPLv3" - __authors__ = [("nath_schwarz", "nathan.notwhite@gmail.com"), + __authors__ = [("nath_schwarz", "nathan.notwhite@gmail.com" ), ("nippey", "matthias.nippert@gmail.com")] @@ -27,13 +27,15 @@ class ImgurCom(SimpleCrypter): LINK_PATTERN = r'i\.imgur\.com/\w{7}s?\.(?:jpeg|jpg|png|gif|apng)' """ Imgur may only show the first 10 images of a gallery. The remaining bits may be found here: """ - GALLERY_JSON = "http://imgur.com/ajaxalbums/getimages/{0}/hit.json?all=true" + GALLERY_JSON = "http://imgur.com/ajaxalbums/getimages/%s/hit.json?all=true" + def sanitize(self,name): """ Turn Imgur Gallery title into a safe Package and Folder name """ keepcharacters = (' ','\t','.','_') replacecharacters = (' ','\t') - return "".join(c if c not in replacecharacters else '_' for c in name.strip() if c.isalnum() or c in keepcharacters).strip('_') + return "".join(c if c not in replacecharacters else '_' for c in name.strip() if c.isalnum() or c in keepcharacters).strip('_') + def get_ids_from_json(self): """ Check the embedded JSON and if needed the external JSON for more images """ @@ -41,32 +43,35 @@ class ImgurCom(SimpleCrypter): #Greedy re should match the closing bracket of json assuming JSON data is placed on a single line m = re.search(r"\simage\s+:\s+({.*})", self.data) if m: - json_all_imgs = json.loads(m.group(1)) + embedded_json = json.loads(m.group(1)) #Extract some metadata (ID, Title, NumImages) - self.gallery_id = json_all_imgs['hash'] - self.gallery_name = self.sanitize( "{0}_{1}".format( json_all_imgs['hash'], json_all_imgs['title'] ) ) - self.gallery_numImgs = int( json_all_imgs['num_images'] ) + gallery_id = embedded_json['hash'] + self.gallery_name = self.sanitize(_("%s_%s") % (gallery_id, embedded_json['title'])) + self.total_num_images = int(embedded_json['num_images']) #Extract images - all_imgs = {e['hash']: e['ext'] for e in json_all_imgs['album_images']['images']} - self.log_debug('Found {0} of {1} expected links in embedded JSON'.format(len(all_imgs), self.gallery_numImgs) ) + images = {e['hash']: e['ext'] for e in embedded_json['album_images']['images']} + + self.log_debug(_("Found %s of %s expected links in embedded JSON") % (len(images), self.total_num_images)) #Depeding on the gallery, the embedded JSON may not contain all image information, then we also try the external JSON #If this doesn't help either (which is possible),... TODO: Find out what to do - if self.gallery_numImgs > len(all_imgs): - json_data = self.load(self.GALLERY_JSON.format(self.gallery_id)) - json_all_imgs = json.loads(json_data) + if len(images) < self.total_num_images: + external_json = json.loads(self.load(self.GALLERY_JSON % gallery_id)) + try: - all_imgs = {e['hash']: e['ext'] for e in json_all_imgs['data']['images']} - self.log_debug('Found {0} of {1} expected links in external JSON'.format(len(all_imgs), self.gallery_numImgs) ) + images = {e['hash']: e['ext'] for e in external_json['data']['images']} + self.log_debug(_("Found %s of %s expected links in external JSON") % (len(images), self.total_num_images)) + except (KeyError, TypeError): - self.log_debug('Could not extract links from external JSON') + self.log_debug(_("Could not extract links from external JSON")) #It is possible that the returned JSON contains an empty 'data' section. We ignore it then. - pass - return all_imgs - self.log_debug('Could not find embedded JSON') + return images + + self.log_debug(_("Could not find embedded JSON")) + return {} @@ -81,11 +86,16 @@ class ImgurCom(SimpleCrypter): ids_indirect = [id for id in ids_json.keys() if id not in ids_direct] #No additional images found - if 0 == len(ids_indirect): + if len(ids_indirect) == 0 : return [] #Translate new IDs to Direct-URLs - return map(lambda id: "http://i.imgur.com/{0}{1}".format( id, ids_json[id]), ids_indirect) + return map(lambda id: "http://i.imgur.com/%s%s" % (id, ids_json[id]), ids_indirect) + + + def setup(self): + self.gallery_name = None + self.total_num_images = 0 def get_links(self): @@ -93,27 +103,27 @@ class ImgurCom(SimpleCrypter): f = lambda url: "http://" + re.sub(r'(\w{7})s\.', r'\1.', url) direct_links = uniqify(map(f, re.findall(self.LINK_PATTERN, self.data))) - self.gallery_foundImgs = len(direct_links) - + #Imgur Galleryies may contain more images than initially shown. Find the rest now! - self.gallery_name = None - self.gallery_numImgs = 0 try: indirect_links = self.get_indirect_links(direct_links) - self.log_debug('Found {0} additional links'.format(len(indirect_links)) ) + self.log_debug(_("Found %s additional links") % (len(indirect_links))) + except (TypeError, KeyError, ValueError) as e: #Fail gracefull as we already had some success - self.log_error('Processing of additional links unsuccessful - {0}: {1}'.format(type(e).__name__, str(e))) + self.log_error(_("Processing of additional links unsuccessful - %s: %s") % (type(e).__name__, str(e))) indirect_links = [] - self.gallery_foundImgs += len(indirect_links) - + + #Check if all images were found and inform the user - if self.gallery_numImgs > self.gallery_foundImgs: - self.log_error('Could not save all images of this gallery: {0}/{1}'.format( self.gallery_foundImgs, self.gallery_numImgs) ) + num_images_found = len(direct_links) + len(indirect_links) + if num_images_found < self.total_num_images: + self.log_error(_("Could not save all images of this gallery: %s/%s") % (num_images_found, self.total_num_images)) #If we could extract a name, use this to create a specific package if self.gallery_name: - self.packages.append( (self.gallery_name, direct_links + indirect_links, self.gallery_name) ) + self.packages.append((self.gallery_name, direct_links + indirect_links, self.gallery_name)) return [] - return direct_links + indirect_links + else: + return direct_links + indirect_links |