2017-01-01 24 views
0

我就从http://player.radiopilatus.ch喜欢解释,并在IRC歌曲annouce实时当改变乐曲的新annouceTcl的HTML单选annouce

我已经testet这一点,但不幸的是我没有问题

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"; 
} 

在我想是这样结尾:

 
!song Interpret Song Unixtimestamp 
!song MARLON_ROUDETTE NEW_AGE 1483293195 

回答

0

通常情况下,你需要问的网站每隔一段时间什么的当前状态(即,当前歌曲)并在您看到更改时进行报告。你问的越频繁,结果就越准确,但是网站的负载就越大(许多人反对让他们的网站被他人的代码所打击)。所以让我们每10秒轮询一次,即10000毫秒。

我们还需要将当前值存储在全局变量中,以便我们可以检测到发生更改的时间。当变化发生时,我们更新全球。如果我们把代码分成几个过程,每个过程都有自己的简单工作,那将会更简单。

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 
    } 
} 

请注意,现在有4个程序。

  1. get_current_song与网站交谈刮目前的歌曲并返回。它没有别的。
  2. get_current_song_if_changed建立在此检测歌曲变化。
  3. poll_and_report_song_changes在此基础上进行定期轮询,如果检测到更改,请在频道上报告。
  4. radio_control是什么实际上绑定到操作,并允许您打开和关闭投票。