def main(): chromecasts, browser = pychromecast.get_listed_chromecasts( friendly_names=[CAST_NAME]) if not chromecasts: print('No chromecast with name "{}" discovered'.format(args.cast)) sys.exit(1) cast = chromecasts[0] # Start socket client's worker thread and wait for initial status update cast.wait() # Create Youtube Controller + Register it yt = YouTubeController() cast.register_handler(yt) # Create a new Youtube Session yt.start_session_if_none() s1 = yt._session s1._start_session() # Initialize the Queue to play s1._initialize_queue(VIDEO_LIST[0]) print("Queue Play Order: ") print("1. " + VIDEO_LIST[0]) order = 2 # Add wanted video id's to the Queue for id in VIDEO_LIST[1:]: print(str(order) + ". " + id) yt.add_to_queue(id) order += 1
class ChromeCast: # Initializer / Instance Attributes def __init__(self, name): self.name = name chromecasts = pychromecast.get_chromecasts() cast = next(cc for cc in chromecasts if cc.device.friendly_name == name) cast.wait() self.yt = YouTubeController() cast.register_handler(self.yt) def play(self, videoId): self.yt.start_session_if_none() self.yt.play_video(videoId)
def main(): os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' PLAYLIST = {} # Authenticate OAuth2 with Google creds = client.GoogleCredentials(access_token=None, client_id=CLIENT_ID, client_secret=CLIENT_SECRET, refresh_token=REFRESH_TOKEN, token_expiry=None, token_uri=TOKEN_URI, user_agent=USER_AGENT) http = creds.authorize(httplib2.Http()) creds.refresh(http) # Build the service youtube = build(API_SERVICE_NAME, API_VERSION, http=http) # Randomize Playlists # Comment this line if you don't want randomized playlists random.shuffle(SERIAL_LIST) # Loop through each playlist for serials in SERIAL_LIST: # Retrieve video's JSON values request = youtube.playlistItems().list(part="snippet,contentDetails", maxResults=10, playlistId=serials) response = request.execute() # Get a list of playlist items list_of_videos = response.get('items') video_ids = [] # For each playlist item, get the corresponding video id for vids in list_of_videos: content_details = vids.get('contentDetails') snippet = vids.get('snippet') # Region blocking, age-restriction, deleted vid - maybe, later .... # Check if video is private if snippet.get('title') != "Private video": video_ids.append(content_details.get('videoId')) PLAYLIST[serials] = video_ids # Combine all video id's together into Queue # Plays the videos in playlist in order x = PLAYLIST.values() queue = [j for i in x for j in i][::-1] # If you want to randomize the videos, uncomment below # random.shuffle(queue) cast = None # Keep trying to connect until wanted chromecast is online while (cast == None): chromecasts, browsers = pychromecast.get_chromecasts() try: # Loop through all the chromecasts in the house cast = next(cc for cc in chromecasts if cc.device.friendly_name == CAST_NAME) except: print("Chromecast Not Found") cast.wait() # Create Youtube Controller + Register it yt = YouTubeController() cast.register_handler(yt) # Create a new Youtube Session yt.start_session_if_none() s1 = yt._session s1._start_session() # Initialize the Queue to play s1._initialize_queue(queue[0]) print("Queue Play Order: ") print("1. " + queue[0]) order = 2 # Add wanted video id's to the Queue for id in queue[1:]: print(str(order) + ". " + id) yt.add_to_queue(id) order += 1