0

我在 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
4

3 回答 3

2

查看在您的 python 代码中实现 youtube-dl的文档。调用命令行工具的 main 对我来说似乎是个坏主意。

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'])
于 2015-03-27T11:33:33.400 回答
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')
于 2015-03-27T11:28:12.583 回答
0

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

从代码中永久保存文件是不可能的。它在请求期间被保存,然后被删除。

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

于 2015-03-28T10:44:30.957 回答