GitHub PullRequestReviewCommentEvent at 2014-05-28 09:16:16
Repository: picard
Actor: mineo
Payload: {"comment":{"url":"https://api.github.com/repos/musicbrainz/picard/pulls/comments/13121952","id":13121952,"diff_hunk":"@@ -0,0 +1,165 @@\n+# -*- coding: utf-8 -*-\n+#\n+# Picard, the next-generation MusicBrainz tagger\n+# Copyright (C) 2007 Oliver Charles\n+# Copyright (C) 2007-2011 Philipp Wolfer\n+# Copyright (C) 2007, 2010, 2011 Lukáš Lalinský\n+# Copyright (C) 2011 Michael Wiencek\n+# Copyright (C) 2011-2012 Wieland Hoffmann\n+# Copyright (C) 2013-2014 Laurent Monin\n+#\n+# This program is free software; you can redistribute it and/or\n+# modify it under the terms of the GNU General Public License\n+# as published by the Free Software Foundation; either version 2\n+# of the License, or (at your option) any later version.\n+#\n+# This program is distributed in the hope that it will be useful,\n+# but WITHOUT ANY WARRANTY; without even the implied warranty of\n+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+# GNU General Public License for more details.\n+#\n+# You should have received a copy of the GNU General Public License\n+# along with this program; if not, write to the Free Software\n+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n+\n+import json\n+import traceback\n+from picard import config, log\n+from picard.const import CAA_HOST, CAA_PORT\n+from picard.coverartproviders import CoverArtProvider\n+from picard.coverartimage import CaaCoverArtImage\n+\n+\n+_CAA_THUMBNAIL_SIZE_MAP = {\n+    0: \"small\",\n+    1: \"large\",\n+}\n+\n+\n+class CoverArtProviderCaa(CoverArtProvider):\n+\n+    \"\"\"Get cover art from Cover Art Archive using release mbid\"\"\"\n+\n+    NAME = \"Cover Art Archive\"\n+\n+    def __init__(self, coverart):\n+        CoverArtProvider.__init__(self, coverart)\n+        self.caa_types = map(unicode.lower, config.setting[\"caa_image_types\"])\n+        self.len_caa_types = len(self.caa_types)\n+\n+    def enabled(self):\n+        \"\"\"Check if CAA artwork has to be downloaded\"\"\"\n+        if not config.setting['ca_provider_use_caa']:\n+            log.debug(\"Cover Art Archive disabled by user\")\n+            return False\n+        if not self.len_caa_types:\n+            log.debug(\"User disabled all Cover Art Archive types\")\n+            return False\n+\n+        # MB web service indicates if CAA has artwork\n+        # http://tickets.musicbrainz.org/browse/MBS-4536\n+        if 'cover_art_archive' not in self.release.children:\n+            log.debug(\"No Cover Art Archive information for %s\"\n+                      % self.release.id)\n+            return False\n+\n+        caa_node = self.release.children['cover_art_archive'][0]\n+        caa_has_suitable_artwork = caa_node.artwork[0].text == 'true'\n+\n+        if not caa_has_suitable_artwork:\n+            log.debug(\"There are no images in the Cover Art Archive for %s\"\n+                      % self.release.id)\n+            return False\n+\n+        want_front = 'front' in self.caa_types\n+        want_back = 'back' in self.caa_types\n+        caa_has_front = caa_node.front[0].text == 'true'\n+        caa_has_back = caa_node.back[0].text == 'true'\n+\n+        if self.len_caa_types == 2 and (want_front or want_back):\n+            # The OR cases are there to still download and process the CAA\n+            # JSON file if front or back is enabled but not in the CAA and\n+            # another type (that's neither front nor back) is enabled.\n+            # For example, if both front and booklet are enabled and the\n+            # CAA only has booklet images, the front element in the XML\n+            # from the webservice will be false (thus front_in_caa is False\n+            # as well) but it's still necessary to download the booklet\n+            # images by using the fact that back is enabled but there are\n+            # no back images in the CAA.\n+            front_in_caa = caa_has_front or not want_front\n+            back_in_caa = caa_has_back or not want_back\n+            caa_has_suitable_artwork = front_in_caa or back_in_caa\n+\n+        elif self.len_caa_types == 1 and (want_front or want_back):\n+            front_in_caa = caa_has_front and want_front\n+            back_in_caa = caa_has_back and want_back\n+            caa_has_suitable_artwork = front_in_caa or back_in_caa\n+\n+        if not caa_has_suitable_artwork:\n+            log.debug(\"There are no suitable images in the Cover Art Archive for %s\"\n+                      % self.release.id)\n+        else:\n+            log.debug(\"There are suitable images in the Cover Art Archive for %s\"\n+                      % self.release.id)\n+\n+        return caa_has_suitable_artwork\n+\n+    def queue_downloads(self):\n+        self.xmlws_download(\n+            CAA_HOST,\n+            CAA_PORT,\n+            \"/release/%s/\" % self.metadata[\"musicbrainz_albumid\"],\n+            self._caa_json_downloaded,\n+            priority=True,\n+            important=False\n+        )\n+        # we will call next_in_queue() after json parsing\n+        return CoverArtProvider.WAIT\n+\n+    def _caa_json_downloaded(self, data, http, error):\n+        \"\"\"Parse CAA JSON file and queue CAA cover art images for download\"\"\"\n+        self.requests_count_decrement()\n+        caa_front_found = False\n+        if error:\n+            self.error(u'CAA JSON error: %s' % (unicode(http.errorString())))\n+        else:\n+            try:\n+                caa_data = json.loads(data)\n+            except ValueError:\n+                self.error(\"Invalid JSON: %s\", http.url().toString())\n+            else:\n+                for image in caa_data[\"images\"]:\n+                    if config.setting[\"caa_approved_only\"] and not image[\"approved\"]:\n+                        continue\n+                    # if image has no type set, we still want it to match\n+                    # pseudo type 'unknown'\n+                    if not image[\"types\"]:\n+                        image[\"types\"] = [u\"unknown\"]\n+                    else:\n+                        image[\"types\"] = map(unicode.lower, image[\"types\"])\n+                    # only keep enabled caa types\n+                    types = set(image[\"types\"]).intersection(\n+                        set(self.caa_types))\n+                    if types:\n+                        if not caa_front_found:\n+                            caa_front_found = u'front' in types","path":"picard/coverartproviders/caa.py","position":145,"original_position":145,"commit_id":"b4b1619187cefa29cfedec1fa9e489b4286b5c28","original_commit_id":"b4b1619187cefa29cfedec1fa9e489b4286b5c28","user":{"login":"mineo","id":78236,"avatar_url":"https://avatars.githubusercontent.com/u/78236?","gravatar_id":null,"url":"https://api.github.com/users/mineo","html_url":"https://github.com/mineo","followers_url":"https://api.github.com/users/mineo/followers","following_url":"https://api.github.com/users/mineo/following{/other_user}","gists_url":"https://api.github.com/users/mineo/gists{/gist_id}","starred_url":"https://api.github.com/users/mineo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mineo/subscriptions","organizations_url":"https://api.github.com/users/mineo/orgs","repos_url":"https://api.github.com/users/mineo/repos","events_url":"https://api.github.com/users/mineo/events{/privacy}","received_events_url":"https://api.github.com/users/mineo/received_events","type":"User","site_admin":false},"body":"This seems to be a write-only variable now.","created_at":"2014-05-28T09:16:16Z","updated_at":"2014-05-28T09:16:16Z","html_url":"https://github.com/musicbrainz/picard/pull/336#discussion_r13121952","pull_request_url":"https://api.github.com/repos/musicbrainz/picard/pulls/336","_links":{"self":{"href":"https://api.github.com/repos/musicbrainz/picard/pulls/comments/13121952"},"html":{"href":"https://github.com/musicbrainz/picard/pull/336#discussion_r13121952"},"pull_request":{"href":"https://api.github.com/repos/musicbrainz/picard/pulls/336"}}}}