我希望从http://player.radiopilatus.ch解释者和歌曲在irc中实时改变歌曲新的年度
我对此有所测试,但不幸的是我没有问题
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 :(得分: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个程序。
get_current_song
与网站交谈以获取当前歌曲并将其返回。它没有别的。get_current_song_if_changed
以此为基础来检测歌曲的变化。poll_and_report_song_changes
以此为基础定期轮询,如果检测到更改,则在渠道上报告。radio_control
是实际绑定到操作的内容,可让您打开和关闭轮询。