Tcl html Radio annouce

96 views Asked by At

I would like from http://player.radiopilatus.ch the interpreter and the song annouce in the irc in realtime when change the Song an new annouce

I have testet with this, but unfortunately I have no issue

set radio_url "http://player.radiopilatus.ch"

bind pub - !radio radio_get

proc radio_get { nick uhost handle channel text } {
    global radio_url
    set file [open "|lynx -source $radio_url" r]

    set html "[gets $file]"
    regsub -all "<br>" $html " " html
    regsub -all "<\[^b]{0,1}b{0,1}>" $html "" html
    regsub "text1=" $html "" html
    regsub "NOW PLAYING:" $html "Now on http://player.radiopilatus.ch playing     \002" html

    putnow "PRIVMSG $channel :$html";
}

At the end I would like something like:

!song Interpret Song Unixtimestamp
!song MARLON_ROUDETTE NEW_AGE 1483293195
1

There are 1 answers

0
Donal Fellows On

Typically, you need to ask the website every so often what the current state (i.e., the current song) is and report when you see a change. The more frequently you ask, the more accurate the results, but the greater the load on the site (and many people object to having their sites hammered by others' code). So let's poll every 10 seconds, i.e., 10000 milliseconds.

We also need to store the current value in a global variable so that we can detect when a change has happened. When the change does happen, we update the global. It's going to be simpler if we break the code up into several procedures, each with its own simpler job.

proc get_current_song {} {
    global radio_url
    set file [open "|lynx -source $radio_url" r]
    set html [gets $file]
    # IMPORTANT! Close the channel once we're done with it...
    close $file

    regsub -all "<br>" $html " " html
    regsub -all "<\[^b]{0,1}b{0,1}>" $html "" html
    regsub "text1=" $html "" html
    regsub "NOW PLAYING:" $html "" song
    return $song
}

set current_song ""

proc get_current_song_if_changed {} {
    global current_song
    set song [get_current_song]
    if {$song ne $current_song} {
        set current_song $song
        return $song
    }
    # No change, so the empty string
    return ""
}

set running 0

proc poll_and_report_song_changes {channel} {
    # Allow us to stop the loop
    global running
    if {!$running} {
        return
    }

    # Schedule the next call of this procedure
    after 10000 poll_and_report_song_changes $channel

    set song [get_current_song_if_changed]
    if {$song ne ""} {
        putnow "PRIVMSG $channel :Now on http://player.radiopilatus.ch playing     \002$song"
    }
}

bind pub - !radio radio_control

proc radio_control { nick uhost handle channel text } {
    global running
    if {$running} {
        set running 0
        putnow "PRIVMSG $channel :Will stop reporting song changes"
    } else {
        set running 1
        putnow "PRIVMSG $channel :Will start reporting song changes"
        poll_and_report_song_changes $channel
    }
}

Notice that there are now 4 procedures.

  1. get_current_song talks to the website to scrape the current song and returns that. It does nothing else.
  2. get_current_song_if_changed builds on that to detect song changes.
  3. poll_and_report_song_changes builds on that to periodically poll, and if a change is detected, report it on the channel.
  4. radio_control is what is actually bound to actions, and lets you turn on and off the polling.