Advertisement
Guest User

Untitled

a guest
Mar 14th, 2017
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 4.51 KB | None | 0 0
  1. #########################################################################################
  2. # Name          m00nie::youtube
  3. # Description       Uses youtube v3 API to search and return videos
  4. #  
  5. # Version       1.8 - Chanset +youtube now controls search access!
  6. #           1.7 - Modify SSL params (fixes issues on some systems)
  7. #           1.6 - Small correction to "stream" categorisation.....
  8. #           1.5 - Added UTF-8 support thanks to CatboxParadox (Requires eggdrop
  9. #               to be compiled with UTF-8 support)
  10. #           1.4 - Correct time format and live streams gaming etc
  11. #                   1.3 - Updated output to be RFC compliant for some IRCDs
  12. #           1.2 - Added auto info grabber for spammed links
  13. #          1.1 - Fixing regex!
  14. #          1.0 - Initial release
  15. # Website       https://www.m00nie.com
  16. # Notes         Grab your own key @ https://developers.google.com/youtube/v3/
  17. #########################################################################################
  18. namespace eval m00nie {
  19.    namespace eval youtube {
  20.     package require http
  21.     package require json
  22.     package require tls
  23.         tls::init -tls1 true -ssl2 false -ssl3 false
  24.     http::register https 443 tls::socket
  25.     bind pub - !yt m00nie::youtube::search
  26.     bind pubm - * m00nie::youtube::autoinfo
  27.     variable version "1.8"
  28.     setudef flag youtube
  29.     variable key "AIzaSyBGV5ntal09yGvWQSjTvUyWmkv-2kyxPS0"
  30.     variable regex {(?:http(?:s|).{3}|)(?:www.|)(?:youtube.com\/watch\?.*v=|youtu.be\/)([\w-]{11})}
  31.     ::http::config -useragent "Mozilla/5.0 (X11; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0"
  32.  
  33. proc autoinfo {nick uhost hand chan text} {
  34.     if {[channel get $chan youtube] && [regexp -nocase -- $m00nie::youtube::regex $text url id]} {
  35.         putlog "m00nie::youtube::autoinfo is running"
  36.         putlog "m00nie::youtube::autoinfo url is: $url and id is: $id"
  37.         set url "https://www.googleapis.com/youtube/v3/videos?id=$id&key=$m00nie::youtube::key&part=snippet,statistics,contentDetails&fields=items(snippet(title,channelTitle,publishedAt),statistics(viewCount),contentDetails(duration))"
  38.         set ids [getinfo $url]
  39.         set title [encoding convertfrom [lindex $ids 0 1 3]]
  40.         set pubiso [lindex $ids 0 1 1]
  41.         regsub {\.000Z} $pubiso "" pubiso
  42.         set pubtime [clock format [clock scan $pubiso]]
  43.         set user [encoding convertfrom [lindex $ids 0 1 5]]
  44.         # Yes all quite horrible...
  45.         set isotime [lindex $ids 0 3 1]
  46.         regsub -all {PT|S} $isotime "" isotime
  47.                 regsub -all {H|M} $isotime ":" isotime
  48.         if { [string index $isotime end-1] == ":" } {
  49.             set sec [string index $isotime end]
  50.                         set trim [string range $isotime 0 end-1]
  51.                         set isotime ${trim}0$sec
  52.         } elseif { [string index $isotime 0] == "0" } {
  53.             set isotime "stream"
  54.         } elseif { [string index $isotime end-2] != ":" } {
  55.             set isotime "${isotime}s"
  56.         }
  57.         set views [lindex $ids 0 5 1]
  58.         puthelp "PRIVMSG $chan :\002\00301,00You\00300,04Tube\003\002 \002$title\002 by $user ยค $isotime"
  59.  
  60.     }
  61. }
  62.  
  63. proc getinfo { url } {
  64.     for { set i 1 } { $i <= 5 } { incr i } {
  65.             set rawpage [::http::data [::http::geturl "$url" -timeout 5000]]
  66.             if {[string length rawpage] > 0} { break }
  67.         }
  68.         putlog "m00nie::youtube::getinfo Rawpage length is: [string length $rawpage]"
  69.         if {[string length $rawpage] == 0} { error "youtube returned ZERO no data :( or we couldnt connect properly" }
  70.         set ids [dict get [json::json2dict $rawpage] items]
  71.     putlog "m00nie::youtube::getinfo IDS are $ids"
  72.     return $ids
  73.  
  74. }
  75.  
  76. proc search {nick uhost hand chan text} {
  77.         if {![channel get $chan youtube] } {
  78.                 return
  79.         }
  80.     putlog "m00nie::youtube::search is running"
  81.     regsub -all {\s+} $text "%20" text
  82.     set url "https://www.googleapis.com/youtube/v3/search?part=snippet&fields=items(id(videoId),snippet(title))&key=$m00nie::youtube::key&q=$text"
  83.     set ids [getinfo $url]
  84.     set output "\002\00301,00You\00300,04Tube\003\002 "
  85.     for {set i 0} {$i < 5} {incr i} {
  86.         set id [lindex $ids $i 1 1]
  87.         set desc [encoding convertfrom [lindex $ids $i 3 1]]
  88.         set yout "https://youtu.be/$id"
  89.         append output "\002" $desc "\002 - " $yout " | "
  90.     }
  91.     set output [string range $output 0 end-2]
  92.     puthelp "PRIVMSG $chan :$output"
  93. }
  94. }
  95. }
  96. putlog "m00nie::youtube $m00nie::youtube::version loaded"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement