1
2
3
4 """
5 Copyright (c) 2010 The Echo Nest. All rights reserved.
6 Created by Tyler Williams on 2010-04-25.
7 """
8 import util
9
11 - def __init__(self, li, start=0, total=0):
12 self.extend(li)
13 self.start = start
14 if total == 0:
15 total = len(li)
16 self.total = total
17
21
23 result = util.callm("%s/%s" % (self._object_type, method_name), kwargs)
24 return result['response']
25
26 - def post_attribute(self, method_name, **kwargs):
27 data = kwargs.pop('data') if 'data' in kwargs else {}
28 result = util.callm("%s/%s" % (self._object_type, method_name), kwargs, POST=True, data=data)
29 return result['response']
30
31
33 - def __init__(self, identifier, buckets = None, **kwargs):
34 super(ArtistProxy, self).__init__()
35 buckets = buckets or []
36 self.id = identifier
37 self._object_type = 'artist'
38 kwargs = dict((str(k), v) for (k,v) in kwargs.iteritems())
39
40 core_attrs = ['name']
41
42 if not all(ca in kwargs for ca in core_attrs):
43 profile = self.get_attribute('profile', **{'bucket':buckets})
44 kwargs.update(profile.get('artist'))
45 [self.__dict__.update({ca:kwargs.pop(ca)}) for ca in core_attrs+['id'] if ca in kwargs]
46 self.cache.update(kwargs)
47
54
55
57 - def __init__(self, identifier, type, buckets = None, **kwargs):
58 super(CatalogProxy, self).__init__()
59 buckets = buckets or []
60 self.id = identifier
61 self._object_type = 'catalog'
62 kwargs = dict((str(k), v) for (k,v) in kwargs.iteritems())
63
64 core_attrs = ['name']
65 if not all(ca in kwargs for ca in core_attrs):
66 if util.short_regex.match(self.id) or util.long_regex.match(self.id) or util.foreign_regex.match(self.id):
67 try:
68 profile = self.get_attribute('profile')
69 kwargs.update(profile['catalog'])
70 except util.EchoNestAPIError:
71 raise Exception('Catalog %s does not exist' % (identifier))
72 else:
73 if not type:
74 raise Exception('You must specify a "type"!')
75 try:
76 profile = self.get_attribute('profile')
77 existing_type = profile['catalog'].get('type', 'Unknown')
78 if type != existing_type:
79 raise Exception("Catalog type requested (%s) does not match existing catalog type (%s)" % (type, existing_type))
80
81 kwargs.update(profile['catalog'])
82 except util.EchoNestAPIError:
83 profile = self.post_attribute('create', type=type, **kwargs)
84 kwargs.update(profile)
85 [self.__dict__.update({ca:kwargs.pop(ca)}) for ca in core_attrs+['id'] if ca in kwargs]
86 self.cache.update(kwargs)
87
91
98
99 - def post_attribute(self, *args, **kwargs):
100 if util.short_regex.match(self.id) or util.long_regex.match(self.id) or util.foreign_regex.match(self.id):
101 kwargs['id'] = self.id
102 else:
103 kwargs['name'] = self.id
104 return super(CatalogProxy, self).post_attribute(*args, **kwargs)
105
106
108 - def __init__(self, session_id, buckets = None, **kwargs):
109 super(PlaylistProxy, self).__init__()
110 buckets = buckets or []
111 self._object_type = 'playlist'
112 kwargs = dict((str(k), v) for (k,v) in kwargs.iteritems())
113 if session_id:
114 kwargs['session_id'] = session_id
115
116 core_attrs = ['session_id']
117 if not all(ca in kwargs for ca in core_attrs):
118 profile = self.get_attribute('dynamic', **kwargs)
119 kwargs.update(profile)
120 [self.__dict__.update({ca:kwargs.pop(ca)}) for ca in core_attrs if ca in kwargs]
121 if not session_id:
122 self.cache.update(kwargs)
123
126
128 - def __init__(self, session_id = None, buckets = None, **kwargs):
129 super(BetaPlaylistProxy, self).__init__()
130 core_attrs = ['session_id']
131 self._object_type = 'playlist'
132 if session_id:
133 self.session_id=session_id
134 else:
135 buckets = buckets or []
136 kwargs['bucket'] = buckets
137 kwargs = dict((str(k), v) for (k,v) in kwargs.iteritems())
138
139 if not all(ca in kwargs for ca in core_attrs):
140 kwargs = dict((str(k), v) for (k,v) in kwargs.iteritems())
141 profile = self.get_attribute('create', **kwargs)
142 kwargs.update(profile)
143 [self.__dict__.update({ca:kwargs.pop(ca)}) for ca in core_attrs if ca in kwargs]
144 self.cache.update(kwargs)
145
148
150 - def __init__(self, identifier, buckets = None, **kwargs):
151 super(SongProxy, self).__init__()
152 buckets = buckets or []
153 self.id = identifier
154 self._object_type = 'song'
155 kwargs = dict((str(k), v) for (k,v) in kwargs.iteritems())
156
157
158 if kwargs.has_key("track_id"):
159 self.track_id = kwargs["track_id"]
160 if kwargs.has_key("tag"):
161 self.tag = kwargs["tag"]
162 if kwargs.has_key("score"):
163 self.score = kwargs["score"]
164 if kwargs.has_key('audio'):
165 self.audio = kwargs['audio']
166 if kwargs.has_key('release_image'):
167 self.release_image = kwargs['release_image']
168
169
170 core_attrs = ['title', 'artist_name', 'artist_id']
171
172 if not all(ca in kwargs for ca in core_attrs):
173 profile = self.get_attribute('profile', **{'id':self.id, 'bucket':buckets})
174 kwargs.update(profile.get('songs')[0])
175 [self.__dict__.update({ca:kwargs.pop(ca)}) for ca in core_attrs]
176 self.cache.update(kwargs)
177
181
182
184 - def __init__(self, identifier, md5, properties):
185 """
186 You should not call this constructor directly, rather use the convenience functions
187 that are in track.py. For example, call track.track_from_filename
188 Let's always get the bucket `audio_summary`
189 """
190 super(TrackProxy, self).__init__()
191 self.id = identifier
192 self.md5 = md5
193 self._object_type = 'track'
194 self.__dict__.update(properties)
195