def create_track(data): q = data['q'] response = spotify.search(q, type='track', limit=1) result = response["tracks"].get('items') if len(result) == 1: result = result[0] try: playlist_name = "Playlista " + datetime.datetime.now().strftime( "%d.%m.%Y, %H") + ":00" if not (playlist := Playlist.query.filter_by(name=playlist_name).first()): playlist = Playlist(name=playlist_name) db.session.add(playlist) db.session.flush() db.session.commit() if not (track := Track.query.filter_by(spotify_id=result["id"]).first()): new_track_name = ", ".join( [a.get('name') for a in result["artists"]]) + " - " + result["name"] track = Track(name=new_track_name, spotify_id=result["id"]) db.session.add(track) db.session.flush() db.session.commit()
def get_tracks(self, track_ids): def track_fetcher(track_ids): return self._fetch_by_track_ids( track_ids, lambda track_ids: self.client.tracks(track_ids)["tracks"]) tracks = self._fetch_in_batches(track_ids, track_fetcher) return [Track.from_spotify_track(track) for _, track in tracks]
def from_spotify_playlist(spotify_playlist): return Playlist( spotify_playlist['id'], spotify_playlist['name'], spotify_playlist['description'], [ Track.from_spotify_playlist_track(track) for track in spotify_playlist['tracks']['items'] ], )
def _get_playlist_tracks(self, playlist_id): def track_fetcher(offset=0): results = self.client.playlist_tracks(playlist_id, offset=offset) return results['items'], results['total'] playlist_track_metadata = self._fetch_until_all_items_returned( track_fetcher) return [ Track.from_spotify_playlist_track(track) for track in playlist_track_metadata ]
def create_services(): track = Track.get_service_name() session = Session.get_service_name() speaker = Speaker.get_service_name() sponsor = Sponsor.get_service_name() microlocation = Microlocation.get_service_name() get_or_create(Service, name=track) get_or_create(Service, name=session) get_or_create(Service, name=speaker) get_or_create(Service, name=sponsor) get_or_create(Service, name=microlocation)
def create_services(event_id, serial_no=''): """Creates services and associates them with `event_id`. Service names have an optional `serial_no` that can be used to make them unique. """ test_micro = 'TestMicrolocation{}_{}'.format(serial_no, event_id) test_track = 'TestTrack{}_{}'.format(serial_no, event_id) test_session = 'TestSession{}_{}'.format(serial_no, event_id) test_speaker = 'TestSpeaker{}_{}'.format(serial_no, event_id) test_sponsor = 'TestSponsor{}_{}'.format(serial_no, event_id) test_sponsor_type = 'TestSponsorType{}_{}'.format(serial_no, event_id) test_session_type = 'TestSessionType{}_{}'.format(serial_no, event_id) test_social_link = 'TestSocialLink{}_{}'.format(serial_no, event_id) microlocation = Microlocation(name=test_micro, event_id=event_id) track = Track( name=test_track, description='descp', event_id=event_id, color='#caf034' ) session_type = SessionType( name=test_session_type, length=30, event_id=event_id ) session = Session(title=test_session, long_abstract='descp', start_time=datetime(2014, 8, 4, 12, 30, 45), end_time=datetime(2014, 8, 4, 13, 00, 00), event_id=event_id, session_type=session_type) speaker = Speaker(name=test_speaker, email='*****@*****.**', organisation='org', country='japan', event_id=event_id) sponsor = Sponsor(name=test_sponsor, sponsor_type=test_sponsor_type, event_id=event_id, level='level') social_link = SocialLink( name=test_social_link, link='http://fb.com', event_id=event_id ) save_to_db(social_link, 'SocialLink saved') save_to_db(session_type, 'SessionType saved') save_to_db(microlocation, 'Microlocation saved') save_to_db(track, 'Track saved') save_to_db(session, 'Session saved') save_to_db(speaker, 'Speaker saved') save_to_db(sponsor, 'Sponsor saved')
def create_permissions(): ownr = Role.query.filter_by(name=Role.OWNER).first() orgr = Role.query.filter_by(name=Role.ORGANIZER).first() coorgr = Role.query.filter_by(name=Role.COORGANIZER).first() track_orgr = Role.query.filter_by(name=TRACK_ORGANIZER).first() mod = Role.query.filter_by(name=MODERATOR).first() regist = Role.query.filter_by(name=REGISTRAR).first() track = Service.query.filter_by(name=Track.get_service_name()).first() session = Service.query.filter_by(name=Session.get_service_name()).first() speaker = Service.query.filter_by(name=Speaker.get_service_name()).first() sponsor = Service.query.filter_by(name=Sponsor.get_service_name()).first() microlocation = Service.query.filter_by( name=Microlocation.get_service_name()).first() # For ORGANIZER and OWNER # All four permissions set to True services = [track, session, speaker, sponsor, microlocation] for service in services: perm, _ = get_or_create(Permission, role=ownr, service=service) db.session.add(perm) for service in services: perm, _ = get_or_create(Permission, role=orgr, service=service) db.session.add(perm) # For COORGANIZER for service in services: perm, _ = get_or_create(Permission, role=coorgr, service=service) perm.can_create, perm.can_delete = False, False db.session.add(perm) # For TRACK_ORGANIZER for service in services: perm, _ = get_or_create(Permission, role=track_orgr, service=service) if not service == track: perm.can_create, perm.can_update, perm.can_delete = False, False, False db.session.add(perm) # For MODERATOR for service in services: perm, _ = get_or_create(Permission, role=mod, service=service) perm.can_create, perm.can_update, perm.can_delete = False, False, False db.session.add(perm) # For REGISTRAR services = [track, session, speaker, sponsor, microlocation] for service in services: perm, _ = get_or_create(Permission, role=regist, service=service) perm.can_create, perm.can_update, perm.can_delete = False, False, False db.session.add(perm)
def from_spotify_album(spotify_album): return Album( spotify_album['name'], spotify_album['id'], [ Track.from_spotify_album_track(track, spotify_album['id']) for track in spotify_album['tracks']['items'] ], [ Artist.from_spotify_album_artist(artist) for artist in spotify_album['artists'] ], Album._parse_date(spotify_album['release_date']), spotify_album['total_tracks'], popularity=spotify_album['popularity'], )
def get_recommendations_based_on_tracks(self, track_ids, song_attribute_ranges): """ Params: tracks_ids ([str]): max length is 5. song_attribute_ranges (SongAttributeRanges). Returns: ([Track]). """ if len(track_ids) > RECOMMENDATION_SEED_LIMIT: track_ids = track_ids[:RECOMMENDATION_SEED_LIMIT] results = self.client.recommendations( seed_tracks=track_ids, limit=RECOMMENDATIONS_LIMIT, min_danceability=song_attribute_ranges.danceability_range[0], max_danceability=song_attribute_ranges.danceability_range[1], min_energy=song_attribute_ranges.energy_range[0], max_energy=song_attribute_ranges.energy_range[1], min_loudness=song_attribute_ranges.loudness_range[0], max_loudness=song_attribute_ranges.loudness_range[1], min_speechiness=song_attribute_ranges.speechiness_range[0], max_speechiness=song_attribute_ranges.speechiness_range[1], min_acousticness=song_attribute_ranges.acousticness_range[0], max_acousticness=song_attribute_ranges.acousticness_range[1], min_instrumentalness=song_attribute_ranges. instrumentalness_range[0], max_instrumentalness=song_attribute_ranges. instrumentalness_range[1], min_liveness=song_attribute_ranges.liveness_range[0], max_liveness=song_attribute_ranges.liveness_range[1], min_valence=song_attribute_ranges.valence_range[0], max_valence=song_attribute_ranges.valence_range[1], min_tempo=int(song_attribute_ranges.tempo_range[0]), max_tempo=int(song_attribute_ranges.tempo_range[1]), min_duration_ms=int(song_attribute_ranges.duration_ms_range[0]), max_duration_ms=int(song_attribute_ranges.duration_ms_range[1]), min_popularity=song_attribute_ranges.popularity_range[0], max_popularity=song_attribute_ranges.popularity_range[1], ) return [Track.from_spotify_track(track) for track in results['tracks']]
def mock_track(name="", id_="", uri="", album=mock_album(), artists=[mock_artist()], disc_number=1, duration_ms=0, popularity=0, track_number=1, audio_features=None): audio_features = mock_audio_features( ) if audio_features is None else audio_features return Track( name, id_, uri, album.id, artists, disc_number, duration_ms, popularity, track_number, audio_features, )
def get_track(event_id=1): return Track(name="name", event_id=event_id, description="description", color="red")