Skip to content

Commit

Permalink
Add support for NRK TV and NRK Radio apps (#777)
Browse files Browse the repository at this point in the history
* Add support for NRK TV and NRK Radio apps

* Update pychromecast/quick_play.py

---------

Co-authored-by: Erik Montnemery <[email protected]>
  • Loading branch information
lersveen and emontnemery authored Jan 10, 2024
1 parent 7e867ad commit 7a125c0
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 1 deletion.
73 changes: 73 additions & 0 deletions examples/nrkradio_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
Example on how to use the NRK Radio Controller
"""
# pylint: disable=invalid-name

import argparse
import logging
import sys
from time import sleep

import zeroconf

import pychromecast
from pychromecast import quick_play

# Change to the friendly name of your Chromecast
CAST_NAME = "Living Room"

# Note: Media ID can be found in the URL, e.g:
# For the live channel https://radio.nrk.no/direkte/p1, the media ID is p1
# For the podcast https://radio.nrk.no/podkast/tazte_priv/l_8457deb0-4f2c-4ef3-97de-b04f2c6ef314,
# the media ID is l_8457deb0-4f2c-4ef3-97de-b04f2c6ef314
# For the on-demand program https://radio.nrk.no/serie/radiodokumentaren/sesong/201011/MDUP01004510,
# the media id is MDUP01004510
MEDIA_ID = "l_8457deb0-4f2c-4ef3-97de-b04f2c6ef314"

parser = argparse.ArgumentParser(
description="Example on how to use the NRK Radio Controller to play a media stream."
)
parser.add_argument(
"--cast", help='Name of cast device (default: "%(default)s")', default=CAST_NAME
)
parser.add_argument(
"--known-host",
help="Add known host (IP), can be used multiple times",
action="append",
)
parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
parser.add_argument(
"--show-zeroconf-debug", help="Enable zeroconf debug log", action="store_true"
)
parser.add_argument(
"--media_id", help='MediaID (default: "%(default)s")', default=MEDIA_ID
)
args = parser.parse_args()

if args.show_debug:
logging.basicConfig(level=logging.DEBUG)
if args.show_zeroconf_debug:
print("Zeroconf version: " + zeroconf.__version__)
logging.getLogger("zeroconf").setLevel(logging.DEBUG)

chromecasts, browser = pychromecast.get_listed_chromecasts(
friendly_names=[args.cast], known_hosts=args.known_host
)
if not chromecasts:
print(f'No chromecast with name "{args.cast}" discovered')
sys.exit(1)

cast = chromecasts[0]
# Start socket client's worker thread and wait for initial status update
cast.wait()
print(f'Found chromecast with name "{args.cast}", attempting to play "{args.media_id}"')

app_name = "nrkradio"
app_data = {
"media_id": args.media_id,
}
quick_play.quick_play(cast, app_name, app_data)

sleep(10)

browser.stop_discovery()
73 changes: 73 additions & 0 deletions examples/nrktv_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
Example on how to use the NRK TV Controller
"""
# pylint: disable=invalid-name

import argparse
import logging
import sys
from time import sleep

import zeroconf

import pychromecast
from pychromecast import quick_play

# Change to the friendly name of your Chromecast
CAST_NAME = "Living Room"

# Note: Media ID for live programs can be found in the URL
# e.g. for https://tv.nrk.no/direkte/nrk1, the media ID is nrk1
# Media ID for non-live programs can be found by clicking the share button
# e.g. https://tv.nrk.no/serie/uti-vaar-hage/sesong/2/episode/2 shows:
# "https://tv.nrk.no/se?v=OUHA43000207", the media ID is OUHA43000207
MEDIA_ID = "OUHA43000207"

parser = argparse.ArgumentParser(
description="Example on how to use the NRK TV Controller to play a media stream."
)
parser.add_argument(
"--cast", help='Name of cast device (default: "%(default)s")', default=CAST_NAME
)
parser.add_argument(
"--known-host",
help="Add known host (IP), can be used multiple times",
action="append",
)
parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
parser.add_argument(
"--show-zeroconf-debug", help="Enable zeroconf debug log", action="store_true"
)
parser.add_argument(
"--media_id", help='MediaID (default: "%(default)s")', default=MEDIA_ID
)

args = parser.parse_args()

if args.show_debug:
logging.basicConfig(level=logging.DEBUG)
if args.show_zeroconf_debug:
print("Zeroconf version: " + zeroconf.__version__)
logging.getLogger("zeroconf").setLevel(logging.DEBUG)

chromecasts, browser = pychromecast.get_listed_chromecasts(
friendly_names=[args.cast], known_hosts=args.known_host
)
if not chromecasts:
print(f'No chromecast with name "{args.cast}" discovered')
sys.exit(1)

cast = chromecasts[0]
# Start socket client's worker thread and wait for initial status update
cast.wait()
print(f'Found chromecast with name "{args.cast}", attempting to play "{args.media_id}"')

app_name = "nrktv"
app_data = {
"media_id": args.media_id,
}
quick_play.quick_play(cast, app_name, app_data)

sleep(10)

browser.stop_discovery()
2 changes: 2 additions & 0 deletions pychromecast/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
APP_BBCSOUNDS = "D350F6A9"
APP_BBCIPLAYER = "5E81F6DB"
APP_SHAKA = "07AEE832"
APP_NRKTV = "3AEDF8D1"
APP_NRKRADIO = "A49874B1"


def get_possible_app_ids():
Expand Down
21 changes: 21 additions & 0 deletions pychromecast/controllers/nrkradio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Controller to interface with NRK Radio.
"""
# Note: Media ID can be found in the URL, e.g:
# For the live channel https://radio.nrk.no/direkte/p1, the media ID is p1
# For the podcast https://radio.nrk.no/podkast/tazte_priv/l_8457deb0-4f2c-4ef3-97de-b04f2c6ef314,
# the Media ID is l_8457deb0-4f2c-4ef3-97de-b04f2c6ef314
# For the on-demand program https://radio.nrk.no/serie/radiodokumentaren/sesong/201011/MDUP01004510,
# the media id is MDUP01004510

from .media import BaseMediaPlayer
from ..config import APP_NRKRADIO

APP_NAMESPACE = "urn:x-cast:com.google.cast.media"


class NrkRadioController(BaseMediaPlayer):
"""Controller to interact with NRK Radio."""

def __init__(self):
super().__init__(supporting_app_id=APP_NRKRADIO)
20 changes: 20 additions & 0 deletions pychromecast/controllers/nrktv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Controller to interface with NRK TV.
"""
# Note: Media ID for live programs can be found in the URL
# e.g. for https://tv.nrk.no/direkte/nrk1, the media ID is nrk1
# Media ID for non-live programs can be found by clicking the share button
# e.g. https://tv.nrk.no/serie/uti-vaar-hage/sesong/2/episode/2 shows:
# "https://tv.nrk.no/se?v=OUHA43000207", the media ID is OUHA43000207

from .media import BaseMediaPlayer
from ..config import APP_NRKTV

APP_NAMESPACE = "urn:x-cast:com.google.cast.media"


class NrkTvController(BaseMediaPlayer):
"""Controller to interact with NRK TV."""

def __init__(self):
super().__init__(supporting_app_id=APP_NRKTV)
8 changes: 7 additions & 1 deletion pychromecast/quick_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
from .controllers.yleareena import YleAreenaController
from .controllers.youtube import YouTubeController
from .controllers.shaka import ShakaController
from .controllers.nrktv import NrkTvController
from .controllers.nrkradio import NrkRadioController


def quick_play(cast, app_name, data):
def quick_play(cast, app_name, data): # pylint:disable=too-many-branches
"""
Given a Chromecast connection, launch the app `app_name` and start playing media
based on parameters defined in `data`.
Expand Down Expand Up @@ -72,6 +74,10 @@ def quick_play(cast, app_name, data):
controller = YouTubeController()
elif app_name == "shaka":
controller = ShakaController()
elif app_name == "nrktv":
controller = NrkTvController()
elif app_name == "nrkradio":
controller = NrkRadioController()
else:
raise NotImplementedError()

Expand Down

0 comments on commit 7a125c0

Please sign in to comment.