1 | #-*- coding: utf-8 -*-
|
---|
2 | import collections
|
---|
3 | import locale
|
---|
4 | import os
|
---|
5 | import re
|
---|
6 | import subprocess
|
---|
7 | import urllib
|
---|
8 |
|
---|
9 | ROOT = ur'D:\Movie'
|
---|
10 | EXTS = '.avi,.mkv,.mp3,.mp4,.mov,.wma,.wmv'.split(',')
|
---|
11 |
|
---|
12 | class Locale:
|
---|
13 | UTF8 = 'utf-8'
|
---|
14 | LOCAL = locale.getpreferredencoding()
|
---|
15 |
|
---|
16 | def __init__(self, string):
|
---|
17 | self.string = string
|
---|
18 |
|
---|
19 | @property
|
---|
20 | def utf8(self):
|
---|
21 | try:
|
---|
22 | return self.string.decode(Locale.LOCAL).encode(Locale.UTF8)
|
---|
23 | except UnicodeEncodeError:
|
---|
24 | return self.string
|
---|
25 |
|
---|
26 | @property
|
---|
27 | def local(self):
|
---|
28 | try:
|
---|
29 | return self.string.decode(Locale.UTF8).encode(Locale.LOCAL)
|
---|
30 | except UnicodeEncodeError:
|
---|
31 | return self.string
|
---|
32 |
|
---|
33 | def sort(lst):
|
---|
34 | convert = lambda text: int(text) if text.isdigit() else text.lower()
|
---|
35 | alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
|
---|
36 | return sorted(lst, key=alphanum_key)
|
---|
37 |
|
---|
38 | def ext(path):
|
---|
39 | return os.path.splitext(path)[1].lower()
|
---|
40 |
|
---|
41 | def find_media(dirpath):
|
---|
42 | try:
|
---|
43 | for entry in sort(os.listdir(dirpath)):
|
---|
44 | fullpath = os.path.join(dirpath, entry)
|
---|
45 |
|
---|
46 | if os.path.isfile(fullpath) and ext(fullpath) in EXTS:
|
---|
47 | return fullpath
|
---|
48 | except:
|
---|
49 | print 'except: ' + dirpath
|
---|
50 |
|
---|
51 | return None
|
---|
52 |
|
---|
53 | def play_dir(media_dir):
|
---|
54 | os.startfile(find_media(media_dir))
|
---|
55 |
|
---|
56 |
|
---|
57 | from flask import Flask, redirect, url_for
|
---|
58 |
|
---|
59 | app = Flask(__name__)
|
---|
60 |
|
---|
61 | def html(body):
|
---|
62 | BODY_KEY = u'__BODY__'
|
---|
63 |
|
---|
64 | with open('template.html', 'rb') as f:
|
---|
65 | TEMPLATE = unicode(f.read().decode('utf-8'))
|
---|
66 |
|
---|
67 | return TEMPLATE.replace(BODY_KEY, body)
|
---|
68 |
|
---|
69 | @app.route('/')
|
---|
70 | def root():
|
---|
71 | return redirect('/explore')
|
---|
72 |
|
---|
73 | @app.route('/explore/')
|
---|
74 | @app.route('/explore/<path:subpath>')
|
---|
75 | def listing(subpath=''):
|
---|
76 | body = ''
|
---|
77 |
|
---|
78 | fullpath = os.path.join(ROOT, subpath)
|
---|
79 | for entry in sort(os.listdir(fullpath)):
|
---|
80 | entrypath = os.path.join(fullpath, entry)
|
---|
81 |
|
---|
82 | if os.path.isfile(entrypath):
|
---|
83 | continue
|
---|
84 |
|
---|
85 | media = find_media(entrypath)
|
---|
86 | if media:
|
---|
87 | rest_prefix = u'play'
|
---|
88 | else:
|
---|
89 | rest_prefix = u'explore'
|
---|
90 |
|
---|
91 | name = os.path.basename(entry)
|
---|
92 | href = u'/{0}/{1}/{2}'.format(rest_prefix, subpath, entry).replace('//', '/')
|
---|
93 |
|
---|
94 | body += u'<a href="{0}">{1}</a> <br />\n'.format(href, name)
|
---|
95 |
|
---|
96 | return html(body)
|
---|
97 |
|
---|
98 | @app.route('/play/<path:media>')
|
---|
99 | def play(media):
|
---|
100 | media = urllib.unquote(media).replace('/', os.path.sep)
|
---|
101 | media_dir = os.path.join(ROOT, media)
|
---|
102 |
|
---|
103 | play_dir(media_dir)
|
---|
104 |
|
---|
105 | closing_script = '<script> window.history.back(); </script>'
|
---|
106 | return closing_script
|
---|
107 |
|
---|
108 | def main():
|
---|
109 | app.run(host='0.0.0.0',
|
---|
110 | port=80,
|
---|
111 | threaded=True)
|
---|
112 |
|
---|
113 | if __name__ == '__main__':
|
---|
114 | main()
|
---|
115 |
|
---|