首页 > 解决方案 > os.path.expanduser("~") 不在heroku上工作

问题描述

我在 django 中遇到 youtube_dl 路径配置问题。基于以下配置,它在开发模式下可以正常工作,但是当托管在 heroku 上时,无法在任何地方跟踪下载的资源。

# Download view
        
def download(request):
    details = []
    if request.method == 'POST':
        v_url = CreateURL(request.POST)
        if v_url.is_valid():
            video_url = v_url.cleaned_data['url']

            #path configured
            homedir = os.path.expanduser("~/Downloads/%(title)s.%(ext)s")
            try:
                ydl = youtube_dl.YoutubeDL( {
                    '-f best': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
                    '-o': f'{homedir}',
               
                    # 'progress_hooks': [my_hook],
                    })
            
                with ydl:
                    result = ydl.extract_info(
                    video_url,
                    download=True # We just want to extract the info
                    )
                
                    if 'entries' in result:
                        # Can be a playlist or a list of videos
                        video = result['entries'][0]
                    else:
                   ` # Just a video`
                        video = result
                        video_data= {
                            'resolution':video['thumbnails'][3]['resolution'],
                            'title':video['title'],
                            'ext':video['ext'], 
                        }
                        details.append(video_data)
           
            except:
                messages.warning(request,"Error in downloading the video, please check your network connection and try again!")
              
            else:
                messages.success(request, 'Downloaded successfully, check your download folder for details!')
                                   
    context={
        'details':details            
        }

    return render(request, 'download.html', context )

任何帮助将不胜感激。谢谢

标签: pythondjango

解决方案


推荐阅读