Exemplo n.º 1
0
def download_yt():
    repair_tags = False
    dir = f'app{os.path.sep}tasks{os.path.sep}temp{os.path.sep}'
    if request.method == "GET":
        return redirect(url_for('main.index', active_slide=1))
    if not current_user.is_authenticated:
        youtube_form = DownloadForm()
        if youtube_form.validate_on_submit():
            t = Task.query.filter_by(user_ip=str(request.remote_addr), status_code=0).first()
            if t:
                flash('Please wait while your other downloading is complete.', 'yt')
                return redirect(url_for('main.index', active_slide=1))
            t = Task(description="Downloading mp3", user_ip=str(request.remote_addr), status_code=0, progress='Waiting')
            quality = 192
            dir += str(request.remote_addr)
            print(f'Loading from youtube user ip:{request.remote_addr} data:{youtube_form.link.data}')
        else:
            flash(youtube_form.link.errors[0], 'yt')
            return redirect(url_for('main.index', active_slide=1))

    else:
        youtube_form = DownloadForm2()
        if youtube_form.validate_on_submit():
            t = Task.query.filter_by(user_id=current_user.id, status_code=0).first()
            if t:
                flash('Please wait while your other downloading is complete.', 'yt')
                return redirect(url_for('main.index', active_slide=1))
            t = Task(description="Downloading mp3", user_id=current_user.id, status_code=0, progress='Waiting')
            quality = get_quality(youtube_form.quality.data)
            repair_tags = youtube_form.repair_tags.data
            dir += str(current_user.id)
            print(f'Loading from youtube user id:{current_user.id} data:{youtube_form.link.data}')
        else:
            flash(youtube_form.link.errors[0], 'yt')
            return redirect(url_for('main.index', active_slide=1))

    db.session.add(t)
    db.session.commit()
    try:
        if youtube_form.link.data.startswith('https://www.youtube.com/watch?v='):
            file = download(youtube_form.link.data[:43], task=t, quality=quality, repair_tags=repair_tags, dir=dir)
        else:
            file = download(youtube_form.link.data[:28], task=t, quality=quality, repair_tags=repair_tags, dir=dir)
    except DownloadError as err:
        t.force_stop('Download error')
        raise err
    return get_download_response(file, t)
Exemplo n.º 2
0
def test_download_from_yt(client):
    try:
        os.mkdir(TEMP_DIR)
    except FileExistsError:
        pass
    filename = download('https://www.youtube.com/watch?v=rTyKk53Wq3w',
                        dir=TEMP_DIR)
    filename = os.path.dirname(filename) + os.path.sep + \
               '.'.join(filename.split(os.sep)[-1].split('.')[:-1]) + '.mp3'
    assert os.path.isfile(filename) is True
Exemplo n.º 3
0
def test_download_from_sc(client):
    try:
        os.mkdir(TEMP_DIR)
    except FileExistsError:
        pass
    filename = download(
        'https://soundcloud.com/elinacooper/'
        'bring-me-the-horizon-nihilist-bluescover-by-the-veer-union',
        dir=TEMP_DIR)
    filename = os.path.dirname(filename) + os.path.sep + \
               '.'.join(filename.split(os.sep)[-1].split('.')[:-1]) + '.mp3'
    assert os.path.isfile(filename) is True
Exemplo n.º 4
0
def test_insert_tags(client):
    try:
        os.mkdir(TEMP_DIR)
    except FileExistsError:
        pass
    filename = download('https://www.youtube.com/watch?v=DpSARXWyCO4',
                        dir=TEMP_DIR)
    filepath = os.path.dirname(filename) + os.path.sep + \
               '.'.join(filename.split(os.sep)[-1].split('.')[:-1]) + '.mp3'
    assert os.path.isfile(filepath) is True
    tags = get_repaired_tags_for_yt('DpSARXWyCO4')
    insert_tags(filepath, tags)
    file = ID3(filepath)
    assert file.getall('APIC')[0] == APIC(
        3, 'image/jpeg', 3, 'Front cover',
        get_image_bytes_from_url(tags['image']))