source: mobile-remote-controller/trunk/mobile-remote-controller.py

Last change on this file was 6, checked in by cheese, 7 years ago

#1 remove noneffective import and comment

File size: 2.4 KB
Line 
1#-*- coding: utf-8 -*-
2import collections
3import locale
4import os
5import re
6import subprocess
7import urllib
8
9ROOT = ur'D:\Movie'
10EXTS = '.avi,.mkv,.mp3,.mp4,.mov,.wma,.wmv'.split(',')
11
12class 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
33def 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
38def ext(path):
39 return os.path.splitext(path)[1].lower()
40
41def 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
53def play_dir(media_dir):
54 os.startfile(find_media(media_dir))
55
56
57from flask import Flask, redirect, url_for
58
59app = Flask(__name__)
60
61def 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('/')
70def root():
71 return redirect('/explore')
72
73@app.route('/explore/')
74@app.route('/explore/<path:subpath>')
75def 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>')
99def 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
108def main():
109 app.run(host='0.0.0.0',
110 port=80,
111 threaded=True)
112
113if __name__ == '__main__':
114 main()
115
Note: See TracBrowser for help on using the repository browser.