如何使用youtubedl生成下载链接并作为消息发送?

2024-09-27 00:22:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Django甚至编程都是新手。我想从youtube视频下载一个mp3文件,并将其作为消息(下载链接)发送给用户。我会下载视频然后转换

以下是我迄今为止所做的尝试。在

在视图.py在

import os
import sys
from .task import *
from datetime import datetime
from .models import Downloader
from .forms import DownloadForm
from django.shortcuts import render


def main_page(request):
    if request.method == 'POST':
        form = DownloadForm(request.POST)
        if form.is_valid():
            video_url = form.cleaned_data.get('link')
            email = form.cleaned_data.get('email')
            try:
                convert_load.delay(video_url)
                Downloader.objects.create(url=video_url, email=email)
            except Exception as e:
                exc_type, exc_obj, exc_tb = sys.exc_info()
                fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
                print(datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' Type:{} Filename:{} Line:{}ERROR: {}'.
                      format(exc_type, fname, exc_tb.tb_lineno, e))
        return render(request, 'download_link.html')
    else:
        form = DownloadForm()
        return render(request, 'main_page.html', {'form': form})


def history(request):
    return render(request, 'history.html', {'instance': Downloader.objects.all()})


def download_link(request):
    link_location = 'media/{}'.format('skjdskdj')
    print(link_location, '\n\n\n\n')
    return render(request, 'download_link.html', {'link': link_location})

在任务.py在

^{pr2}$

如果有人能为我的问题提供线索/解决方案,我会很高兴的?在


Tags: fromimportformurldatetimereturnemailrequest

热门问题