def _update_models(self, track_details, action, user_slack_id): try: user_profile = UserProfile.objects.get( slack_username=user_slack_id) except UserProfile.DoesNotExist: return None try: track = Track.objects.get(spotify_id=track_details['uri']) except Track.DoesNotExist: track = Track() track.title = track_details['name'] track.spotify_id = track_details['uri'] track.album = track_details['album']['name'] track.url = track_details[ "preview_url"] if "preview_url" in track_details else "" track.save() for artist_details in track_details["artists"]: try: artist = Artist.objects.get(spotify_id=artist_details["uri"]) except Artist.DoesNotExist: artist = Artist() artist.spotify_id = artist_details["uri"] artist.name = artist_details["name"] artist.save() track.artists.add(artist) track.save() try: playlist = Playlist.objects.get(name="Fusebox") except Playlist.DoesNotExist: playlist = Playlist() playlist.name = "Fusebox" playlist.spotify_id = os.getenv("SPOTIFY_PLAYLIST_ID") playlist.save() try: playlist_tracks = PlaylistTracks.objects.get(track=track, playlist=playlist) except PlaylistTracks.DoesNotExist: playlist_tracks = PlaylistTracks() playlist_tracks.track = track playlist_tracks.playlist = playlist if "queue" == action: playlist_tracks.queued_by = user_profile.user playlist_tracks.queued_on = timezone.now() elif "dequeue" == action: playlist_tracks.dequeued_by = user_profile.user playlist_tracks.dequeued_on = timezone.now() playlist_tracks.save()
def post_geo(data): try: track = Track.objects.get(member_id=data['member']) Track.objects.filter(member_id=data['member']).update( coordinates=data['coordinates']) return HttpResponse(renderers.JSONRenderer().render( {'coordinates': data['coordinates']})) except Track.DoesNotExist: track = Track(member_id=data['member'], coordinates=data['coordinates']) track.save() return HttpResponse(renderers.JSONRenderer().render( {'coordinates': track.coordinates}))
def current_playing_track(): client = get_spotify() logger = logging.getLogger(__name__) played = None try: track_details = client._get("me/player") except Exception as e: logger.error("Failed to retrieve details about current playing song: " + str(e)) track_details = None if track_details \ and "item" in track_details \ and "uri" in track_details["item"] \ and "artists" in track_details["item"]: try: track = Track.objects.get(spotify_id=track_details["item"]["uri"]) except Track.DoesNotExist: track = Track() track.title = track_details["item"]["name"] track.album = track_details["item"]["album"]["name"] track.spotify_id = track_details["item"]["uri"] track.url = track_details["item"]["preview_url"] track.save() for artist_details in track_details["item"]["artists"]: try: artist = Artist.objects.get(spotify_id=artist_details["uri"]) except Artist.DoesNotExist: artist = Artist() artist.spotify_id = artist_details["uri"] artist.name = artist_details["name"] artist.save() track.artists.add(artist) track.save() # Update played song if it has not played recently time_threshold = timezone.now() - timedelta(minutes=120) try: played = Played.objects.filter(track=track, on__gt=time_threshold)[:1].get() except Played.DoesNotExist: played = Played() played.on = timezone.now() played.track = track played.save() else: track = None return track, track_details, played
def del_all_tracks(request): try: token = request.META["HTTP_AUTHENTICATION"] received_json_data = json.loads(request.body) owner = User.objects.get(spotifyId=received_json_data['userId']) party = Party.objects.get(id=received_json_data['partyId']) except ValueError: return HttpResponse("Inavlid Json", status=400) if owner.is_authenticated(token): return HttpResponse("No authenticated", status=403) Track.del_all_tracks(party, owner) return return_all_tracks(party, owner)
def set_tracks(request): try: token = request.META["HTTP_AUTHENTICATION"] received_json_data = json.loads(request.body) received_json_tracks = received_json_data['tracks'] owner = User.objects.get(spotifyId=received_json_data['userId']) party = Party.objects.get(id=received_json_data['partyId']) except ValueError: return HttpResponse("Inavlid Json", status=400) if owner.is_authenticated(token): return HttpResponse("No authenticated", status=403) for _track in received_json_tracks: try: "If track exist update priority" track = Track.objects.get(id=_track["id"]) track.priority = _track["priority"] track.save() except KeyError: "If not exist create a new track" track = Track.create_track(owner, party, _track["spotify_track_id"], _track["name"], _track["duration_ms"], _track["explicit"], _track["preview_url"], _track["href"], _track["popularity"], _track["uri"], _track["priority"]) track.save() return return_all_tracks(party, owner)
def edit_by_key(self, request): current_user = endpoints.get_current_user() nick = (current_user.nickname() if current_user is not None else 'anonymous') url = self.create_wave(request.music_string, nick) if current_user: t = Track.get_by_id( request.m_id, parent=user_key(current_user.nickname()) ) self.validate_existance(request.name, "Insert a name") self.validate_existance(t, "Not found id: {}".format(request.m_id)) delete_file(t.music_filename) t.name = request.name t.music_string = request.music_string t.music_filename = url t.put() return OutputMelody( m_id=request.m_id, name=request.name, music_string=request.music_string, path=url, )
def upsert_track(path, album): audio = MP3(os.path.join(root, path), ID3=EasyID3) title = audio['title'] artist = audio['artist'] track_num = audio['tracknumber'][0] duration = audio.info.length t = Track( title=title, artist=artist, album=album, track_number=track_num, duration=duration, ) try: t.save() except (django.db.utils.IntegrityError, ValueError): pass
def create_music(self, request): current_user = endpoints.get_current_user() nick = (current_user.nickname() if current_user is not None else 'anonymous') self.validate_existance(request.name, "Insert a name") url = self.create_wave(request.music_string, nick) m_id = 0 if current_user: new_track = Track(parent=user_key(current_user.nickname())) new_track.author = current_user new_track.name = request.name new_track.music_string = request.music_string.upper() new_track.music_filename = url key = new_track.put() m_id = key.id() return OutputMelody( m_id=m_id, name=request.name, music_string=request.music_string, path=url, )
def remove_by_key(self, request): current_user = endpoints.get_current_user() if current_user: t = Track.get_by_id( request.m_id, parent=user_key(current_user.nickname()) ) self.validate_existance(t, "Not found id: {}".format(request.m_id)) delete_file(t.music_filename) t.delete() return message_types.VoidMessage()
def add_track(request): try: token = request.META["HTTP_AUTHORIZATION"] received_json_data = json.loads(request.body) received_json_tracks = received_json_data['tracks'] owner = User.objects.get(spotify_id=received_json_data['userId']) party = Party.objects.get(id=received_json_data['partyId']) except ValueError: return HttpResponse("Inavlid Json", status=400) if not owner.is_authenticated(token): return HttpResponse("No authenticated", status=403) for _track in received_json_tracks: "Create a new track" track = Track.create_track(owner, party, _track["spotify_track_id"], _track["name"], _track["duration_ms"], _track["explicit"], _track["preview_url"], _track["href"], _track["popularity"], _track["uri"], Track.get_last_priority(party, owner) + 1, _track["artist_name"]) track.save() return return_all_tracks(party, owner)
def get_all_music(self, request): current_user = endpoints.get_current_user() tracks = Track.all().filter("author =", current_user) items = [] for t in tracks: items.append(OutputMelody( m_id=t.key().id(), name=t.name, music_string=t.music_string, path=t.music_filename )) return OutputMelodyCollection(items=items)
def return_all_tracks(party, owner): tracks = Track.get_all_tracks(party, owner) serializer = TrackSerializer(tracks, many=True) return JSONResponse(serializer.data, status=200)