示例#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]
示例#3
0
def report(report_id):
    if request.method == 'GET':
        if report_id is None:
            reports = Report.query.all()
            reports = [r.to_dict() for r in reports]

            return jsonify({
                'data': reports
            })
        else:
            reports = Report.query.filter_by(id=report_id).first()

            return jsonify({
                'data': reports.to_dict()
            })

    elif request.method == 'POST':
        data = request.json
        print(data)
        valid, missing = Report.validate_json(data)

        if not valid:
            return jsonify({
                'message': '{} not given in request.'.format(', '.join(missing))
            }), 422

        report = Report(data['user_id'],
                        data['report_type'],
                        data['timestamp'],
                        data['location']['longitude'],
                        data['location']['latitude'])
        db.session.add(report)
        db.session.commit()

        return jsonify(report.id)

    elif request.method == 'PUT':
        print(request.headers)

        if 'video' not in request.files:
            return jsonify({'message': 'Request does not have a file'}), 422
        
        file = request.files['video']
        
        if file.filename == '':
            return jsonify({'message': 'Request does not have a file'}), 422
        
        if file and utils.allowed_file(file.filename):
            ext = utils.get_ext(file.filename)
            file_id = uuid.uuid4().int
            filename = str(file_id) + '.' + ext
            abs_fpath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            file.save(abs_fpath)

            rel_path = os.path.join(os.path.basename(app.config['UPLOAD_FOLDER']), filename)

            video = Video(path=rel_path, ext=ext)
            db.session.add(video)
            db.session.commit()

            url = os.path.join('video', str(video.id))
            video.url = url
            db.session.commit()

            report = Report.query.filter_by(id=report_id).first()
            report.video_id = video.id
            db.session.commit()

            if report.report_type == 'hit':
                pipelines.hit(report, abs_fpath)
            elif report.report_type == 'witness':
                pipelines.witness(report, abs_fpath)

            return jsonify({'message': 'Successfully uploaded video.'})

    elif request.method == 'DELETE':
        report = Report.query.filter_by(id=report_id).first()
        db.session.delete(report)
        db.session.commit()
        return jsonify({
            'message': 'Successfully deleted.'
        })