Ford

WikipediA search script for eggdrop

May 13th, 2012
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 5.56 KB | None | 0 0
  1. ##############################################################################################
  2. ##  ##     wikipedia.tcl for eggdrop by Ford_Lawnmower irc.geekshed.net #Script-Help    ##  ##
  3. ##############################################################################################
  4. ## To use this script you must set channel flag +wikipedia (ie .chanset #chan +wikipedia)   ##
  5. ##############################################################################################
  6. ##############################################################################################
  7. ##  ##                             Start Setup.                                         ##  ##
  8. ##############################################################################################
  9. ## Change wikipedia_cmdchar to the character you want to use.                               ##
  10. set wikipedia_cmdchar "!"
  11. ## Change wikipedia_lang to the 2 digit language code you want to use                       ##
  12. set wikipedia_lang "en"
  13. proc wikipedia {nick host hand chan wikipediasite wikipediaurl} {
  14.   if {[lsearch -exact [channel info $chan] +wikipedia] != -1} {
  15. ## Change the characters between the "" below to change the logo shown with each result.    ##
  16.     set wikipedialogo "\002\00301,00WikipediA\002\003"
  17. ## Change the format codes between the "" below to change the color/state of the text.      ##
  18.     set textf "\0034"
  19. ##############################################################################################
  20. ##  ##                           End Setup.                                              ## ##
  21. ############################################################################################## 
  22.     if {[catch {set wikipediaSock [socket -async $wikipediasite 80]} sockerr]} {
  23.       putserv "PRIVMSG $chan :$wikipediasite $wikipediaurl $sockerr error"
  24.       return 0
  25.       } else {
  26.       puts $wikipediaSock "GET $wikipediaurl HTTP/1.0"
  27.       puts $wikipediaSock "Host: $wikipediasite"
  28.       puts $wikipediaSock ""
  29.       flush $wikipediaSock
  30.       while {![eof $wikipediaSock]} {
  31.         set wikipediavar " [gets $wikipediaSock] "
  32.         if {[regexp {may refer to:} $wikipediavar] || [regexp {HTTP\/1\.0 403} $wikipediavar]} {
  33.            putserv "PRIVMSG $chan :\002Nothing found on WikipediA! Please refine your search and check your spelling."
  34.            close $wikipediaSock
  35.        return 0
  36.         }
  37.         if {[regexp {<b>(.*)</p>} $wikipediavar] || [regexp {<p>(.*)</p>} $wikipediavar]} {
  38.           regexp {<p>(.*)</p>} $wikipediavar match wikipediaresult
  39.           set wikipediaresult [concat [wikipediadehex [wikipediastrip $wikipediaresult]] \003\037\002http://${wikipediasite}${wikipediaurl}]
  40.       set wikipediaresult [wikitextsplit $wikipediaresult 50]
  41.           set counter 0
  42.           while {$counter <= [llength $wikipediaresult]} {
  43.         if {[lindex $wikipediaresult $counter] != ""} {
  44.           putserv "PRIVMSG $chan :${wikipedialogo} ${textf}[lindex $wikipediaresult $counter]"
  45.         }
  46.         incr counter
  47.       }
  48.       close $wikipediaSock
  49.       return 0
  50.         }
  51.       }
  52.       close $wikipediaSock
  53.       return 0
  54.     }
  55.   }
  56. }
  57. proc googlewikisearch {nick host hand chan search} {
  58.   global wikipedia_lang
  59.   if {[lsearch -exact [channel info $chan] +wikipedia] != -1} {
  60.     set googlewikisite "www.google.com"
  61.     set googlewikisearch [string map {{ } \%20} "${search}"]
  62.     set googlewikiurl "/search?q=${googlewikisearch}+site:${wikipedia_lang}.wikipedia.org&rls=${wikipedia_lang}&hl=${wikipedia_lang}"
  63.     if {[catch {set googlewikiSock [socket -async $googlewikisite 80]} sockerr]} {
  64.       putserv "PRIVMSG $chan :$googlewikisite $googlewikiurl $sockerr error"
  65.       return 0
  66.     } else {
  67.       puts $googlewikiSock "GET $googlewikiurl HTTP/1.0"
  68.       puts $googlewikiSock "Host: $googlewikisite"
  69.       puts $googlewikiSock ""
  70.       flush $googlewikiSock
  71.       while {![eof $googlewikiSock]} {
  72.         set googlewikivar " [gets $googlewikiSock] "
  73.     if {[regexp {<cite>(.*?)<\/cite>} $googlewikivar match googlewikiresult]} {
  74.           if {[regexp {wikipedia\.org} $googlewikiresult]} {
  75.             set googlewikiresult [wikipediastrip $googlewikiresult]
  76.             regexp {(.*?)\/} $googlewikiresult match wikisite
  77.             regexp {\.org(.*)} $googlewikiresult match wikiurl
  78.             wikipedia $nick $host $hand $chan $wikisite $wikiurl
  79.             close $googlewikiSock
  80.         return 0
  81.           }
  82.     }
  83.       }
  84.       putserv "PRIVMSG $chan :\002Nothing found on WikipediA! Please refine your search and check your spelling."
  85.       close $googlewikiSock
  86.       return 0
  87.     }
  88.   }
  89. }
  90. proc wikitextsplit {text limit} {
  91.   set text [split $text " "]
  92.   set tokens [llength $text]
  93.   set start 0
  94.   set return ""
  95.   while {[llength [lrange $text $start $tokens]] > $limit} {
  96.     incr tokens -1
  97.     if {[llength [lrange $text $start $tokens]] <= $limit} {
  98.       lappend return [join [lrange $text $start $tokens]]
  99.       set start [expr $tokens + 1]
  100.       set tokens [llength $text]
  101.     }
  102.   }
  103.   lappend return [join [lrange $text $start $tokens]]
  104.   return $return
  105. }
  106. proc wikipediadehex {string} {
  107.   regsub -all {[\[\]]} $string "" string
  108.   set string [subst [regsub -nocase -all {\&#([0-9]{3});} $string {[format %c \1]}]]
  109.   return [string map {\&quote; \"} $string]
  110. }
  111. proc wikipediastrip {string} {
  112.  regsub -all {<[^<>]+>} $string "" string
  113.  regsub -all {\[\d+\]} $string "" string
  114.  return $string
  115. }
  116. bind pub - [string trimleft $wikipedia_cmdchar]wiki googlewikisearch
  117. setudef flag wikipedia
  118. putlog "\002*Loaded* \002\00301,00WikipediA\002\003 \002by Ford_Lawnmower irc.GeekShed.net #Script-Help"
Advertisement
Add Comment
Please, Sign In to add comment