django服务器上的youtube-dl

时间:2015-03-27 10:57:23

标签: python django heroku youtube-dl

我在heroku上有一个python / django服务器,这是我的索引方法:

def index(request):
    youtube_dl.main(['https://www.youtube.com/watch?v=ufERJEdcfAY'])
    return HttpResponse('hello')

当我进入我的主页时,响应是预期的'你好',但我在服务器的任何地方都找不到媒体文件

我还有另一个主要脚本:

if __name__ == '__main__':
    youtube_dl.main(['https://www.youtube.com/watch?v=ufERJEdcfAY'])

当我正在运行python __main__.py时,一切正常,文件将下载到我运行命令的文件夹中。

任何想法都会受到高度欢迎,问候!

修改

我尝试了踩踏,似乎它正在工作,但我找不到文件。 我跑heroku run bash试图找到文件所在的目录,但不能。

 ←[32m2015-03-27T11:53:42.068905+00:00 heroku[api]:←[0m Deploy a5bebbc by qobyyy@
gmail.com
←[32m2015-03-27T11:53:42.068905+00:00 heroku[api]:←[0m Release v44 created by qo
byyy@gmail.com
←[36m2015-03-27T11:53:42.880957+00:00 heroku[web.1]:←[0m State changed from up t
o starting
←[36m2015-03-27T11:53:45.526784+00:00 heroku[web.1]:←[0m Stopping all processes
with SIGTERM
←[36m2015-03-27T11:53:46.281281+00:00 app[web.1]:←[0m 2015-03-27 11:53:46 [3] [I
NFO] Shutting down: Master
←[36m2015-03-27T11:53:46.262444+00:00 app[web.1]:←[0m 2015-03-27 11:53:46 [10] [
INFO] Worker exiting (pid: 10)
←[36m2015-03-27T11:53:46.262449+00:00 app[web.1]:←[0m 2015-03-27 11:53:46 [9] [I
NFO] Worker exiting (pid: 9)
←[36m2015-03-27T11:53:46.263200+00:00 app[web.1]:←[0m 2015-03-27 11:53:46 [3] [I
NFO] Handling signal: term
←[36m2015-03-27T11:53:46.868377+00:00 heroku[web.1]:←[0m Starting process with c
ommand `gunicorn gettingstarted.wsgi --log-file -`
←[36m2015-03-27T11:53:47.009560+00:00 heroku[web.1]:←[0m Process exited with sta
tus 0
←[36m2015-03-27T11:53:48.660418+00:00 app[web.1]:←[0m 2015-03-27 11:53:48 [3] [I
NFO] Listening at: http://0.0.0.0:52428 (3)
←[36m2015-03-27T11:53:48.660502+00:00 app[web.1]:←[0m 2015-03-27 11:53:48 [3] [I
NFO] Using worker: sync
←[36m2015-03-27T11:53:48.669618+00:00 app[web.1]:←[0m 2015-03-27 11:53:48 [9] [I
NFO] Booting worker with pid: 9
←[36m2015-03-27T11:53:48.659633+00:00 app[web.1]:←[0m 2015-03-27 11:53:48 [3] [I
NFO] Starting gunicorn 19.0.0
←[36m2015-03-27T11:53:48.711500+00:00 app[web.1]:←[0m 2015-03-27 11:53:48 [10] [
INFO] Booting worker with pid: 10
←[36m2015-03-27T11:53:49.101270+00:00 heroku[web.1]:←[0m State changed from star
ting to up
←[33m2015-03-27T11:53:50.392891+00:00 heroku[router]:←[0m at=info method=GET pat
h="/" host=rocky-castle-8297.herokuapp.com request_id=0ad1d01d-174e-439c-b91d-2c
4487920d97 fwd="85.250.225.100" dyno=web.1 connect=1ms service=785ms status=200
bytes=213
←[36m2015-03-27T11:53:50.793930+00:00 app[web.1]:←[0m [youtube] ufERJEdcfAY: Dow
nloading webpage
←[36m2015-03-27T11:53:51.015179+00:00 app[web.1]:←[0m [youtube] ufERJEdcfAY: Ext
racting video information
←[36m2015-03-27T11:53:51.068767+00:00 app[web.1]:←[0m [youtube] ufERJEdcfAY: Dow
nloading js player en_US-vflFAPa9H
←[36m2015-03-27T11:53:51.349748+00:00 app[web.1]:←[0m [youtube] ufERJEdcfAY: Dow
nloading DASH manifest
←[36m2015-03-27T11:53:51.477715+00:00 app[web.1]:←[0m [download] Destination: Pi
xies  - - Where Is My Mind-ufERJEdcfAY.mp4
[download] 100% of 8.70MiB in 00:0059MiB/s ETA 00:00known ETA

3 个答案:

答案 0 :(得分:2)

查看docs以在python代码中实现youtube-dl。对我来说,调用命令行工具的主要部分似乎是一个坏主意。

from __future__ import unicode_literals
import youtube_dl

ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])

答案 1 :(得分:1)

您可以尝试将此作为thread,以避免任何超时错误。

def youtube(id):
    youtube_dl.main([id])

import threading
def index(request):
    t = threading.Thread(target=youtube, args=("ufERJEdcfAY",))
    t.start()
    return HttpResponse('hello')

答案 2 :(得分:0)

根据这个:https://devcenter.heroku.com/articles/read-only-filesystem

无法从代码中永久保存文件。 它会在请求期间保存,然后删除。

我的解决方案是将它上传到dropbox(这里我使用了线程,非常感谢@itzmeontv)和他们的Python SDK,然后在dropbox中共享该文件的链接。