69 lines
1.6 KiB
Python
69 lines
1.6 KiB
Python
# Required Klein and Pychromecast modules
|
|
from twisted.web.static import File
|
|
from klein import run, route
|
|
from klein import Klein
|
|
import pychromecast
|
|
import time
|
|
import os
|
|
import json
|
|
|
|
|
|
app = Klein()
|
|
|
|
chromecasts = pychromecast.get_chromecasts()
|
|
[cc.device.friendly_name for cc in chromecasts]
|
|
cast = next(cc for cc in chromecasts if cc.device.friendly_name == "living room") # You may need to change to your chromecast name for now
|
|
cast.wait()
|
|
print(cast.status) # Has display name and status text
|
|
mc = cast.media_controller
|
|
|
|
@route('/movies/', branch=True)
|
|
def static(request):
|
|
return File("./movies")
|
|
|
|
@route('/devices/')
|
|
def dlist(request):
|
|
|
|
return mc
|
|
|
|
@route('/play/<fn>')
|
|
def crun(request,fn):
|
|
mc.play_media('http://192.168.1.158:8080/movies/' + fn, 'video/mp4') # Update this IP to match yours
|
|
mc.block_until_active()
|
|
mc.pause()
|
|
time.sleep(2)
|
|
mc.play()
|
|
return 'Play'
|
|
|
|
@route('/pause/')
|
|
def cpause(request):
|
|
mc.pause()
|
|
return 'Pause'
|
|
|
|
@route('/resume/')
|
|
def cres(request):
|
|
mc.play()
|
|
return 'Resuming'
|
|
|
|
@route('/stop/')
|
|
def cstop(request):
|
|
mc.stop()
|
|
return 'STOP!!!'
|
|
|
|
@route('/list/')
|
|
def clist(request):
|
|
path = './movies'
|
|
files = os.listdir\
|
|
(path)
|
|
e = []
|
|
d = {'data' : e }
|
|
for x in range(len(files)):
|
|
#e.append("movie")
|
|
e.append(files[x])
|
|
return json.dumps(d)
|
|
|
|
@route('/')
|
|
def home(request):
|
|
return 'Welcome to the RPI Python based Chromecast Media Service! Please use the HTTP API!'
|
|
|
|
run("192.168.1.158", 8080) # Obviously you need to change that IP to your local one
|