Advertisement
Guest User

Untitled

a guest
Jul 9th, 2019
1,109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 15.30 KB | None | 0 0
  1. # +-------------------------------------------------------------------------------------+
  2. # |                                                                                     |
  3. # |                         YouTUBE Script v2.0.0                                       |
  4. # |                                                                                     |
  5. # +-------------------------------------------------------------------------------------+
  6. # |                                                                                     |
  7. # | *** Website             @  http://www.EggdropTCL.com                                |
  8. # | *** GitHub              @  http://github.com/MadaliNTCL/eggdrop-tcl                 |
  9. # |                                                                                     |
  10. # +-------------------------------------------------------------------------------------+
  11. # | *** IRC Support:                                                                    |
  12. # |                    #EggdropTCL     @ QuakeNET                                       |
  13. # |                    #EggdropTCL     @ UnderNET                                       |
  14. # |                    #EggdropTCL     @ EfNET                                          |
  15. # |                                                                                     |
  16. # | *** Contact:                                                                        |
  17. # |                    Yahoo Messenger/Mail: madalinmen28@yahoo.com                     |
  18. # |                    Google Mail         : madalinmen28@gmail.com                     |
  19. # |                    Skype Messenger     : madalinmen28                               |
  20. # |                                                                                     |
  21. # +-------------------------------------------------------------------------------------+
  22. # + *** Commands ***                                                                    |
  23. # |     +---------------+                                                               |
  24. # |     [ OP - PUBLIC]                                                                  |
  25. # |     +---------------+                                                               |
  26. # |                                                                                     |
  27. # |     +++ !youtube on                                                                 |
  28. # |     +++ !youtube off                                                                |
  29. # |                                                                                     |
  30. # | IMPORTANT:                                                                          |
  31. # |                                                                                     |
  32. # | 500 requests per day                                                                |
  33. # | You need Google Api Key                                                             |
  34. # |                                                                                     |
  35. # | Updates:                                                                            |
  36. # | * v2                                                                                |
  37. # | -- It now catches the youtube link from /me and /ame                                |
  38. # | * minor fixes                                                                       |
  39. # | -- Add HTML::Entities for decoding Titles                                           |
  40. # | -- Convert UTF8 Titles to default codepage                                          |
  41. # +-------------------------------------------------------------------------------------+
  42.  
  43. bind PUBM - * youtube
  44. bind PUBM - * youtube:pubm
  45. bind CTCP - ACTION youtube:act
  46.  
  47. package require json
  48. package require http
  49. package require tls
  50.  
  51. namespace eval htmlentities {
  52.     namespace ensemble create -subcommands {encode decode}
  53.     variable encode {
  54.         \u0027 &#39\; \u00a0 &nbsp\; \u00a1 &iexcl\; \u00a2 &cent\; \u00a3 &pound\; \u00a4
  55.         &curren\; \u00a5 &yen\; \u00a6 &brvbar\; \u00a7 &sect\; \u00a8 &uml\;
  56.         \u00a9 &copy\; \u00aa &ordf\; \u00ab &laquo\; \u00ac &not\; \u00ad
  57.         &shy\; \u00ae &reg\; \u00af &macr\; \u00b0 &deg\; \u00b1 &plusmn\;
  58.         \u00b2 &sup2\; \u00b3 &sup3\; \u00b4 &acute\; \u00b5 &micro\; \u00b6
  59.         &para\; \u00b7 &middot\; \u00b8 &cedil\; \u00b9 &sup1\; \u00ba &ordm\;
  60.         \u00bb &raquo\; \u00bc &frac14\; \u00bd &frac12\; \u00be &frac34\;
  61.         \u00bf &iquest\; \u00c0 &Agrave\; \u00c1 &Aacute\; \u00c2 &Acirc\;
  62.         \u00c3 &Atilde\; \u00c4 &Auml\; \u00c5 &Aring\; \u00c6 &AElig\; \u00c7
  63.         &Ccedil\; \u00c8 &Egrave\; \u00c9 &Eacute\; \u00ca &Ecirc\; \u00cb
  64.         &Euml\; \u00cc &Igrave\; \u00cd &Iacute\; \u00ce &Icirc\; \u00cf
  65.         &Iuml\; \u00d0 &ETH\; \u00d1 &Ntilde\; \u00d2 &Ograve\; \u00d3
  66.         &Oacute\; \u00d4 &Ocirc\; \u00d5 &Otilde\; \u00d6 &Ouml\; \u00d7
  67.         &times\; \u00d8 &Oslash\; \u00d9 &Ugrave\; \u00da &Uacute\; \u00db
  68.         &Ucirc\; \u00dc &Uuml\; \u00dd &Yacute\; \u00de &THORN\; \u00df
  69.         &szlig\; \u00e0 &agrave\; \u00e1 &aacute\; \u00e2 &acirc\; \u00e3
  70.         &atilde\; \u00e4 &auml\; \u00e5 &aring\; \u00e6 &aelig\; \u00e7
  71.         &ccedil\; \u00e8 &egrave\; \u00e9 &eacute\; \u00ea &ecirc\; \u00eb
  72.         &euml\; \u00ec &igrave\; \u00ed &iacute\; \u00ee &icirc\; \u00ef
  73.         &iuml\; \u00f0 &eth\; \u00f1 &ntilde\; \u00f2 &ograve\; \u00f3
  74.         &oacute\; \u00f4 &ocirc\; \u00f5 &otilde\; \u00f6 &ouml\; \u00f7
  75.         &divide\; \u00f8 &oslash\; \u00f9 &ugrave\; \u00fa &uacute\; \u00fb
  76.         &ucirc\; \u00fc &uuml\; \u00fd &yacute\; \u00fe &thorn\; \u00ff &yuml\;
  77.         \u0192 &fnof\; \u0391 &Alpha\; \u0392 &Beta\; \u0393 &Gamma\; \u0394
  78.         &Delta\; \u0395 &Epsilon\; \u0396 &Zeta\; \u0397 &Eta\; \u0398 &Theta\;
  79.         \u0399 &Iota\; \u039a &Kappa\; \u039b &Lambda\; \u039c &Mu\; \u039d
  80.         &Nu\; \u039e &Xi\; \u039f &Omicron\; \u03a0 &Pi\; \u03a1 &Rho\; \u03a3
  81.         &Sigma\; \u03a4 &Tau\; \u03a5 &Upsilon\; \u03a6 &Phi\; \u03a7 &Chi\;
  82.         \u03a8 &Psi\; \u03a9 &Omega\; \u03b1 &alpha\; \u03b2 &beta\; \u03b3
  83.         &gamma\; \u03b4 &delta\; \u03b5 &epsilon\; \u03b6 &zeta\; \u03b7 &eta\;
  84.         \u03b8 &theta\; \u03b9 &iota\; \u03ba &kappa\; \u03bb &lambda\; \u03bc
  85.         &mu\; \u03bd &nu\; \u03be &xi\; \u03bf &omicron\; \u03c0 &pi\; \u03c1
  86.         &rho\; \u03c2 &sigmaf\; \u03c3 &sigma\; \u03c4 &tau\; \u03c5 &upsilon\;
  87.         \u03c6 &phi\; \u03c7 &chi\; \u03c8 &psi\; \u03c9 &omega\; \u03d1
  88.         &thetasym\; \u03d2 &upsih\; \u03d6 &piv\; \u2022 &bull\; \u2026
  89.         &hellip\; \u2032 &prime\; \u2033 &Prime\; \u203e &oline\; \u2044
  90.         &frasl\; \u2118 &weierp\; \u2111 &image\; \u211c &real\; \u2122
  91.         &trade\; \u2135 &alefsym\; \u2190 &larr\; \u2191 &uarr\; \u2192 &rarr\;
  92.         \u2193 &darr\; \u2194 &harr\; \u21b5 &crarr\; \u21d0 &lArr\; \u21d1
  93.         &uArr\; \u21d2 &rArr\; \u21d3 &dArr\; \u21d4 &hArr\; \u2200 &forall\;
  94.         \u2202 &part\; \u2203 &exist\; \u2205 &empty\; \u2207 &nabla\; \u2208
  95.         &isin\; \u2209 &notin\; \u220b &ni\; \u220f &prod\; \u2211 &sum\;
  96.         \u2212 &minus\; \u2217 &lowast\; \u221a &radic\; \u221d &prop\; \u221e
  97.         &infin\; \u2220 &ang\; \u2227 &and\; \u2228 &or\; \u2229 &cap\; \u222a
  98.         &cup\; \u222b &int\; \u2234 &there4\; \u223c &sim\; \u2245 &cong\;
  99.         \u2248 &asymp\; \u2260 &ne\; \u2261 &equiv\; \u2264 &le\; \u2265 &ge\;
  100.         \u2282 &sub\; \u2283 &sup\; \u2284 &nsub\; \u2286 &sube\; \u2287
  101.         &supe\; \u2295 &oplus\; \u2297 &otimes\; \u22a5 &perp\; \u22c5 &sdot\;
  102.         \u2308 &lceil\; \u2309 &rceil\; \u230a &lfloor\; \u230b &rfloor\;
  103.         \u2329 &lang\; \u232a &rang\; \u25ca &loz\; \u2660 &spades\; \u2663
  104.         &clubs\; \u2665 &hearts\; \u2666 &diams\; \u0022 &quot\; \u0026 &amp\;
  105.         \u003c &lt\; \u003e &gt\; \u0152 &OElig\; \u0153 &oelig\; \u0160
  106.         &Scaron\; \u0161 &scaron\; \u0178 &Yuml\; \u02c6 &circ\; \u02dc
  107.         &tilde\; \u2002 &ensp\; \u2003 &emsp\; \u2009 &thinsp\; \u200c &zwnj\;
  108.         \u200d &zwj\; \u200e &lrm\; \u200f &rlm\; \u2013 &ndash\; \u2014
  109.         &mdash\; \u2018 &lsquo\; \u2019 &rsquo\; \u201a &sbquo\; \u201c
  110.         &ldquo\; \u201d &rdquo\; \u201e &bdquo\; \u2020 &dagger\; \u2021
  111.         &Dagger\; \u2030 &permil\; \u2039 &lsaquo\; \u203a &rsaquo\; \u20ac
  112.         &euro\;
  113.     }
  114.     variable decode [lreverse $encode]
  115.     proc encode {string} {
  116.         variable encode
  117.         string map $encode $string
  118.     }
  119.     proc decode {string} {
  120.         variable decode
  121.         string map $decode $string
  122.     }
  123. }
  124.  
  125. set youtube(api) "AIzaSyDrsOKrP6_X5FIIdO9Ld4dwB_G7EH5Gtt8"
  126. set youtube(version) "2.0"
  127.  
  128. setudef flag youtube
  129.  
  130. proc youtube:pubm {nick uhost hand chan arg} {
  131.     global temp
  132.  
  133.     if {[string index $arg 0] in {! . `}} {
  134.         set temp(cmd) [string range $arg 1 end]
  135.         set temp(cmd) [lindex [split $temp(cmd)] 0]
  136.         set arg [join [lrange [split $arg] 1 end]]
  137.     } elseif {[isbotnick [lindex [split $arg] 0]]} {
  138.         set temp(cmd) [lindex [split $arg] 1]
  139.         set arg [join [lrange [split $arg] 2 end]]
  140.     } else { return 0 }
  141.  
  142.     if {[info commands ytpubm:$temp(cmd)] ne ""} { ytpubm:$temp(cmd) $nick $uhost $hand $chan $arg }
  143. }
  144.  
  145. proc ytpubm:youtube {nick uhost hand chan arg} {
  146.     global youtube tempYT
  147.  
  148.     switch -exact -- [lindex [split $arg] 0] {
  149.         on {
  150.             if {[isop $nick $chan] || [matchattr $hand n]} {
  151.                 channel set $chan +youtube
  152.  
  153.                 putserv "PRIVMSG $chan :\002$nick\002 - \00302Set channel mode \00306+youtube\0032 on \00304$chan"
  154.                 return
  155.             }
  156.         }
  157.         off {
  158.             if {[isop $nick $chan] || [matchattr $hand n]} {
  159.                 channel set $chan -youtube
  160.  
  161.                 putserv "PRIVMSG $chan :\002$nick\002 - \00302Set channel mode \00306-youtube\0032 on \00304$chan"
  162.                 return
  163.             }
  164.         }
  165.         top {}
  166.         search {
  167.             ::http::register https 443 [list ::tls::socket -tls1 1]
  168.  
  169.             if {[catch {http::geturl "https://www.googleapis.com/youtube/v3/search?[http::formatQuery key $youtube(api) maxresults 5 part id,snippet type video order viewCount q [lindex [split $arg] 1]]"} tok]} {
  170.                 putlog "Socket error: $tok"
  171.                 return 0
  172.             }
  173.             if {[http::status $tok] ne "ok"} {
  174.                 set status [http::status $tok]
  175.  
  176.                 putlog "TCP error: $status"
  177.                 return 0
  178.             }
  179.             if {[http::ncode $tok] != 200} {
  180.                 set code [http::code $tok]
  181.                 http::cleanup $tok
  182.  
  183.                 putlog "HTTP Error: $code"
  184.                 return 0
  185.             }
  186.  
  187.             set data [http::data $tok]
  188.  
  189.             set parse [::json::json2dict $data]
  190.  
  191.             set tempYT(video1) "[lindex [dict get [lindex [dict get $parse items] 0] id] 3] [lindex [dict get [lindex [dict get $parse items] 0] snippet] 5]"
  192.             set tempYT(video2) "[lindex [dict get [lindex [dict get $parse items] 1] id] 3] [lindex [dict get [lindex [dict get $parse items] 1] snippet] 5]"
  193.             set tempYT(video3) "[lindex [dict get [lindex [dict get $parse items] 2] id] 3] [lindex [dict get [lindex [dict get $parse items] 2] snippet] 5]"
  194.             set tempYT(video4) "[lindex [dict get [lindex [dict get $parse items] 3] id] 3] [lindex [dict get [lindex [dict get $parse items] 3] snippet] 5]"          
  195.             set tempYT(video5) "[lindex [dict get [lindex [dict get $parse items] 4] id] 3] [lindex [dict get [lindex [dict get $parse items] 4] snippet] 5]"
  196.            
  197.             putserv "PRIVMSG $chan :\002\00301,00You\00300,04Tube\002\017 - \00304Top 5\00302 baseado no \00303viewCount\003"
  198.  
  199.             set number 0
  200.             foreach line [array names tempYT] {
  201.                 if {[llength $line]} {
  202.                     incr number
  203.                    
  204.                     set id "[lindex $tempYT($line) 0]"
  205.                     set rawTitle "[join [lrange $tempYT($line) 1 end]]"
  206.                     set strTitle [htmlentities::decode $rawTitle]
  207.                     set title [encoding convertfrom utf-8 $strTitle]
  208.  
  209.                    
  210.                     putserv "PRIVMSG $chan :$number. \00312$title\003 \037\002/\037\002 \00302Link\003: \00304https://www.youtube.com/watch?v=$id"
  211.                 }
  212.             }
  213.         }
  214.     }
  215. }
  216.  
  217. proc youtube:act {nick uhost hand dest key arg} {
  218.     global ytignore youtube
  219.  
  220.     if {![channel get $dest youtube]} { return 0 }
  221.     if {![string match -nocase *yout* $arg]} { return 0 }
  222.  
  223.     ## ++
  224.     set floodtime 10
  225.  
  226.     ## ++
  227.     if {![info exists ytignore($nick)]} {
  228.         set ytignore($nick) [unixtime]
  229.         utimer $floodtime [list unset -nocomplain ytignore($nick)]
  230.     }
  231.  
  232.     ## ++
  233.     if {[expr [unixtime]-$ytignore($nick)]>$floodtime} { putlog "ignoram"; return 0 }
  234.  
  235.     set youtubecheck [regexp -all -nocase {(?:\/watch\?v=|youtu\.be\/)([\d\w-]{11})} $arg match youtubeid]
  236.  
  237.     youtube:parse $youtubecheck $match $youtubeid $dest
  238. }
  239.  
  240. proc youtube {nick uhost hand chan arg} {
  241.     global ytignore youtube
  242.  
  243.     if {![channel get $chan youtube]} { return 0 }
  244.     if {![string match -nocase *yout* $arg]} { return 0 }
  245.  
  246.     ## ++
  247.     set floodtime 10
  248.  
  249.     ## ++
  250.     if {![info exists ytignore($nick)]} {
  251.         set ytignore($nick) [unixtime]
  252.         utimer $floodtime [list unset -nocomplain ytignore($nick)]
  253.     }
  254.  
  255.     ## ++
  256.     if {[expr [unixtime]-$ytignore($nick)]>$floodtime} { putlog "ignoram"; return 0 }
  257.  
  258.     set youtubecheck [regexp -all -nocase {(?:\/watch\?v=|youtu\.be\/)([\d\w-]{11})} $arg match youtubeid]
  259.  
  260.     youtube:parse $youtubecheck $match $youtubeid $chan
  261. }
  262.  
  263. proc youtube:parse {youtubecheck match youtubeid dest} {
  264.     global ytignore youtube
  265.  
  266.     ::http::register https 443 [list ::tls::socket -tls1 1]
  267.  
  268.     if {[catch {http::geturl "https://www.googleapis.com/youtube/v3/videos?[http::formatQuery id $youtubeid key $youtube(api) part snippet,contentDetails,statistics,status]"} tok]} {
  269.         putlog "Socket error: $tok"
  270.         return 0
  271.     }
  272.     if {[http::status $tok] ne "ok"} {
  273.         set status [http::status $tok]
  274.  
  275.         putlog "TCP error: $status"
  276.         return 0
  277.     }
  278.     if {[http::ncode $tok] != 200} {
  279.         set code [http::code $tok]
  280.         http::cleanup $tok
  281.  
  282.         putlog "HTTP Error: $code"
  283.         return 0
  284.     }
  285.  
  286.     set data [http::data $tok]
  287.  
  288.     set parse [::json::json2dict $data]
  289.  
  290.     set playtime [lindex [dict get [lindex [dict get $parse items] 0] snippet] 1]
  291.     set rawTitle [lindex [dict get [lindex [dict get $parse items] 0] snippet] 5]
  292.     set viewCount [lindex [dict get [lindex [dict get $parse items] 0] statistics] 1]
  293.     set likeCount [lindex [dict get [lindex [dict get $parse items] 0] statistics] 3]
  294.     set dislikeCount [lindex [dict get [lindex [dict get $parse items] 0] statistics] 5]
  295.     set commentCount [lindex [dict get [lindex [dict get $parse items] 0] statistics] 9]
  296.     set publishedAt [lindex [dict get [lindex [dict get $parse items] 0] snippet] 1]
  297.     set publishedAt [string map [list "T" " " ".000Z" ""] $publishedAt]
  298.     set duration [lindex [dict get [lindex [dict get $parse items] 0] contentDetails] 1]
  299.     set duration [string map [list "PT" "" "M" "m " "S" "s"] $duration]
  300.     set definition [string toupper [lindex [dict get [lindex [dict get $parse items] 0] contentDetails] 5]]
  301.  
  302.         set strTitle [htmlentities::decode $rawTitle]
  303.         set title [encoding convertfrom utf-8 $strTitle]
  304.  
  305.     putserv "PRIVMSG $dest :\002\00301,00You\00300,04Tube\002\017 \00312$title\003 \002\00304$definition\003\002 \037\002/\002\037 \00302Views\003: \00303[youtube:convert $viewCount]\003 \037\002/\002\037 \00302Likes\003: \00310[youtube:convert $likeCount]\003 \037\002/\002\037 \00302Dislikes\003: \00304[youtube:convert $dislikeCount]\003 \037\002/\002\037 \00302Comments\003: \00304[youtube:convert $commentCount]\003 \037\002/\002\037 \00302Duration: \00304$duration\003 \037\002/\002\037 \00302Published at: \00304$publishedAt"
  306. }
  307.  
  308. proc youtube:convert {num} { while {[regsub {^([-+]?\d+)(\d\d\d)} $num "\\1.\\2" num]} {}; return $num }
  309.  
  310. putlog "++ \[ - \00304PUBLIC\003 - \00306loaded\003 * \00303YouTUBE\003 \]"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement