예제 #1
0
    def populate():
        client = get_spotify()
        errors = []

        tracks = Track.objects.filter(populated=False, spotify_id__isnull=False)[:10]
        for track in tracks:
            try:
                data = client._get("audio-features/" + client._get_id("track", track.spotify_id))
            except Exception as e:
                errors.append(str(e))
                continue

            track.analysis_url = data["analysis_url"]
            track.key = data["key"]
            track.time_signature = data["time_signature"]
            track.danceability = data["danceability"]
            track.energy = data["energy"]
            track.loudness = data["loudness"]
            track.speechiness = data["speechiness"]
            track.acousticness = data["acousticness"]
            track.instrumentalness = data["instrumentalness"]
            track.liveness = data["liveness"]
            track.valence = data["valence"]
            track.tempo = data["tempo"]
            track.duration_ms = data["duration_ms"]
            track.populated = True
            track.save()

            youtube_videos = YouTubeHelper().search("%s - %s" % (track.artists_to_str, track.title))

            for youtube_video in youtube_videos:
                try:
                    # do not load the same video twice
                    Video.objects.get(video_id=youtube_video["id"]["videoId"])
                    continue
                except Video.DoesNotExist:
                    pass
                except Video.MultipleObjectsReturned:
                    continue

                try:
                    video = Video()
                    video.track = track
                    video.source = "youtube"
                    video.description = youtube_video["snippet"]["description"]
                    video.title = youtube_video["snippet"]["title"]
                    video.channel_id = youtube_video["snippet"]["channelId"]
                    video.url = "https://www.youtube.com/watch?v=%s" % youtube_video["id"]["videoId"]
                    video.video_id = youtube_video["id"]["videoId"]
                    video.save()
                except Exception as e:
                    errors.append(str(e))
                    continue

        return [tracks, errors]
예제 #2
0
    def populate(self):
        errors = []
        limit = 10
        last_id = 0
        total = 0

        while True:
            tracks = Track.objects.filter(
                ~Exists(Video.objects.filter(track=OuterRef('pk'))),
                id__gt=last_id).order_by('id')[:limit]

            for track in tracks:
                youtube_videos = self.search(
                    "%s - %s" % (track.artists_to_str, track.title))

                if len(youtube_videos) > 0:
                    for youtube_video in youtube_videos:
                        try:
                            # do not load the same video twice
                            Video.objects.get(
                                video_id=youtube_video["id"]["videoId"])
                            continue
                        except Video.DoesNotExist:
                            pass
                        except Video.MultipleObjectsReturned:
                            continue

                        try:
                            video = Video()
                            video.track = track
                            video.source = "youtube"
                            video.description = youtube_video["snippet"][
                                "description"]
                            video.title = youtube_video["snippet"]["title"]
                            video.channel_id = youtube_video["snippet"][
                                "channelId"]
                            video.url = "https://www.youtube.com/watch?v=%s" % youtube_video[
                                "id"]["videoId"]
                            video.video_id = youtube_video["id"]["videoId"]
                            video.save()
                        except Exception as e:
                            errors.append(str(e))
                            continue
                else:
                    errors.append("No videos for track %s by %s" %
                                  (track.title, track.artists_to_str))

                last_id = track.id
                total += 1

            if 0 == len(tracks):
                break

        return [total, errors]