-
Notifications
You must be signed in to change notification settings - Fork 1
/
spotifytrack.py
executable file
·66 lines (55 loc) · 2.06 KB
/
spotifytrack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
# -*- coding: utf-8 -*-
from analyzable import *
import utils
from musicdatatype import MusicDataType
import os
import numpy as np
from echonest import get_echonestsong_by_spotifyid
class SpotifyTrack(Analyzable):
"""
This class represents a SpotifyTrack
Attributes:
id: Spotify id of the track
name: name of the spotify track
preview_url: preview_url of the spotify track
uri: uri of the spotify track
features: :class:`.EchonestFeatures` for the spotify track
"""
def __init__(
self,
id,
name,
preview_url,
uri,artist
):
Analyzable.__init__(self, id)
self.preview_url = preview_url
self.name = name
self.type = 'spotify'
self.uri = uri
self.artist = artist
def get_chromagram(self, mode=MusicDataType.AUDIO):
"""
Get chromagram for the spotify track
Creates chromagram either from mp3(:meth:`utils.get_chromagram_from_audio`) or echonest analysis (:meth:`apiwrappers.echonest.get_echonestsong_by_spotifyid`) of the spotify track.Chromagram may return None if there is no matching echonest song for the spotify track
Args:
mode: :class:`.MusicDataType`. either should be audio or echonest
Returns:
Chromagram representation.
"""
directory = os.path.dirname(os.path.abspath(__file__))
if mode == MusicDataType.AUDIO:
audio_file_path = directory + '/' + self.id + '.mp3'
utils.download_file(self.preview_url, audio_file_path)
chromagram = utils.get_chromagram_from_audio(audio_file_path)
utils.remove_file(audio_file_path)
return chromagram
elif mode == MusicDataType.ECHONEST:
e = get_echonestsong_by_spotifyid(self.id)
if e:
self.echonest_features = e.features
return e.features.chroma
else:
print "No echonest song found for spotify:track:"+self.id
return None