1# Copyright (C) 2006 Adam Olsen
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 1, or (at your option)
6# any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16
17import logging
18import time
19
20from xl import common, covers, event, providers, settings
21
22from . import _ecs as ecs
23from . import amazonprefs
24
25logger = logging.getLogger(__name__)
26
27AMAZON = None
28USER_AGENT = None
29
30
31def enable(exaile):
32    if exaile.loading:
33        event.add_callback(_enable, "exaile_loaded")
34    else:
35        _enable(None, exaile, None)
36
37
38def _enable(eventname, exaile, nothing):
39    global AMAZON, USER_AGENT
40    USER_AGENT = exaile.get_user_agent_string('amazoncovers')
41    AMAZON = AmazonCoverSearch()
42    providers.register('covers', AMAZON)
43
44
45def disable(exaile):
46    providers.unregister('covers', AMAZON)
47
48
49def get_preferences_pane():
50    return amazonprefs
51
52
53class AmazonCoverSearch(covers.CoverSearchMethod):
54    """
55    Searches amazon for an album cover
56    """
57
58    name = 'amazon'
59    title = 'Amazon'
60
61    def __init__(self):
62        self.starttime = 0
63
64    def find_covers(self, track, limit=-1):
65        """
66        Searches amazon for album covers
67        """
68        try:
69            artist = track.get_tag_raw('artist')[0]
70            album = track.get_tag_raw('album')[0]
71        except (AttributeError, TypeError):
72            return []
73
74        # get the settings for amazon key and secret key
75        api_key = settings.get_option('plugin/amazoncovers/api_key', '')
76        secret_key = settings.get_option('plugin/amazoncovers/secret_key', '')
77        if not api_key or not secret_key:
78            logger.warning(
79                'Please enter your Amazon API and secret '
80                'keys in the Amazon Covers preferences'
81            )
82            return []
83
84        # wait at least 1 second until the next attempt
85        waittime = 1 - (time.time() - self.starttime)
86        if waittime > 0:
87            time.sleep(waittime)
88        self.starttime = time.time()
89
90        search = "%s - %s" % (artist, album)
91        try:
92            albums = ecs.search_covers(search, api_key, secret_key, USER_AGENT)
93        except ecs.AmazonSearchError:
94            return []
95        return albums
96
97    def get_cover_data(self, url):
98        return common.get_url_contents(url, USER_AGENT)
99