0

約 5 つのカテゴリのビデオがあり、各カテゴリにはビデオ ファイルの数時間のセッションがあります。トルネードを使用してこれらすべてをエンド ユーザーに提供し、要求されたビデオを提供したいと考えています。取得する最良の方法は何ですか?これはできましたか?(サーバーはビデオが配置/追加されたディレクトリを監視し、対応するカテゴリメニューにビデオを自動的に追加/新しいカテゴリを作成できるため、URLを自動生成する方法が必要です。これは、ハンドラーを回避することも意味します各カテゴリ)。私が考えていることは可能ですか?

class VideoHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("relevant-video") # serve correct video maybe suing the incoming url?

settings = {'static_path': os.path.join(PATH, 'static')}

handlers = [(r'/videos', VideoHandler),
            (r'/favicon.ico', tornado.web.StaticFileHandler, {'path': favicon_path})]
4

1 に答える 1

1

tornado.ioloop.PeriodicCallbackビデオをカテゴリにマッピングするディクショナリを作成するために、次のタスクを定期的に実行できます。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import os

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)

videos = {}

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('index.html', my_videos=videos)

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/", MainHandler),
        ]
        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            debug=True,
        )
        tornado.web.Application.__init__(self, handlers, **settings)

    def update_categories(self): 
        videos.clear()
        for path, subdirs, files in os.walk(self.settings['static_path'] + '/videos/'):
            category_name = os.path.basename(path)
            videos[category_name] = []
            for name in files:
                videos[category_name].append(name)

if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = Application()
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.PeriodicCallback(app.update_categories, 1000).start() # run every second
    tornado.ioloop.IOLoop.instance().start()

サブディレクトリ名をカテゴリ名として使用します。プロジェクトツリー:

 $ tree 
 .
 ├── static
 │   └── videos
 │       ├── cat1
 │       │   ├── vid1.avi
 │       │   └── vid2.avi
 │       ├── cat2
 │       │   ├── vid3.avi
 │       └── Empty category
 ├── templates
 │   ├── index.html
 └── test.py

私はこのテンプレートを使用しています:

<!-- index.html -->
<html>
<head>
    <title>Test</title>
</head>
<body>
    {% for category in my_videos %}
        <h1>{{category}}</h1>
        {% for video in my_videos[category] %}
            <p>{{video}} -> {{static_url("videos/")}}{{category}}/{{video}}</p>
        {% end %}
    {% end %}
</body>
</html>

すべてのビデオを印刷します。ハンドラーの辞書から 1 つのビデオを選択して処理することもできます。

ファイルシステムは定期的にトラバースされるため、このアプローチは少し重くなります。代わりに、新しいコンテンツが追加されたときにアプリケーションに通知することをお勧めします。

于 2013-06-08T07:04:42.040 に答える