Skip to content

Instantly share code, notes, and snippets.

@n13i
Last active March 25, 2020 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n13i/b33d09d26466d8cf49ad8f65b66a2000 to your computer and use it in GitHub Desktop.
Save n13i/b33d09d26466d8cf49ad8f65b66a2000 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import re
import json
import urllib.request
import pychromecast
from pychromecast.controllers.youtube import YouTubeController
# Your Chromecast device name here
CAST_NAME = 'Your Chromecast device name here'
# Your Google API key here (using YouTube Data API v3)
API_KEY = 'Your Google API key here'
# Your AsaCoco pusher
PLAYLIST_ID = 'PLw58RgSzDmjr7hbo0zrfaQ027uMc6WHfO'
url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={0}&maxResults=1&key={1}'.format(PLAYLIST_ID, API_KEY)
res = urllib.request.urlopen(url)
data = json.loads(res.read().decode('utf-8'))
# find the freshest AsaCoco
item = data['items'][0]
title = item['snippet']['title']
video_id = item['snippet']['resourceId']['videoId']
print("found: {0} {1}".format(title, video_id))
# play it on Chromecast
chromecasts = pychromecast.get_chromecasts()
cast = next(cc for cc in chromecasts if cc.device.friendly_name == CAST_NAME)
cast.wait()
yt = YouTubeController()
cast.register_handler(yt)
yt.play_video(video_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment