YoutubeTV - channel with ADB - channel codes

I may be too far down a rabbit hole here but posting in case someone can help…

I have set up automations that allow me to tune to certain channels in YoutubeTV, using ADB commands from a thread that I now cannot find:

  - service: androidtv.adb_command
    data_template:
      entity_id: media_player.tv_adb
      command: am start -a android.intent.action.VIEW -d watch/{{ state_attr('sensor.youtubetv_channel_lookup',
        'channels')[channel] }} -n com.google.android.youtube.tvunplugged/com.google.android.apps.youtube.tvunplugged.activity.MainActivity

The trick here is having to map the channel (e.g. ESPN) to YoutubeTV’s channel code (ESPN is currently bj3v-DQPnNs). I thought I was smart and I’d set up a sensor with the mappings, but as it turns out apparently they rotate those from time to time, which breaks my automations, and I’m tired of updating them manually.

So…I’ve been trying to come up with a way to scrape these from their site but it’s proven more challenging than I expected.

Stepping back – has anyone come up with a fool proof way to do this? Or maybe have any ideas about scraping?

I’ll be glad to share more details of what I have working if anyone is interested, but as I said it breaks about once a month, give or take…

Thanks for posting. I am interested in your work. Don’t yet have anything to offer, but am going to look into it. as i also use youtube tv

What is the page you are scraping?
You can use node red for scraping, it has more control since it uses javascript in the function nodes.

The only place I know that has this information is https://tv.youtube.com/live

…which is:
behind Google’s authentication,
a BIG page, and
complicated HTML.

And those three have vexxed me so far, lol.

I went there incognito and I could see the urls.
Then there is a “see all channels” button which leads to here:


If these are the real links then at least you don’t need to auth.
Since I’m typing on my phone I haven’t been able to look at the HTML but whatever it is, it’s possible.

Is these links correct?

I just took a look… seems like those are the same URL’s except they are prepended with the channel name in that list and not so in the guide.
so in the channel list you linked. you’ll see abc-blahblahlbha but in the real guide once logged in there is not abc- (abc- being the channel name, cbs-, fox-, etc)
so it may be a good starting point to scrape.

So using blahblahlbha will that give you ABC on the media player?
Or do you need to add something more that is not static?

Yes… on the page you linked not signed in for channel ABC if I copy the link there and
put that exact link into my signed in page, it removes the abc- part and brings up the ABC channel ‘page’

Hrm, on the web it does bring up the channel but sending that via ADB doesn’t work. E.g. I tried:

  • watch\espn-UCakwQ1jKQnYJcUMghvnp-Yw
  • browse\espn-UCakwQ1jKQnYJcUMghvnp-Yw
  • watch\UCakwQ1jKQnYJcUMghvnp-Yw

YTTV just kind of flashes but doesn’t change the channel. Maybe that link is a web-specific thing?

But what is the correct url for that channel then?

I’m also manually updating by grabbing the URL from LiveTV in a web browser, and luckily it doesn’t change that often. CNN is currently https://tv.youtube.com/watch/4Ogm6iKafo4, for instance. ESPN is https://tv.youtube.com/watch/bj3v-DQPnNs. If there’s a way to scrape those URLs and update them automatically, I’m all in.

Lol, either we watch too much TV (quite possible) or have bad timing, but it seems like we hit it regularly enough to be annoying. Then we got super lazy and just used the ones that worked, and now they’re all broken. Worse, I don’t think they change them all together, and some seem to change less frequently than others.

Probably, they’re just tracking the channels I watch, lol.

I only use it to emulate a “regular” TV so that I get live TV when I turn the TV on, and it just goes straight to CNN. It used to change more frequently, but it’s every few months now that I’ll notice that my automation no longer works.

Ah, that explains it. Things I had working at one point in time:

  1. Custom Alexa Skill, allowing “Ask Television to Tune to [Channel]”
  2. Remembering Previous Channel, allowing Alexa “Ask Television to go to previous channel”
  3. Dedicated Google Calendar allowing scheduled tuning. E.g. add a calendar entry for “ESPN” at 5 p.m. and whatever TV is on will tune to that.
  4. Transferring channel from Living Room to Bedroom, i.e. turning on Bedroom TV will tune to whatever the Living Room was last tuned to.

I told you, we watch too much TV. :slight_smile:

But it all needs this mapping. I.e. if you were last on channel X it needs to be able to look up that X = blahblahblah to send you back to that channel.

1 Like

watch wont work because it has to be browse. and you cant use that to go directly to live tv because each show has it’s own ID. so best you can do is browse to the channel.
you have a back slash above. can you try re-sending with a forward slash?

Wrong. Maybe you’re talking about the web, but as I said, I have had this working, except for having to update the links when they get rotated.

This will take your player to ESPN right now, assuming you have ADB configured:

  - service: androidtv.adb_command
    data_template:
      entity_id: media_player.tv_adb
      command: am start -a android.intent.action.VIEW -d watch/bj3v-DQPnNs -n com.google.android.youtube.tvunplugged/com.google.android.apps.youtube.tvunplugged.activity.MainActivity

In a few days/weeks? Not so much.

One really frustrating thing about Youtube TV is trying to Google anything about it. 99.9% of the hits you get are just for Youtube, and there just aren’t enough people hacking Youtube TV for this type of thing to foster a community.

1 Like

Okay. Thanks for letting me know

This finally got annoying enough for me to get back to. I don’t have a great solution, but I’ve taken another step forward, so posting here in case it helps anyone, or someone can take the next step forwards.

I found the YouTube TV website gets its data from this page:
https://tv.youtube.com/youtubei/v1/unplugged/browse

I haven’t been able to call that directly without getting blocked by their automation detection. So what I’m doing now instead is to manually load the website (tv.youtube.com/live) with dev tools loaded, and copy/pasting the results of the above file to my server. So now I can get all the updated channels at once just by pulling/updating this file. Then I’ve written the attached php to parse the codes from their file into JSON that can be consumed in a REST sensor within HA.

<?php
$youtubeSource = 'browse.json';

$youtubeRaw = json_decode(file_get_contents($youtubeSource), true);

$returnValues = array();

foreach ($youtubeRaw["contents"]["epgRenderer"]["paginationRenderer"]["epgPaginationRenderer"]["contents"] as $channelObj) {    
    $channelName = $channelObj["epgRowRenderer"]["station"]["epgStationRenderer"]["icon"]["accessibility"]["accessibilityData"]["label"];
    $channelValue = $channelObj["epgRowRenderer"]["airings"][0]["epgAiringRenderer"]["navigationEndpoint"]["watchEndpoint"]["videoId"];

    if ($channelValue == "") {
        $channelValue = $channelObj["epgRowRenderer"]["airings"][0]["epgAiringRenderer"]["navigationEndpoint"]["unpluggedPopupEndpoint"]["popupRenderer"]["unpluggedSelectionMenuDialogRenderer"]["items"][0]["unpluggedMenuItemRenderer"]["command"]["watchEndpoint"]["videoId"];
    }

    array_push($returnValues, ["name" => strtoupper(trim($channelName)), "value" => trim($channelValue)]);
}

if (empty($returnValues)) {
    print '{"count":0,"channels":{}}';
} else {
    printf('{"count":%s,"channels":{', count($returnValues) );
    $hasPrinted = false;
    foreach ($returnValues as $row) {
        if ($hasPrinted) 
            print ', ';
        
        printf('"%s": "%s"', $row["name"], $row["value"]);
        $hasPrinted = true;
    }
    print '}}';
}

?>

Why are you manually printing an array as JSON?
Why not json_encode and echo it?