Beispiel #1
0
    def get(self, pk_or_uri):
        """ Returns a single track object by primary key, if the track does
        not exist a 404 will be returned.

        Arguments
        ---------
        pk : str
            The track primary key
        """

        try:
            uuid.UUID(pk_or_uri, version=4)
        except ValueError:
            field = Track.spotify_uri
        else:
            field = Track.id

        track = Track.query.filter(field == pk_or_uri).first()
        if track is None:
            return http.NotFound()

        data = TrackSerializer().serialize(track)
        data['audio_summary'] = track.audio_summary

        return http.OK(data)
Beispiel #2
0
    def get(self):
        """ Renders a HTML test client for testing google OAuth2 Flow
        """
        if current_app.debug is False:
            return http.NotFound()

        return render_template('oauth2/spotify.html')
Beispiel #3
0
    def handle_404(e):
        """ Handle 404 errors.
        """

        response = {'message': 'Not Found'}

        return http.NotFound(response)
Beispiel #4
0
    def get(self, id=None):
        """Returns the artist data from spotify
        """

        if id is None:
            return http.NotFound()

        # TODO: exception handling
        token = get_client_credentials()

        r = requests.get(
            'https://api.spotify.com/v1/artists/{0}'.format(id),
            headers={
                "Authorization": "Bearer {0}".format(token),
            })

        if r.status_code != httplib.OK:
            return http.NotFound()

        return http.OK(r.json())
Beispiel #5
0
    def get(self, id=None):
        """Returns an albums tracks
        """

        if id is None:
            return http.NotFound()

        # TODO: exception handling
        token = get_client_credentials()

        r = requests.get(
            'https://api.spotify.com/v1/albums/{0}/tracks'.format(id),
            headers={
                "Authorization": "Bearer {0}".format(token),
            },
            params=request.args)

        if r.status_code != httplib.OK:
            return http.NotFound()

        return http.OK(r.json())
Beispiel #6
0
    def get(self, pk):
        """ Get a serialized user object.

        Arguments
        ---------
        pk : str
            The user primary key UUID
        """

        try:
            uuid.UUID(pk, version=4)
        except ValueError:
            user = None
        else:
            user = User.query.get(pk)

        if user is None:
            return http.NotFound()

        return http.OK(UserSerializer().serialize(user))
Beispiel #7
0
    def get(self, pk):

        try:
            uuid.UUID(pk, version=4)
        except ValueError:
            user = None
        else:
            user = User.query.get(pk)

        if user is None:
            return http.NotFound()

        since = request.args.get('from', None)
        if since:
            since = pytz.utc.localize(datetime.strptime(since, '%Y-%m-%d'))
        until = request.args.get('to', None)
        if until:
            until = pytz.utc.localize(datetime.strptime(until, '%Y-%m-%d'))

        payload = {
            'most_played_tracks': [{
                'track': FMTrackSerializer().serialize(u),
                'total': t
            } for u, t in self.most_played_tracks(pk, since, until)],
            'most_played_artists': [{
                'artist': ArtistSerializer().serialize(u),
                'total': t
            } for u, t in self.most_played_artists(pk, since, until)],
            'most_played_genres': [{
                'name': u.name,
                'total': t
            } for u, t in self.most_played_genres(pk, since, until)],
            'total_plays':
            self.total_plays(pk, since, until),
            'total_play_time':
            self.total_play_time(pk, since, until),
        }

        return http.OK(payload)