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 

Find part of a word/url
Goto page 1, 2  Next
 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
JKM
Voice


Joined: 06 Dec 2008
Posts: 30

PostPosted: Tue Nov 03, 2009 2:07 am    Post subject: Find part of a word/url Reply with quote

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


Joined: 01 Jan 2008
Posts: 140

PostPosted: Tue Nov 03, 2009 3:20 am    Post subject: Reply with quote

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:

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:

(Tcl) % source regexmiddle.tcl
catch1: a new
catch2: an extraordinary
(Tcl) %


Im sure, there are better ways than that to do it :>
Back to top
View user's profile Send private message
JKM
Voice


Joined: 06 Dec 2008
Posts: 30

PostPosted: Tue Nov 03, 2009 5:03 am    Post subject: Reply with quote

Like this?
Code:
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.
Back to top
View user's profile Send private message
raider2k
Op


Joined: 01 Jan 2008
Posts: 140

PostPosted: Tue Nov 03, 2009 5:22 am    Post subject: Reply with quote

Code:

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


Joined: 06 Dec 2008
Posts: 30

PostPosted: Tue Nov 03, 2009 7:27 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
raider2k
Op


Joined: 01 Jan 2008
Posts: 140

PostPosted: Tue Nov 03, 2009 8:01 am    Post subject: Reply with quote

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:

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:

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


Joined: 06 Dec 2008
Posts: 30

PostPosted: Tue Nov 03, 2009 8:12 am    Post subject: Reply with quote

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:*
Back to top
View user's profile Send private message
raider2k
Op


Joined: 01 Jan 2008
Posts: 140

PostPosted: Tue Nov 03, 2009 8:22 am    Post subject: Reply with quote

sorry, dont seem to understand what you mean. what is spotify:track:? and what do you want to be done differently regarding urls?
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Tue Nov 03, 2009 3:23 pm    Post subject: Reply with quote

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:
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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
JKM
Voice


Joined: 06 Dec 2008
Posts: 30

PostPosted: Tue Nov 03, 2009 11:27 pm    Post subject: Reply with quote

Code:
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:
Quote:
[04:43] Tcl error [SpotInfo_proc]: wrong # args: should be "grab_http_data type url query arg"
And then
Quote:
[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!
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Nov 04, 2009 2:36 pm    Post subject: Reply with quote

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:
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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
JKM
Voice


Joined: 06 Dec 2008
Posts: 30

PostPosted: Wed Nov 04, 2009 4:32 pm    Post subject: Reply with quote

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

Edit: This error comes every time someone writes something.
Back to top
View user's profile Send private message
JKM
Voice


Joined: 06 Dec 2008
Posts: 30

PostPosted: Fri Nov 06, 2009 8:08 am    Post subject: Reply with quote

Anyone? Smile
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Fri Nov 06, 2009 8:37 am    Post subject: Reply with quote

Then, most likely, you've made a typo while writing the new code.
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
JKM
Voice


Joined: 06 Dec 2008
Posts: 30

PostPosted: Fri Nov 06, 2009 11:18 am    Post subject: Reply with quote

It seems to be an error in the regex line - I just copied that line from your code.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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