Created
December 6, 2019 05:54
-
-
Save coding-brigadier/daa6b4ffc9cdfea05d7774f42f6c0859 to your computer and use it in GitHub Desktop.
My attempt on making an eggdrop script to parse music info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package require http | |
package require base64 | |
package require xml | |
# Change these to point to your admin section | |
set host "localhost" | |
set port "8080" | |
set username "admin" | |
set password "hackme" | |
proc extract_music_info { xml } { | |
set source_opening "<source mount=\"/audio.ogg\">" | |
set source_closing "</source>" | |
set title_opening "<title>" | |
set title_closing "</title>" | |
set artist_opening "<artist>" | |
set artist_closing "</artist>" | |
set source_opening_len [string length $source_opening] | |
set title_opening_len [string length $title_opening] | |
set artist_opening_len [string length $artist_opening] | |
# Find the <source tag | |
set source_tag_start_idx [string first $source_opening $xml] | |
if { $source_tag_start_idx eq -1 } { | |
error "No music source was found" | |
} | |
set source_tag_idx [expr $source_tag_start_idx + $source_opening_len] | |
set data_len [string length $xml] | |
set sub_xml [string range $xml $source_tag_idx [expr $data_len - 1]] | |
set source_end_idx [string first $source_closing $sub_xml] | |
set source_xml [string range $sub_xml 0 [expr $source_end_idx - 1]] | |
# Get artist | |
set source_xml $sub_xml | |
set artist_tag_idx [string first $artist_opening $source_xml] | |
if { $artist_tag_idx eq -1 } { | |
error "Artist could not be found" | |
} | |
set artist_start_idx [expr $artist_tag_idx + $artist_opening_len] | |
set artist_end_idx [string first $artist_closing $source_xml] | |
set song("artist") [string range $source_xml $artist_start_idx [expr $artist_end_idx - 1]] | |
# Get title | |
set title_tag_idx [string first $title_opening $sub_xml] | |
if { $title_tag_idx eq -1 } { | |
error "Title could not be found" | |
} | |
set title_start_idx [expr $title_tag_idx + $title_opening_len] | |
set title_end_idx [string first $title_closing $source_xml] | |
set song("title") [string range $source_xml $title_start_idx [expr $title_end_idx - 1]] | |
return [array get song] | |
} | |
proc message_song_info { nick uhost handle channel txt } { | |
global username | |
global password | |
global host | |
global port | |
set login "$username:$password" | |
set auth "Basic [base64::encode $login]" | |
set headers [list Authorization $auth] | |
# set url "http://localhost:8000/admin/stats.xml" | |
set url "http://$host:$port/admin/stats.xml" | |
set token [http::geturl $url -headers $headers] | |
# upvar 1 $res state | |
set status [http::status $token] | |
if {$status ne "ok"} { | |
error $status | |
} | |
set data [http::data $token] | |
array set song [extract_music_info $data] | |
# puts $song("artist") | |
# puts $song("title") | |
set artist $song("artist") | |
set title $song("title") | |
putserv "privmsg $channel :$artist - $title" | |
} | |
# set val [message_song_info "test" "test" "Test" "#test" "teeeest"] | |
bind pubm * "% !np" message_song_info |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment