Need help to scrape song data from 'I Like Radio' website

Hi,

I use direct streams to play radio on my google home speaker but I don’t get any song or artist info. I would like scrape this website Guldkanalen – MUSIK FÖR VUXNA ÖRON - I LIKE RADIO to get the info.

I don’t have any experience with scrapers before so I’m not sure how to do. I have tried to follow two guides from the community:

When trying these sensors

  - platform: scrape
    resource: https://www.ilikeradio.se/guldkanalen/
    name: Guldkanalen_track
    select: 'span[class="player-audio-content-title-content"]'
    value_template: '{{ value | title }}'

  - platform: scrape
    resource: https://www.ilikeradio.se/guldkanalen/
    name: Guldkanalen_artist
    select: 'span[class="player-audio-content-desc"]'
    value_template: '{{ value | title }}'
  
  - platform: scrape
    name: Guldkanalen_song
    resource: https://www.ilikeradio.se/guldkanalen/
    select: ".entry-content > div:nth-child(2) > div > span.player-audio-content-title-content"

  - platform: scrape
    name: Guldkanalen_singer
    resource: https://www.ilikeradio.se/guldkanalen/
    select: ".entry-content > div:nth-child(2) > div > span.player-audio-content-desc"

I get this result
ha_lovelace

Is it possible to do this or is there another method?

I’m afraid you’re out of luck using the scrape sensor. That website uses JavaScript to populate the player. What HA sees is:

<div class="no-pm">
<div class="player-audio-content-title">
<span class="player-audio-content-title-content"></span>
<span class="player-audio-content-desc"></span>
</div>
</div>

However, you might be able to fetch the data using the rest integration. I can see that the web browser is retrieving some data from “https://app.khz.se/api/v2/program/current?channel_id=72&with=host%2Cbuttons%2Cimages” and “https://app.khz.se/api/v2/timeline?channel_id=72&client_id=15618288&to=2022-05-22T15%3A26%3A12&limit=20”.
Maybe you can leave out some of the parameters? It seems as if “https://app.khz.se/api/v2/timeline?channel_id=72&limit=1” gives you the current song?

I tried this

  - platform: rest
    name: rest_artist
    resource: https://app.khz.se/api/v2/timeline?channel_id=72&limit=1
    value_template: "{{ value_json.artist_name }}"
    scan_interval: 15

  - platform: rest
    name: rest_title
    resource: https://app.khz.se/api/v2/timeline?channel_id=72&limit=1
    value_template: "{{ value_json.title }}"
    scan_interval: 15

But it doesn’t return anything.

The JSON returned is actually a bit more complex. Try this:

rest:
  - resource: https://app.khz.se/api/v2/timeline?channel_id=72&limit=1
    scan_interval: 15
    sensor:
      - name: Artist
        value_template: "{{ value_json[0].song.artist_name }}"
      - name: Title
        value_template: "{{ value_json[0].song.title }}"

Thanks,
I combined them to get a single sensor with both artist and title

rest:
  - resource: https://app.khz.se/api/v2/timeline?channel_id=72&limit=1
    scan_interval: 15
    sensor:
      - name: Guldkanalen
        value_template: >-
          {{ value_json[0].song.artist_name}} - {{ value_json[0].song.title }}

ha_song_info

1 Like