Created
November 15, 2013 18:45
-
-
Save eren/7489503 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #-*- encoding: utf8 -*- | |
| # | |
| # Copyright (C) 2013 Eren Türkay <[email protected]> | |
| # 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 2 | |
| # 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, write to the Free Software | |
| # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
| # 02110-1301, USA. | |
| # Spotify module for getting spotify tracks | |
| # | |
| # Got from https://github.com/myano/jenni and ported to new willie | |
| # command syntax | |
| import httplib | |
| import json | |
| from willie.module import rule | |
| class NotModifiedError(Exception): | |
| def __init__(self): | |
| super(NotModifiedError, self).__init__( | |
| "The data hasn't changed since your last request.") | |
| class ForbiddenError(Exception): | |
| def __init__(self): | |
| super(ForbiddenError, self).__init__( | |
| "The rate-limiting has kicked in. Please try again later.") | |
| class NotFoundException(LookupError): | |
| def __init__(self): | |
| super(NotFoundException, self).__init__( | |
| "Could not find that Spotify URI.") | |
| class BadRequestException(LookupError): | |
| def __init__(self): | |
| super(BadRequestException, self).__init__( | |
| "The request was not understood.") | |
| class InternalServerError(Exception): | |
| def __init__(self): | |
| super(InternalServerError, self).__init__( | |
| "The server encounted an unexpected problem.") | |
| class ServiceUnavailable(Exception): | |
| def __init__(self): | |
| super(ServiceUnavailable, self).__init__( | |
| "The API is temporarily unavailable.") | |
| SpotifyStatusCodes = { | |
| 304: NotModifiedError, | |
| 400: BadRequestException, | |
| 403: ForbiddenError, | |
| 404: NotFoundException, | |
| 500: InternalServerError, | |
| 503: ServiceUnavailable | |
| } | |
| class Spotify: | |
| base_url = "ws.spotify.com" | |
| service_url = '/lookup/1/.json' | |
| def __init__(self): | |
| self.conn = httplib.HTTPConnection(self.base_url) | |
| def __del__(self): | |
| self.conn.close() | |
| def lookup(self, uri, extras=None): | |
| lookup_url = "%s?uri=%s" % (self.service_url, uri) | |
| if extras is not None: | |
| lookup_url += "&extras=%s" % extras | |
| self.conn.request("GET", lookup_url) | |
| resp = self.conn.getresponse() | |
| if resp.status == 200: | |
| result = json.loads(resp.read()) | |
| return result | |
| try: | |
| raise SpotifyStatusCodes[resp.status] | |
| except ValueError: | |
| raise Exception("Unknown response from the Spotify API") | |
| def print_track(bot, track): | |
| bot.say('%s by %s (%s, %s)' % \ | |
| (track['name'], track['artists'][0]['name'], \ | |
| track['album']['name'], track['album']['released'])) | |
| def print_album(bot, album): | |
| bot.say('%s. Released in %s by %s' % \ | |
| (album['name'], album['released'], album['artist'])) | |
| def print_artist(bot, artist): | |
| bot.say('[Spotify Artist] %s' % artist['name']) | |
| # @rule('.*(spotify:(album|artist|track|user:[^:]+:playlist):([a-zA-Z0-9]+)).*') | |
| @rule('.*(spotify:(album|artist|track):([a-zA-Z0-9]+)).*') | |
| def query(bot, trigger, found_match=None): | |
| match = found_match or trigger | |
| spotify = Spotify() | |
| spotify_uri = match.group(1) | |
| result = None | |
| if trigger.nick == 'mengu': | |
| return | |
| try: | |
| result = spotify.lookup(spotify_uri) | |
| except Exception, e: | |
| # bot.say("ERROR: %s" % e) | |
| return | |
| formatters = { | |
| 'track': print_track, | |
| 'album': print_album, | |
| 'artist': print_artist | |
| } | |
| try: | |
| result_type = result['info']['type'] | |
| formatters[result_type](bot, result[result_type]) | |
| except KeyError: | |
| bot.say("ERROR: Unknown response from Spotify Server") | |
| return | |
| if __name__ == '__main__': | |
| s = Spotify() | |
| ret = s.lookup('spotify:track:4DpxW3ejhqmoU8FQ1o5JGL') | |
| print ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment