This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

How do I make a script put an ID from a .txt file into a URL

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
J
Justdabomb2
Voice
Posts: 37
Joined: Fri Sep 29, 2006 7:16 pm
Location: United States of America

How do I make a script put an ID from a .txt file into a URL

Post by Justdabomb2 »

How do I make a script put an ID from a .txt file into a URL so that it will go into a specific page.

The code I have currently is below.

The web page is: http://www.nintendowifi.com/gaminghub/M ... ocale=en_U

The GSPID is: 69379673


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.

e.g. If a person gspid was 12345678, then the url would look like this - http://www.nintendowifi.com/gaminghub/M ... ocale=en_U


code:

Code: Select all

bind pub - !mphtstats show:stats

proc show:stats {nick uhost hand chan text} {
  set url "http://www.nintendowifi.com/gaminghub/MetroidHuntersLicenseFrame.jsp?gspid=69379673&lang=en&region=en_US&locale=en_U"
  set token [::http::geturl $url]
  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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Yeah!
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Is that the entire script? There's nothing there to read/write to a file.
If you want an example of reading/writing to a file:

Code: Select all

############################################################################################################################################################################
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&region=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 :P
Last edited by rosc2112 on Sun Oct 01, 2006 4:03 pm, edited 1 time in total.
J
Justdabomb2
Voice
Posts: 37
Joined: Fri Sep 29, 2006 7:16 pm
Location: United States of America

:)

Post by Justdabomb2 »

I'll try it now.
Yeah!
J
Justdabomb2
Voice
Posts: 37
Joined: Fri Sep 29, 2006 7:16 pm
Location: United States of America

Hmmmm.

Post by Justdabomb2 »

The code I used it below, but I didn't work. Could you maybe try testing and getting it to work, or maybe see if you can find a problem with mine.

The part where is saves the GSPID worked fine, but I could get that part to work before on my own, but it still wont show the stats. : :x

Code: Select all

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&region=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"
Yeah!
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

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.
J
Justdabomb2
Voice
Posts: 37
Joined: Fri Sep 29, 2006 7:16 pm
Location: United States of America

...

Post by Justdabomb2 »

I just want help, there is a problem with the script at hand.
Yeah!
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Instead of repeated nested "foreach" try regexp and lappend'ing the results into a variable, using ONE foreach.

EG:

Code: Select all

##############################################################################################################################################################################################
   foreach line $content {
       if {[regexp {<tr><td style=\"text-align: left;\" class=\"left\">(.*?)</td><td style=\"text-align: right\" class=\"right\">(.*?)</td></tr>} $content match VarName VarData]} {
             lappend MyVar "$VarName $VarData"
       }
}
The items in the regexp within (.*?) are the bits of data regexp will grab and put into vars..

Then you can manipulate the data in $MyVar in whatever way you want using lindex, lsearch, etc.
J
Justdabomb2
Voice
Posts: 37
Joined: Fri Sep 29, 2006 7:16 pm
Location: United States of America

:'(

Post by Justdabomb2 »

I have never used those so I do not know how I would use that in my script.
Yeah!
User avatar
slennox
Owner
Posts: 593
Joined: Sat Sep 22, 2001 8:00 pm
Contact:

Post by slennox »

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 :-)
J
Justdabomb2
Voice
Posts: 37
Joined: Fri Sep 29, 2006 7:16 pm
Location: United States of America

...

Post by Justdabomb2 »

Umm thanks... Hopefully someone can help me now....
Yeah!
J
Justdabomb2
Voice
Posts: 37
Joined: Fri Sep 29, 2006 7:16 pm
Location: United States of America

Post by Justdabomb2 »

anyone?
Yeah!
Post Reply