Twitter Search Script

By FordLawnmower on Mar 18, 2009

This script uses twitters search engine to search tweets for matches. It returns the top 3 matches along with usernames.
Setup:

Basic Syntax is !tsearch search words or @tsearch search words or /tsearch search words.
!tsearch or @tsearch or /tsearch will render no results. You must provide search words.
The !tsearch, public trigger and will notice the user. The @tsearch trigger will message the channel if you are an op. Triggers can be turned off from the right click channel menu.
Advanced Syntax:

  • Search = twitter search Result = containing both "twitter" and "search". This is the default operator.
  • Search = "happy hour" Result = containing the exact phrase "happy hour".
  • Search = love OR hate Result = containing either "love" or "hate" (or both).
  • Search = beer -root Result = containing "beer" but not "root".
  • Search = #haiku Result = containing the hashtag "haiku".
  • Search = from:FordLawnmower Result = sent from person "FordLawnmower".
  • Search = to:FordLawnmower Result = sent to person "FordLawnmower".
  • Search = @FordLawnmower Result = referencing person "FordLawnmower".
  • Search = "beer" near:"Ohio" Result = containing the exact phrase "beer" and sent near "ohio".
  • Search = near:NYC within:15mi Result = sent within 15 miles of "NYC".
  • Search = beer since:2010-12-27 Result = containing "beer" and sent since date "2010-12-27" (year-month-day).
  • Search = ftw until:2010-12-27 Result = containing "ftw" and sent up to date "2010-12-27".
  • Search = movie -scary :) Result = containing "movie", but not "scary", and with a positive attitude.
  • Search = flight :( Result = containing "flight" and with a negative attitude.
  • Search = traffic ? Result = containing "traffic" and asking a question.
  • Search = hilarious filter:links Result = containing "hilarious" and linking to URLs.
  • Search = news source:twitter Result = containing "news" and entered via Twitter
    These are just a few examples. Most of these can be combined with each other to make more accurate search results.

Screen Shot:
Image

;Twitter search by Ford_Lawnmower irc.geekshed.net #Script-Help
alias -l Gettsearch {
  if ($sslready) {
    $1-2 Searching........
    var %sockname $+(tsearch,$network,$2,$ticks)
    sockopen -e %sockname twitter.com 443
    ;The three lines below set the number of results and the colors.
    Svar %sockname maxresults 3
    Svar %sockname namecolor $+($chr(3),14,$chr(2))
    Svar %sockname textcolor $+($chr(15),$chr(3),04)
    Svar %sockname output $1-2 
    Svar %sockname url $+(/i/search/timeline?type=relevance&include_available_features=1&include_entities=1&q=,$urlencode($3-))
  }
  else $1-2 You must install openssl for this script to run: http://www.mirc.com/download/openssl/openssl-0.9.8r-setup.exe
}
menu Channel,Status {
  .$iif($group(#tsearch) == On,$style(1)) tsearch Trigger
  ..$iif($group(#tsearch) == On,$style(2)) On: .enable #tsearch
  ..$iif($group(#tsearch) == Off,$style(2)) Off: .disable #tsearch
}
#tsearch on
On $*:Text:/^(\+|-|!|@)tsearch.*/Si:#: {
  var %action $regml(1)
  if (%action isin +-) && ($regex($nick($chan,$nick).pnick,/(!|~|&|@)/)) {
    if (%action == +) {
      if ($istok(%tsearchChanList,$+($network,$chan),32)) { .msg $chan $nick $chan is already running the tsearch script }
      else { 
        .enable #tsearch
        Set %tsearchChanList $addtok(%tsearchChanList,$+($network,$chan),32)
        .msg $chan $nick has activated the tsearch script for $chan .
      }
    }
    else {
      if (!$istok(%tsearchChanList,$+($network,$chan),32)) { .msg $chan $nick $chan is not running the tsearch script }
      else { 
        Set %tsearchChanList $remtok(%tsearchChanList,$+($network,$chan),1,32)
        .msg $chan $nick has deactivated the tsearch script for $chan . 
      }
    }
  }
  elseif (!$timer($+(tsearch,$network,$nick))) && ($istok(%tsearchChanList,$+($network,$chan),32)) {
    .timer $+ $+(tsearch,$network,$nick) 1 6 noop
    var %method $iif(%action == !,.notice $nick,$iif($regex($nick($chan,$nick).pnick,/(!|~|&|@|%|\+)/),.msg $chan,.notice $nick))
    Gettsearch %method $2-
  }
}
#tsearch end
alias tsearch { Gettsearch echo -gta $1- }
On *:sockopen:tsearch*: {
  if (!$sockerr) {
    sockwrite -nt $sockname GET $svar($sockname,url) HTTP/1.0
    sockwrite -n $sockname Host: twitter.com
    sockwrite -n $sockname Connection: close
    sockwrite -n $sockname $crlf
  }
  else { 
    $svar($sockname,output) $svar($sockname,textcolor) Twitter Search is currently overloaded or ignoring your request. Please wait a few moments and try again.
    sockclose $sockname
    return 
  }
}
On *:sockread:tsearch*: {
  if ($sockerr) { 
    $svar($sockname,output) $svar($sockname,textcolor) Twitter Search is currently overloaded or ignoring your request. Please wait a few moments and try again.
    sockclose $sockname 
    return
  }
  else {
    sockread -fn 2000048 &tsearch 
    write -n $sockname $bvar(&tsearch,1-).text   
  }
}
On *:sockclose:tsearch*: {
  bread  $sockname 1 $int($calc($file($sockname).size / 1.7)) &tsearch
  .remove $sockname
  var %count 1
  ;noop $bstringmap(&tsearch,\u003c,<,\u003e,>,&lt;,<,&gt;,>,\n,$chr(32),\/,/,&quot;,",&#10;,$chr(32))
  while ($bintween(&tsearch,\u003cp class=\"js-tweet-text tweet-text\"\u003e,\u003c\/p\u003e,%count)) {
    var %match $replace($v1,\u003c,<,\u003e,>,&lt;,<,&gt;,>,\n,$chr(32),\/,/,&quot;,",&#10;,$chr(32))
    $svar($sockname,output) $+($svar($sockname,namecolor),$remove($bintween(&tsearch,js-user-profile-link js-nav\" href=\",\",%count),$chr(13),$chr(10),\/),:) $&
      $+($svar($sockname,textcolor),$httpstrip($fix&#(%match)))
    if (%count >= $svar($sockname,maxresults)) break
    inc %count
  }
  if (!$bintween(&tsearch,\u003cp class=\"js-tweet-text tweet-text\"\u003e,\u003c\/p\u003e,1)) $svar($sockname,output) $svar($sockname,textcolor) Nothing found!
  ;bwrite $sockname -1 -1 $bstringmap(&tsearch,\u003c,<,\u003e,>,&lt;,<,&gt;,>,\n,$chr(32),\/,/,&quot;,",&#10;,$chr(32))
}
;syntax $binreplace(&binvar,$textfrom,$textto)
alias -l binreplace {
  if ($bvar($1,0)) {
    if ($3) bset -t &to 1 $3
    var %start 1
    while ($bfind($1,%start,$2).text) {
      %start = $v1
      var %end $calc($v1 + $len($2))
      bcopy &left 1 $1 1 $calc(%start - 1)
      if ($bvar($1,$calc(%end + 1))) bcopy &right 1 $1 %end $bvar($1,0)
      bunset $1
      if ($bvar(&left,0)) bcopy $1 1 &left 1 $bvar(&left,0)
      if ($bvar(&to,0)) bcopy $1 $calc($bvar($1,0) + 1) &to 1 $bvar(&to,0)
      %start = $calc($bvar($1,0) + 1)
      if ($bvar(&right,0)) bcopy $1 $calc($bvar($1,0) + 1) &right 1 $bvar(&right,0)
      bunset &right
      bunset &left
    }
    return $1
  }
  return $1
}
;syntax $bstringmap(&binvar,$find,$replace,$find,$replace,$etc,$etc)
alias -l bstringmap {
  var %next 2
  while ($($+($,$calc(%next + 1)),2)) {
    binreplace $1 $($+($,%next),2) $($+($,$calc(%next + 1)),2)
    inc %next 2
  }
  return $1
}
alias -l bintween {
  var %count = 1, %mark = 0, %mark2 = 0
  while (%count <= $4) {
    if ($bfind($1, %mark2, $2).text) {
      %mark = $calc($bfind($1, %mark2, $2).text + $len($2))
      if ($bfind($1, %mark, $3).text) {
        %mark2 = $bfind($1, %mark, $3).text
      }
      else { return 0 } 
    }
    else { return 0 }
    inc %count
  }
  return $bvar($1, $iif(%mark > 0,%mark,1), $calc(%mark2 - %mark)).text
}
alias -l httpstrip { var %x, %i = $regsub($1-,/(^[^<]*>|<[^>]*>|<[^>]*$)/g,$null,%x) | return $remove($replace(%x,&amp;,&),&nbsp;,$chr(10),$chr(13),$crlf) }
alias -l Put {
  if (!$regex($1,/(\.|^)(msg|notice|echo)$/Si)) || (!$3) { echo -st **Put error** Syntax /Put msg #channel text - or - /Put notice nickname text  | return }
  tokenize 32 $regsubex($1-,/([$\|%\[\]][^\s]*)/g,$+($chr(2),$chr(2),\t))
  var %tokens $0, %Tstart 3, %Dtimer 1500
  if ($timer($+(Put,$2,$network)).secs) { %Dtimer = $calc($v1 * 1000) }  
  while ($len($($+($,%Tstart,-,%tokens),2)) > 430) {
    dec %tokens
    if ($len($($+($,%Tstart,-,%tokens),2)) <= 430) {
      .timer -m 1 %Dtimer $1-2 $($+($,%Tstart,-,%tokens),2))
      inc %Dtimer 1500
      %Tstart = $calc(%tokens + 1)
      %tokens = $0
    }
  }
  .timer -m 1 %Dtimer $1-2 $($+($,%Tstart,-,%tokens),2))
  .timer $+ $+(Put,$2,$network) -m 1 $calc(%Dtimer + 1500) noop 
}
alias -l urlencode return $regsubex($1-,/([^a-z0-9])/ig,% $+ $base($asc(\t),10,16,2))
alias -l Svar {
  var %sockname $1, %item $2
  if ($isid) {
    if ($regex($sock(%sockname).mark,/ $+ %item $+ \x01([^\x01]*)/i)) return $regml(1)
  }
  else {
    tokenize 44 $2-
    var %count 1
    while (%count <= $0) {
      var %record $($+($,%count),2), %item $gettok(%record,1,32), %value $gettok(%record,2-,32)
      if (!$regex($sock(%sockname).mark,/ $+ %item $+ \x01/i)) sockmark %sockname $+($sock(%sockname).mark,$chr(1),%item,$chr(1),%value)
      else sockmark %sockname $regsubex($sock(%sockname).mark,/( $+ %item $+ \x01[^\x01]*)/i,$+(%item,$chr(1),%value))
      inc %count
    }
  }
}
alias -l fix&# { 
  var %fix $regsubex($charMap($1-),/\&\#([0-9]{1,4});/ig,$UTF8(\t))
  return $regsubex(%fix,/\\u([0-9a-f]{4})/gi,$h2u(\t))
}
alias -l charMap {
  var %map $remove(&euro;|&nota;|&sbquo;|&fnof;|&bdquo;|&hellip;|&dagger;|&Dagger;|&circ;|&permil;|&Scaron;|&lsaquo;|&OElig;|&nota;|&nota;|&nota;|&nota;|$&
    &lsquo;|&rsquo;|&ldquo;|&rdquo;|&bull;|&ndash;|&mdash;|&tilde;|&trade;|&scaron;|&rsaquo;|&oelig;|&nota;|&nota;|&Yuml;|&nbsp;|&iexcl;|&cent;|&pound;|$&
    &curren;|&yen;|&brvbar;|&sect;|&uml;|&copy;|&ordf;|&laquo;|&not;|&shy;|&reg;|&macr;|&deg;|&plusmn;|&sup2;|&sup3;|&acute;|&micro;|&para;|&middot;|$&
    &cedil;|&sup1;|&ordm;|&raquo;|&frac14;|&frac12;|&frac34;|&iquest;|&Agrave;|&Aacute;|&Acirc;|&Atilde;|&Auml;|&Aring;|&AElig;|&Ccedil;|&Egrave;|$&
    &Eacute;|&Ecirc;|&Euml;|&Igrave;|&Iacute;|&Icirc;|&Iuml;|&ETH;|&Ntilde;|&Ograve;|&Oacute;|&Ocirc;|&Otilde;|&Ouml;|&times;|&Oslash;|&Ugrave;|$&
    &Uacute;|&Ucirc;|&Uuml;|&Yacute;|&THORN;|&szlig;|&agrave;|&aacute;|&acirc;|&atilde;|&auml;|&aring;|&aelig;|&ccedil;|&egrave;|&eacute;|&ecirc;|&euml;|$&
    &igrave;|&iacute;|&icirc;|&iuml;|&eth;|&ntilde;|&ograve;|&oacute;|&ocirc;|&otilde;|&ouml;|&divide;|&oslash;|&ugrave;|&uacute;|&ucirc;|&uuml;|&yacute;|$&
    &thorn;|&yuml;|,$chr(32))
  var %char $regsubex($str(.,128),/(.)/g,$+($chr($calc(\n + 127)),|))
  return $regsubex($1-,/( $+ %map $+ )/g,$gettok(%char,$findtokcs(%map,\t,124),124))
}
alias -l UTF8 {
  if ($version >= 7) return $chr($1)
  else {
    if ($1 < 255) { return $chr($1) }
    if ($1 >= 256) && ($1 < 2048) { return $+($chr($calc(192 + $div($1,64))),$chr($calc(128 + $mod($1,64)))) }
    if ($1 >= 2048) && ($1 < 65536) { return $+($chr($calc(224 + $div($1,4096))),$chr($calc(128 + $mod($div($1,64),64))),$chr($calc(128 + $mod($1,64)))) }
    if ($1 >= 65536) && ($1 < 2097152) {
      return $+($chr($calc(240 + $div($1,262144))),$chr($calc(128 + $mod($div($1,4096),64))),$chr($calc(128 + $mod($div($1,64),64))),$&
        $chr($calc(128 + $mod($1,64))))
    }
  }
}
alias -l div { return $int($calc($1 / $2)) }
alias -l mod {
  var %int $int($calc($1 / $2))
  return $calc($1 - (%int * $2))
}
alias -l H2U { return $utf8($base($1,16,10)) }

Comments

Sign in to comment.
^WeSt   -  Oct 11, 2013

The script is stucking the mIRC i use to break it!

Also when i an VAILD command to search and the bot starting searching then the mIRC stucks for 10-15 seconds and then output the results

mIRC Version: 7.32
OpenSSL version: 1.0.1e

(11/Oct/2013 17:07:53) <@westor> !tsearch google
(11/Oct/2013 17:07:54) -RafaeLLa-v2- Searching........

  • Break: command halted (line 97, script9.ini)

(11/Oct/2013 17:09:35) <@westor> !tsearch @google
(11/Oct/2013 17:09:35) -RafaeLLa-v2- Searching........

  • Break: command halted (line 95, script9.ini)
FordLawnmower  -  Oct 11, 2013

@^WeSt Yea, I see that the page is about 30 times larger than it was when I first wrote this.
It's way too big to parse the entire page without freezing mIRC.
I made some adjustments and removed most of the parsing. Try the updated code and let me know how it works.

^WeSt  -  Oct 12, 2013

YES!!! very much better.. now only 1 second stuck and output the message correct.

Also do you know if there is an /silent install command for openssl ?

  • Thanks!
^WeSt  -  Oct 12, 2013

The search files does not deleted, you have to uncheck the .remove into the SOCKCLOSE event to remove the search file.

.remove $sockname

FordLawnmower  -  Oct 12, 2013

Updated I had that to read the file for parsing. Fixed now.

Sign in to comment

Chromatic   -  Aug 04, 2013

....

 Respond  
FordLawnmower   -  Jul 09, 2013

Updated

 Respond  
FordLawnmower   -  Oct 29, 2011

Updated and working again.

 Respond  
FordLawnmower   -  Mar 29, 2010

Thanks montague :)

 Respond  
montague   -  Mar 29, 2010

Spectacular..! +1 like 10/10 :D

 Respond  
FordLawnmower   -  Jul 21, 2009

I rewrote most of this code. It seems to be much smoother now.

 Respond  
uneek   -  Mar 19, 2009

Yea I saw it at swiftirc last week :D

 Respond  
FordLawnmower   -  Mar 19, 2009

That's not very many variables for a socket script kirby. It can be done with less, but it's much more understandable this way. Besides they are all unset at the end ;/
I hate the use of tokenize for this, it's just not in my scripting style. I like to write code that is very easy to read and follow. Tokenize just muddy's the water, with a bunch of $* crap. I knew the menu was very basic. You are correct, your code is much more functional for the menu. By the way, I posted this several other places, some longer than a month ago. Thanks for the comments :)
Thank you uneek :)

 Respond  
uneek   -  Mar 19, 2009

Works fine for me. I've been using it for a week or so! Great work.

 Respond  
Kirby   -  Mar 18, 2009

Looks nice.
Except the screenshot is a little blurry; makes it a little hard to see. =/

I'm a little obsessed with using as few variables as possible, even though they are priceless at times, but you can certainly do so in this script.

  set %twitter.inuse 1
  set %twitter.nick $me
  set %twitter.chan ECHO
  sockclose twitter
  set %twitterurl /search?q= $+ $replace($1-,$chr(32),+)
  set %twittersite search.twitter.com
  set %twitter.max 5

Although yes, you can **/unset %twitter* at the end, but you can also use /sockmark and then /tokenize later on.
If you are not familiar with them,
/help /tokenize and /help /sockmark**.

Basically, you can store multiple items into the socket mark (up to 512 bytes, or half a kilobyte) and then tokenize them to make identifiers ($1,$2,$3,.., so on).
If done correctly, you can possibly use one (at most) variable in the whole script.

What I would also encourage is to change your menu layout slightly (the way you used groups was nice).

menu channel {
  .Twitter
  .Trigger On:enable #TwitterTrigger
  .Trigger Off:disable #TwitterTrigger
}
menu * {
  $iif($group(#TwitterTrigger) == On,$style(1)) Twitter
  .$iif($group(#TwitterTrigger) == On,$style(2)) On: .enable #TwitterTrigger
  .$iif($group(#TwitterTrigger) == Off,$style(2)) Off: .disable #TwitterTrigger
}

What I personally like about this layout is that it displays the status (if the group is enabled or disabled). If the group were to be disabled, then the 'Off' part would be grayed out, and only the 'On' part could be selected--vice-versa with if groups were to be enabled.
It also makes a 'check' mark if the group is enabled.

Otherwise, nice work.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.