I am having a lot of trouble with adding other's GSPID's into a .txt file, and then having it get the peron's GSPID from the file and then adding it the URL (with the command !mphtstats <nick>). I need it to find the <nick> in the .txt file and then get their GSPID that they added (with the command !addmphtgspid <gspid>) and then have it put their GSPID in the URL.
############################################################################################################################################################################
set mydatafile "/home/whatever/bot/data/mydata.txt"
set userperms "f|f"
bind pub - !save saveproc
bind pub - !getdata dataproc
proc saveproc {nick uhost hand chan text} {
global mydatafile userperms
# Probably should test $text for proper format (numbers only, length, etc,) too...
set text [split $text]
# test if user has the right flags/permissions to use
if {![matchattr $hand $userperms $chan]} {
puthelp "PRIVMSG $nick :Sorry $nick, I don't know you well enough to use this."
return
}
if {![file exists $mydatafile]} {
# lucky user first one to add data
set dataOut [open $mydatafile w]
puts $dataOut "$hand:$text"
catch {close $dataOut}
return
}
set linenumber -1
# edit: OOPS forgot to open the file first, lol
set input [open $mydatafile r]
set dataIn [split [read $input] \n]
catch {close $input}
foreach line $dataIn {
incr linenumber
# check to see if user has an existing line, replace with new data if they do
if {[string tolower [lindex [split $line :] 0]] == [string tolower $hand]} {
set dataIn [lreplace $dataIn $linenumber $linenumber "$hand:$text"]
}
}
# There were no matching handles found, so just insert the data
if {$linenumber == -1} {
set dataIn [linsert $dataIn end "$hand:text"]
}
# write data to file
set dataOut [open $mydatafile w]
foreach line $dataIn {
if {$line != ""} {
puts $dataOut [join $line]
}
}
flush $dataOut
catch {close $dataOut}
}
proc getdata {nick uhost hand chan text} {
global mydatafile userperms
# test if user has the right flags/permissions to use
if {![matchattr $hand $userperms $chan]} {
puthelp "PRIVMSG $nick :Sorry $nick, I don't know you well enough to use this."
return
}
if {![file exists $mydatafile]} {
puthelp "PRIVMSG $nick :No data file found, try !save gspid"
return
}
set gspid ""
set linenumber -1
# open file first here too :P
set input [open $mydatafile r]
set dataIn [split [read $input] \n]
catch {close $input}
foreach line $dataIn {
incr linenumber
# check to see if user has an existing line
if {[string tolower [lindex [split $line :] 0]] == [string tolower $hand]} {
set gspid [lindex [split $line :] 1]
}
}
if {$gspid != ""} {
set url "http://www.nintendowifi.com/gaminghub/MetroidHuntersLicenseFrame.jsp?gspid=$gspid&lang=en®ion=en_US&locale=en_U"
} else {
puthelp "PRIVMSG $nick :You don't have a gspid saved.. Try !save <gspid> first"
return
}
# Now you can run your geturl stuff
catch {set token [::http::geturl $url -timeout 30000]} error
if {[string match -nocase "*couldn't open socket*" $error]} {
puthelp "PRIVMSG $nick :Error: couldn't connect..Try again later"
::http::cleanup $page
return
}
if { [::http::status $page] == "timeout" } {
puthelp "PRIVMSG $nick :Error: Connection timed out."
::http::cleanup $page
return
}
set content [::http::data $token]
::http::cleanup $content
# so forth and so on, etc etc.
}
Not tested, should work, standard disclaimer applies..
Edit: Fixed a couple of oversights
Last edited by rosc2112 on Sun Oct 01, 2006 4:03 pm, edited 1 time in total.
set mphtstatsfile "scripts/stats/mphtstats.txt"
set userperms "-|-"
bind pub - !addgspid add:gspid
bind pub - !mphtstats show:mphtstats
proc add:gspid {nick uhost hand chan text} {
global mphtstatsfile userperms
# Probably should test $text for proper format (numbers only, length, etc,) too...
set text [split $text]
# test if user has the right flags/permissions to use
if {![matchattr $hand $userperms $chan]} {
puthelp "PRIVMSG $nick :Sorry $nick, I don't know you well enough to use this."
return
}
if {![file exists $mphtstatsfile]} {
# lucky user first one to add data
set dataOut [open $mphtstatsfile w]
puts $dataOut "$hand:$text"
catch {close $dataOut}
return
}
set linenumber -1
# edit: OOPS forgot to open the file first, lol
set input [open $mphtstatsfile r]
set dataIn [split [read $input] \n]
catch {close $input}
foreach line $dataIn {
incr linenumber
# check to see if user has an existing line, replace with new data if they do
if {[string tolower [lindex [split $line :] 0]] == [string tolower $hand]} {
set dataIn [lreplace $dataIn $linenumber $linenumber "$hand:$text"]
}
}
# There were no matching handles found, so just insert the data
if {$linenumber == -1} {
set dataIn [linsert $dataIn end "$hand:text"]
}
# write data to file
set dataOut [open $mphtstatsfile w]
foreach line $dataIn {
if {$line != ""} {
puts $dataOut [join $line]
}
}
flush $dataOut
catch {close $dataOut}
}
proc show:mphtstats {nick uhost hand chan text} {
global mphtstatsfile userperms
# test if user has the right flags/permissions to use
if {![matchattr $hand $userperms $chan]} {
puthelp "PRIVMSG $nick :Sorry $nick, I don't know you well enough to use this."
return
}
if {![file exists $mphtstatsfile]} {
puthelp "PRIVMSG $nick :No data file found, try !addgspid gspid"
return
}
set gspid ""
set linenumber -1
# open file first here too :P
set input [open $mphtstatsfile r]
set dataIn [split [read $input] \n]
catch {close $input}
foreach line $dataIn {
incr linenumber
# check to see if user has an existing line
if {[string tolower [lindex [split $line :] 0]] == [string tolower $hand]} {
set gspid [lindex [split $line :] 1]
}
}
if {$gspid != ""} {
set url "http://www.nintendowifi.com/gaminghub/MetroidHuntersLicenseFrame.jsp?gspid=$gspid&lang=en®ion=en_US&locale=en_U"
} else {
puthelp "PRIVMSG $nick :You don't have a gspid saved.. Try !addgspid <gspid> first"
return
}
# Now you can run your geturl stuff
catch {set token [::http::geturl $url -timeout 30000]} error
if {[string match -nocase "*couldn't open socket*" $error]} {
puthelp "PRIVMSG $nick :Error: couldn't connect..Try again later"
::http::cleanup $page
return
}
if { [::http::status $page] == "timeout" } {
puthelp "PRIVMSG $nick :Error: Connection timed out."
::http::cleanup $page
return
}
set content [::http::data $token]
::http::cleanup $content
set top "<tr><td style=\"text-align: left;\" class=\"left\">Overall Rank</td><td style=\"text-align: right\" class=\"right\">"
set bottom "</td></tr>"
set tosave [string range $content [set f [string first $top $content]] [string first $bottom $content $f]]
regsub -all {<[^>]+>|<} $tosave { } tosave
foreach line [split $tosave \n] {
set top2 "<tr><td style=\"text-align: left;\" class=\"left\">Alt Kills</td><td style=\"text-align: right\" class=\"right\">"
set bottom2 "</td></tr>"
set tosave2 [string range $content [set f [string first $top2 $content]] [string first $bottom2 $content $f]]
regsub -all {<[^>]+>|<} $tosave2 { } tosave2
foreach line2 [split $tosave2 \n] {
set top3 "<tr><td style=\"text-align: left;\" class=\"left\">BiPed Kills</td><td style=\"text-align: right\" class=\"right\">"
set bottom3 "</td></tr>"
set tosave3 [string range $content [set f [string first $top3 $content]] [string first $bottom3 $content $f]]
regsub -all {<[^>]+>|<} $tosave3 { } tosave3
foreach line3 [split $tosave3 \n] {
set top4 "<tr><td style=\"text-align: left;\" class=\"left\">Games Won</td><td style=\"text-align: right\" class=\"right\">"
set bottom4 "</td></tr>"
set tosave4 [string range $content [set f [string first $top4 $content]] [string first $bottom4 $content $f]]
regsub -all {<[^>]+>|<} $tosave4 { } tosave4
foreach line4 [split $tosave4 \n] {
set top5 "<tr><td style=\"text-align: left;\" class=\"left\">Games Played</td><td style=\"text-align: right\" class=\"right\">"
set bottom5 "</td></tr>"
set tosave5 [string range $content [set f [string first $top5 $content]] [string first $bottom5 $content $f]]
regsub -all {<[^>]+>|<} $tosave5 { } tosave5
foreach line5 [split $tosave5 \n] {
set top6 "<tr><td style=\"text-align: left;\" class=\"left\">Disconnects</td><td style=\"text-align: right\" class=\"right\">"
set bottom6 "</td></tr>"
set tosave6 [string range $content [set f [string first $top6 $content]] [string first $bottom6 $content $f]]
regsub -all {<[^>]+>|<} $tosave6 { } tosave6
foreach line6 [split $tosave6 \n] {
set top7 "<tr><td style=\"text-align: left;\" class=\"left\">Win Rate</td><td style=\"text-align: right\" class=\"right\">"
set bottom7 "</td></tr>"
set tosave7 [string range $content [set f [string first $top7 $content]] [string first $bottom7 $content $f]]
regsub -all {<[^>]+>|<} $tosave7 { } tosave7
foreach line7 [split $tosave7 \n] {
set top8 "<tr><td style=\"text-align: left;\" class=\"left\">Headshots</td><td style=\"text-align: right\" class=\"right\">"
set bottom8 "</td></tr>"
set tosave8 [string range $content [set f [string first $top8 $content]] [string first $bottom8 $content $f]]
regsub -all {<[^>]+>|<} $tosave8 { } tosave8
foreach line8 [split $tosave8 \n] {
set top9 "<tr><td style=\"text-align: left;\" class=\"left\">Shots Fired</td><td style=\"text-align: right\" class=\"right\">"
set bottom9 "</td></tr>"
set tosave9 [string range $content [set f [string first $top9 $content]] [string first $bottom9 $content $f]]
regsub -all {<[^>]+>|<} $tosave9 { } tosave9
foreach line9 [split $tosave9 \n] {
putserv "PRIVMSG $chan :$line , $line2 , $line3 , $line4 , $line5 , $line6 , $line7 , $line8 , $line9"
}
}
}
}
}
}
}
}
}
}
putlog "Justdabot's MPHT Stats Script Loaded"
You asked for help reading/writing to a file. I gave that to you. If you now want a full-blown script to grab webpage data, try the request forum. I have no interest in such a script myself.. Maybe someone else will.
Scripting Help is for people who are interested in scripting - experienced scripters helping new scripters. If you just post a block of code with an accompanying request for corrections/changes/features, and it's not clear you wrote the code, then it does belong in the requests forum, so that's where this topic's been moved