egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests
View previous topic :: View next topic  
Author Message
Justdabomb2
Voice


Joined: 29 Sep 2006
Posts: 37
Location: United States of America

PostPosted: Sat Sep 30, 2006 10:12 pm    Post subject: How do I make a script put an ID from a .txt file into a URL Reply with quote

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/MetroidHuntersLicenseFrame.jsp?gspid=69379673&lang=en&region=en_US&locale=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/MetroidHuntersLicenseFrame.jsp?gspid=12345678&lang=en&region=en_US&locale=en_U


code:

Code:
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!
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
rosc2112
Revered One


Joined: 19 Feb 2006
Posts: 1454
Location: Northeast Pennsylvania

PostPosted: Sun Oct 01, 2006 3:24 pm    Post subject: Reply with quote

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:

############################################################################################################################################################################
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 Razz


Last edited by rosc2112 on Sun Oct 01, 2006 4:03 pm; edited 1 time in total
Back to top
View user's profile Send private message
Justdabomb2
Voice


Joined: 29 Sep 2006
Posts: 37
Location: United States of America

PostPosted: Sun Oct 01, 2006 4:14 pm    Post subject: :) Reply with quote

I'll try it now.
_________________
Yeah!
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
Justdabomb2
Voice


Joined: 29 Sep 2006
Posts: 37
Location: United States of America

PostPosted: Sun Oct 01, 2006 4:40 pm    Post subject: Hmmmm. Reply with quote

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. : Mad

Code:
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!
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
rosc2112
Revered One


Joined: 19 Feb 2006
Posts: 1454
Location: Northeast Pennsylvania

PostPosted: Sun Oct 01, 2006 8:16 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Justdabomb2
Voice


Joined: 29 Sep 2006
Posts: 37
Location: United States of America

PostPosted: Sun Oct 01, 2006 8:17 pm    Post subject: ... Reply with quote

I just want help, there is a problem with the script at hand.
_________________
Yeah!
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
rosc2112
Revered One


Joined: 19 Feb 2006
Posts: 1454
Location: Northeast Pennsylvania

PostPosted: Sun Oct 01, 2006 8:33 pm    Post subject: Reply with quote

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

EG:
Code:

##############################################################################################################################################################################################
   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.
Back to top
View user's profile Send private message
Justdabomb2
Voice


Joined: 29 Sep 2006
Posts: 37
Location: United States of America

PostPosted: Sun Oct 01, 2006 8:37 pm    Post subject: :'( Reply with quote

I have never used those so I do not know how I would use that in my script.
_________________
Yeah!
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
slennox
Owner


Joined: 22 Sep 2001
Posts: 593

PostPosted: Sun Oct 01, 2006 8:39 pm    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message Visit poster's website
Justdabomb2
Voice


Joined: 29 Sep 2006
Posts: 37
Location: United States of America

PostPosted: Sun Oct 01, 2006 8:41 pm    Post subject: ... Reply with quote

Umm thanks... Hopefully someone can help me now....
_________________
Yeah!
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
Justdabomb2
Voice


Joined: 29 Sep 2006
Posts: 37
Location: United States of America

PostPosted: Tue Oct 03, 2006 5:19 pm    Post subject: Reply with quote

anyone?
_________________
Yeah!
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber