Skip to content

Instantly share code, notes, and snippets.

@colegleason
Last active August 29, 2015 13:57
Show Gist options
  • Save colegleason/9401095 to your computer and use it in GitHub Desktop.
Save colegleason/9401095 to your computer and use it in GitHub Desktop.
[wearscript] Serve SRT files to Glass.
<html style="width:100%; height:100%; overflow:hidden">
<body style="width:100%; height:100%; overflow:hidden; margin:0">
<script>
function cb(channel, msg) {
var tree = new WS.Cards();
tree.add(msg, '');
WS.cardTree(tree);
}
function server() {
WS.log('Subtitles');
WS.sound('SUCCESS');
var tree = new WS.Cards();
tree.add('Waiting for subtitles','');
WS.cardTree(tree);
WS.displayCardTree();
WS.subscribe('subtitle', cb);
}
function main() {
if (WS.scriptVersion(1)) return;
WS.serverConnect('{{WSUrl}}', server);
}
window.onload = main;
</script>
</body>
</html>
{
"name": "Subtitles"
}
import pysrt
import datetime, time, calendar
import wearscript
import argparse
import time
import gevent
from HTMLParser import HTMLParser
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
def elapsed(since):
return (datetime.datetime.now() - since).total_seconds()
def seconds(time):
minutes = time.hour * 60 + time.minute
return minutes * 60 + time.second
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def length(sub):
start = datetime.datetime.combine(datetime.date(1,1,1),sub.start.to_time())
end = datetime.datetime.combine(datetime.date(1,1,1),sub.end.to_time())
td = end - start
return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**3
def broadcast_subs(args):
filename, ws = args
subs = pysrt.open(filename, encoding='iso-8859-1')
start = datetime.datetime.now()
for sub in subs:
while seconds(sub.start.to_time()) > elapsed(start):
pass
print sub.text, length(sub)
ws.publish("subtitle", strip_tags(sub.text), length(sub))
def callback(ws, **kwargs):
def print_subs(*data):
print "Message received!"
gevent.spawn(broadcast_subs, ('data/himym_1.srt', ws))
ws.handler_loop()
wearscript.parse(callback, argparse.ArgumentParser())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment