Twitter Search for Eggdrop

By FordLawnmower on Sep 18, 2010

Eggdrop script that searches Twitter for mentions of a word(s) and returns the results to the channel.
**Requires Openssl and tls

Default trigger: !tsearch
Chanset: +tsearch
Syntax: !tsearch search words here

Advanced Searches::
Normal: !tserch words here
From a User: !tsearch from:UserNameHere
To a User: !tsearch to:UserNameHere
Referencing a User: !tsearch @UserNameHere
Hashtag: !tsearch #HashTagHere
Mixed Example: !tsearch from:UserNameHere search words here

Example Output:
15:32:07 <!Ford> !tsearch mirc
15:32:09 Twitter ahora recuerdo porque deje de entrar al #Mirc by lordpatto http://twitter.com/lordpatto/statuses/24877726982
15:32:10 Twitter Agreed RT @demenece: a twitter le falta el /me. Que era uno de los features m%s divertidos del mIRC. En realidad a todos los chats les falta by martinszy http://twitter.com/martinszy/statuses/24877183504
15:32:11 Twitter curto pacarai esses memes de tuiter. "Faust'o Morreu". No tempo do mirc a gente dizia q a Hebe tinha morrido. by guicz http://twitter.com/guicz/statuses/24876869318
15:32:13 Twitter @cjorchard First it was the web then it was an API script in mIRC then a gadget in Windows 7 now TweetDeck. by Zetacon http://twitter.com/Zetacon/statuses/24876536248
Example Advanced Search:

09:36:32 <!Ford> !tsearch water from:fordlawnmower
09:36:34 Twitter The U.S. state that contains the most square miles of inland water is Alaska. by FordLawnmower http://twitter.com/FordLawnmower/statuses/24747078031
09:36:35 Twitter A giraffe can go without water longer than a camel can. by FordLawnmower http://twitter.com/FordLawnmower/statuses/24687549208
09:36:36 Twitter 2 results

Note You have to add the +tsearch flag for each channel you want this script running in. (ie. .chanset #chan +tsearch)

##############################################################################################
##  ## TwitterSearch.tcl for eggdrop by Ford_Lawnmower irc.geekshed.net #Script-Help    ##  ##
##############################################################################################
##   To use this script you must set channel flag +tsearch (ie .chanset #chan +tsearch)     ##
##############################################################################################
#package require http
package require tls
##############################################################################################
##  ##                             Start Setup.                                         ##  ##
##############################################################################################
## Change the character between the "" below to change the command character/trigger.       ##
namespace eval twittersearch {
  set twittersearchcmdchar "!"
  proc twittersearch {nick host hand chan search} {
    if {[lsearch -exact [channel info $chan] +tsearch] != -1} {
## Change the characters between the "" below to change the logo shown with each result.    ##
      set twittersearchlogo "\002\00314T\0034w\0038i\00314t\0039t\0034e\00314r\002\003"
## Change the format codes between the "" below to change the color/state of the text.      ##
      set textf "\00304"
      set authorf "\002\00314"
## Change twittersearchmax to change the maximum results returned to the channel.            ##
      set twittersearchmax 3
##############################################################################################
##  ##                           End Setup.                                              ## ##
##############################################################################################
      set twittersearchsite "twitter.com"
      if {$search == ""} { 
        putserv "PRIVMSG $chan :$twittersearchlogo ${textf} You must provide at least one search term!"
      } else {
        set twittersearch  [urlencode $search]
        set twittersearchurl "/i/search/timeline?type=relevance&include_available_features=1&include_entities=1&q=${twittersearch}"
        set twittersearchfound ""
        set tslink ""
        set tsauthor ""
        set tstitle ""
        set tscount 1
        if {[catch {set twittersearchsock [tls::socket $twittersearchsite 443]} sockerr]} {
          putlog "$twittersearchsite $twittersearchurl $sockerr error"
          return 0
        } else { 
          puts $twittersearchsock "GET $twittersearchurl HTTP/1.0"
          puts $twittersearchsock "Host: $twittersearchsite"
          puts $twittersearchsock ""
          flush $twittersearchsock   
          while {![eof $twittersearchsock]} {
            set twittersearchvar " [gets $twittersearchsock] "        
            set twittersearchdata [string map {\\u003c < \\u003e > &lt; < &gt; > \\n "" \\\/ / &quot; \" &#10; " " [ \\[ ] \\] $ \\$} $twittersearchvar]
            set twittersearchresults [regexp -all -inline {<p\sclass=\\"js-tweet-text\stweet-text\\">(.*?)<\/p>} $twittersearchdata]
            regsub -all {<.*?>} $twittersearchresults {} twittersearchresults
            set twittersearchusers [regexp -all -inline {js-user-profile-link\sjs-nav\\"\shref=\\"\/(.*?)\\"} $twittersearchdata]
            set count 1
            set counter 1
            while {$counter <= $twittersearchmax && [lindex $twittersearchusers $count] != ""} {
              putchan $chan "${authorf}[recode [lindex $twittersearchusers $count]]: ${textf}[recode [lindex $twittersearchresults $count]]"
              incr counter
              incr count 2
            }
          }
      close $twittersearchsock
          return 0
        }
      }
    }
  }
  proc urlencode {string} {
    return [subst [regsub -nocase -all {([^a-z0-9])} $string {%[format %x [scan "\\&" %c]]}]]
  }
  proc recode { textin } {
    return [string map {&quot; \" &middot; · &amp; & &nbsp; ""} [subst [regsub -nocase -all {&#([0-9]{1,5});} $textin {\u\1}]]]
  }
  proc noop {} { return 1 }
  bind pub - ${twittersearchcmdchar}tsearch twittersearch::twittersearch
  setudef flag tsearch
  putlog "TwitterSearch Script by Ford_Lawnmower successfully loaded!"
}  

Comments

Sign in to comment.
GravitaZ   -  Oct 26, 2018

Hi Sir, I know its a slim chance, but if you are still working on these I was trying to set up my bot again. The search results were only showing the username and not the actual tweets. Was wondering if you can check it out.

 Respond  
GravitaZ2   -  Nov 29, 2014

Doesn't seem to be working :/
Maybe I'm doing something wrong
[13:56:32] Tcl error [twittersearch::twittersearch]: error flushing "sock16997120": software caused connection abort
[13:56:50] Tcl error [twittersearch::twittersearch]: error flushing "sock169fb890": software caused connection abort

 Respond  
FordLawnmower   -  Apr 18, 2014

Updated

 Respond  
prashant179   -  Jan 14, 2014

Hello Mr. FordLawnmower this Script Proper Install and no error. I follow your every step but i try use Syntax i get no result.

I use All Syntax:

~Normal: !tserch words here
~From a User: !tsearch from:UserNameHere
~To a User: !tsearch to:UserNameHere
~Referencing a User: !tsearch @UserNameHere
~Hashtag: !tsearch #HashTagHere
~Mixed Example: !tsearch from:UserNameHere search words here

No result Show. IDK what the Problem why i not get Search Result.

 Respond  
Libbard   -  Jul 02, 2013

it won't search anything :(

could you see what the problem :(

Thanks Ford for all your work.. you are the master :)

Regards,

Libbard

 Respond  
itskamel   -  Oct 30, 2011

thank you so much, just tested and its working.

 Respond  
FordLawnmower   -  Oct 30, 2011

Updated and working again :)

 Respond  
itskamel   -  Oct 28, 2011

hey thanks for the reply i will check back. i was thinking it was on my end. :)
post back and let me know. looks like it will be awesome. good job anyway.

 Respond  
FordLawnmower   -  Oct 28, 2011

It's broke itskamel . I will post an update later today.

 Respond  
itskamel   -  Oct 28, 2011

everything i do it tells me 0 results. :/
any sugestions?

 Respond  
FordLawnmower   -  Sep 19, 2010

Added advanced search description.

Advanced Searches::
Normal: !tserch words here
From a User: !tsearch from:UserNameHere
To a User: !tsearch to:UserNameHere
Referencing a User: !tsearch @UserNameHere
Hashtag: !tsearch #HashTagHere
Mixed Example: !tsearch from:UserNameHere search words here

Example Advanced Search:

09:36:32 <!Ford> !tsearch water from:fordlawnmower
09:36:34 Twitter The U.S. state that contains the most square miles of inland water is Alaska. by FordLawnmower http://twitter.com/FordLawnmower/statuses/24747078031
09:36:35 Twitter A giraffe can go without water longer than a camel can. by FordLawnmower http://twitter.com/FordLawnmower/statuses/24687549208
09:36:36 Twitter 2 results

 Respond  
FordLawnmower   -  Sep 19, 2010

Thanks Raz :)
Thanks SkyBytes :)
I'm going to expand this a little bit for more advanced search results.

 Respond  
SkyBytes   -  Sep 19, 2010

I agree with RaZ.

 Respond  
RaZ   -  Sep 19, 2010

oww Very Nice ))

 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.