Esempio n. 1
0
def index():
    form = SearchForm()
    if form.validate_on_submit():
        video_url = form.q.data

        # first check if the video is allready in database
        video = Video.query.filter_by(
            youtube_id=Video.get_youtube_id(video_url), search=True).first()
        if video is None:
            try:
                yt = YouTube(video_url).streams.filter(progressive=True).last()
                yt.download()

            except Exception as e:
                print(e, video_url)
                flash('Cannot download video. Please choose another.')

            else:
                video = Video(url=video_url, search=True)
                db.session.add(video)
                db.session.commit()

                # process video, extract features
                proc = Processor()
                proc.process_file(yt.default_filename, video.id)
                os.remove(yt.default_filename)

                pred = Predictor()
                pred.save_encodings(video.id)

        if video is not None:
            return redirect(
                url_for('search',
                        video_id=video.id,
                        ft=form.features.data,
                        distance=form.distance.data))

    searches = Video.query.filter_by(search=True).order_by(
        Video.id.desc()).limit(4).all()

    return render_template('index.html',
                           title='Home',
                           form=form,
                           searches=searches)