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.

Find part of a word/url

Help for those learning Tcl or writing their own scripts.
J
JKM
Voice
Posts: 30
Joined: Sat Dec 06, 2008 11:14 pm

Find part of a word/url

Post by JKM »

I want to use the 22 char long code behind http://open.spotify.com/track/ or spotify:track:. I've coded some small scripts, but that's only with commands. Now, I want to catch the code even if it's in the middle of a sentence.
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

not sure if I got what you are after, catching something in the middle of a sentence can be done with regex in example:

Code: Select all

set line "I have had a new experience last week"
if { [regexp -all -nocase -- {have had (.+?) exp} $line -> catch] } {
	set catch $catch
}
puts "catch1: $catch"
set line2 "I have had an extraordinary experience last week"
if { [regexp -all -nocase -- {have had (.+?) exp} $line2 -> catch2] } {
	set catch2 $catch2
}
puts "catch2: $catch2"
which will result in this:

Code: Select all

(Tcl) % source regexmiddle.tcl
catch1: a new
catch2: an extraordinary
(Tcl) % 
Im sure, there are better ways than that to do it :>
J
JKM
Voice
Posts: 30
Joined: Sat Dec 06, 2008 11:14 pm

Post by JKM »

Like this?

Code: Select all

proc SpotInfo_proc { nick uhost hand chan arg } {
  if { [regexp -all -nocase -- {http://open.spotify.com/track/(^.{22}$)} $line2 -> spotCode] } {
    set data_lines [grab_http_data 2 http://mysite/check?code= $spotCode]
  }
}

proc grab_http_data { type url query arg } {
   set user_agent "http-query.tcl <eggdrop script>"
   set tiny_token [http::config -useragent $user_agent]
   if {$type == 1} {
      set tiny_token [http::geturl $url -timeout 10000]
   } else {
      set proper_url "$url?$query=$arg"
      set tiny_token [http::geturl $proper_url -timeout 10000]
   }
   set html_data [http::data $tiny_token]
   if {$html_data != ""} {
      return $html_data
   } {
      return "error"
   }
}
Edit: I'm still wondering about:
- How can make an OR inside line 2? if { regex http://open.spotify.com... || regex spotify:track:XXX }
- What if someone is only pasting the url - in other words, not in the middle of a sentence.
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

Code: Select all

[regexp -all -nocase -- {words1 (.+?) words2} $line2 -> spotCode]
means:
(.+?) catches everything between words1 and words2 - regardless of what it is and puts it into variable spotCode. post a few examples please of what are you trying to catch (exampleurls, etc)
J
JKM
Voice
Posts: 30
Joined: Sat Dec 06, 2008 11:14 pm

Post by JKM »

http://open.spotify.com/track/75JFxkI2RXiU7L9VXzMkle
http://open.spotify.com/track/3yV98lWu83L5RZZ6RKoUg9
http://open.spotify.com/track/4OSRg1faLprPCt86C80zWt
http://open.spotify.com/track/3ou9rSNUQnE7XYmJkUUIOc
http://open.spotify.com/track/3QkNkun3dzkeGo1jpkC76S
spotify:track:75JFxkI2RXiU7L9VXzMkle
spotify:track:3yV98lWu83L5RZZ6RKoUg9
spotify:track:4OSRg1faLprPCt86C80zWt
spotify:track:3ou9rSNUQnE7XYmJkUUIOc
spotify:track:3QkNkun3dzkeGo1jpkC76S
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

so ... by looking at it the only thing you are looking for is to keep the hashcode at the end of the url, throw away the part before?

if thats correct, try the following:

Code: Select all

set urls {
	http://open.spotify.com/track/75JFxkI2RXiU7L9VXzMkle
	http://open.spotify.com/track/3yV98lWu83L5RZZ6RKoUg9
	http://open.spotify.com/track/4OSRg1faLprPCt86C80zWt
	http://open.spotify.com/track/3ou9rSNUQnE7XYmJkUUIOc
	http://open.spotify.com/track/3QkNkun3dzkeGo1jpkC76S
}
foreach listedurl $urls {
	set newurl [string map { "http://open.spotify.com/track/" "" } $listedurl]
	puts "http://mysite.com/check?code=$newurl"
}
results in the following:

Code: Select all

(Tcl) % source urltest.tcl
http://mysite.com/check?code=list
http://mysite.com/check?code=75JFxkI2RXiU7L9VXzMkle
http://mysite.com/check?code=3yV98lWu83L5RZZ6RKoUg9
http://mysite.com/check?code=4OSRg1faLprPCt86C80zWt
http://mysite.com/check?code=3ou9rSNUQnE7XYmJkUUIOc
http://mysite.com/check?code=3QkNkun3dzkeGo1jpkC76S
(Tcl) % 
string map to compare a fixed charset against $listedurl, set to "" then in this case, make it kind of disappear and only the rest of the url is being kept. not sure if thats exactly what you are looking for but thats definately a way to deal with something like that, unless the url is always a different one (then use the regexp from previous posts).
J
JKM
Voice
Posts: 30
Joined: Sat Dec 06, 2008 11:14 pm

Post by JKM »

I gave you some examples for the url, I want to fetch the code/hash from any spotify urls and uris
http://open.spotify.com/track/*
spotify:track:*
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

sorry, dont seem to understand what you mean. what is spotify:track:? and what do you want to be done differently regarding urls?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

This should do the trick, it will handle both http://open.spotify.com/track/ and spotify:track: style uri's, and will be able to catch them in both sentences and "alone".

Code: Select all

proc SpotInfo_proc {nick uhost hand chan arg} {
  if {[regexp -nocase -- {(?:http://open\.spotify\.com/track/|spotify:track:)([[:alnum:]]{22})} $arg spotify track]} {
    set data_lines [grab_http_data 2 http://mysite/check?code=${track}]
  }
}
NML_375
J
JKM
Voice
Posts: 30
Joined: Sat Dec 06, 2008 11:14 pm

Post by JKM »

Code: Select all

bind pubm -|- * SpotInfo_proc

package require http

proc SpotInfo_proc {nick uhost hand chan arg} {
  if {[regexp -nocase -- {(?:http://open\.spotify\.com/track/|spotify:track:)([[:alnum:]]{22})} $arg spotify track]} {
    set data_lines [grab_http_data 2 http://mysite.com/spotInfo.php?uri=${track}]
  }
}

proc grab_http_data { type url } {
   set user_agent "http-query.tcl <eggdrop script>"
   set tiny_token [http::config -useragent $user_agent]
   if {$type == 1} {
      set tiny_token [http::geturl $url -timeout 10000]
   } else {
      set proper_url "$url"
      set tiny_token [http::geturl $proper_url -timeout 10000]
   }
   set html_data [http::data $tiny_token]
   if {$html_data != ""} {
      return $html_data
   } {
      return "error"
   }
}

putlog "SpotInfo.tcl loaded"
It doesn't return any errors or anything. First it said:
[04:43] Tcl error [SpotInfo_proc]: wrong # args: should be "grab_http_data type url query arg"
And then
[04:49] Tcl error [SpotInfo_proc]: can't read "query": no such variable
But then I fixed the grab_http_data, but now it doesn't say anything.

Thanks for the help so far!
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Think I concatenated the url and query parameters into one, but I see you managed to adapt your grab_http_data proc to compensate.

Looking at the whole script, what you do is fetch the content of the http://mysite.com/... URI, and store this in the local variable data_lines in SpotInfo_proc. However, you do not do anything else with it, and the data is lost once the proc ends.

If you'd like to output this into the channel, you'll most likely need to use puthelp along with the appropriate irc command, ie:

Code: Select all

proc SpotInfo_proc {nick uhost hand chan arg} {
  if {[regexp -nocase -- {(?:http://open\.spotify\.com/track/|spotify:track:)([[:alnum:]]{22})} $arg spotify track]} {
    set data_lines [grab_http_data 2 http://mysite.com/spotInfo.php?uri=${track}]
    if {![string equal $data_lines "error"]} {
      foreach line [split $data_lines "\n"] {
        puthelp "NOTICE $chan :$line"
      }
    }
  }
}
You'll probably have to adjust the code to suit the actual output from the http-request, as I have no idea of the actual content.
NML_375
J
JKM
Voice
Posts: 30
Joined: Sat Dec 06, 2008 11:14 pm

Post by JKM »

[SpotInfo_proc]: wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"

Edit: This error comes every time someone writes something.
J
JKM
Voice
Posts: 30
Joined: Sat Dec 06, 2008 11:14 pm

Post by JKM »

Anyone? :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Then, most likely, you've made a typo while writing the new code.
NML_375
J
JKM
Voice
Posts: 30
Joined: Sat Dec 06, 2008 11:14 pm

Post by JKM »

It seems to be an error in the regex line - I just copied that line from your code.
Post Reply