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