Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         variable maxresult 5
  29.         setudef flag youtube
  30.         setudef int youtube_maxresult
  31.         variable key "GET-YOUR-OWN"
  32.         variable regex {(?:http(?:s|).{3}|)(?:www.|)(?:youtube.com\/watch\?.*v=|youtu.be\/)([\w-]{11})}
  33.         ::http::config -useragent "Mozilla/5.0 (X11; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0"
  34.  
  35.         proc autoinfo {nick uhost hand chan text} {
  36.             if {[channel get $chan youtube] && [regexp -nocase -- $m00nie::youtube::regex $text url id]} {
  37.                 putlog "m00nie::youtube::autoinfo is running"
  38.                 putlog "m00nie::youtube::autoinfo url is: $url and id is: $id"
  39.                 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))"
  40.                 set ids [getinfo $url]
  41.                 set title [encoding convertfrom [lindex $ids 0 1 3]]
  42.                 set pubiso [lindex $ids 0 1 1]
  43.                 regsub {\.000Z} $pubiso "" pubiso
  44.                 set pubtime [clock format [clock scan $pubiso]]
  45.                 set user [encoding convertfrom [lindex $ids 0 1 5]]
  46.                 # Yes all quite horrible...
  47.                 set isotime [lindex $ids 0 3 1]
  48.                 regsub -all {PT|S} $isotime "" isotime
  49.                 regsub -all {H|M} $isotime ":" isotime
  50.                 if { [string index $isotime end-1] == ":" } {
  51.                     set sec [string index $isotime end]
  52.                     set trim [string range $isotime 0 end-1]
  53.                     set isotime ${trim}0$sec
  54.                 } elseif { [string index $isotime 0] == "0" } {
  55.                     set isotime "stream"
  56.                 } elseif { [string index $isotime end-2] != ":" } {
  57.                     set isotime "${isotime}s"
  58.                 }
  59.                 set views [lindex $ids 0 5 1]
  60.                 puthelp "PRIVMSG $chan :\002\00301,00You\00300,04Tube\003\002 \002$title\002 by $user (duration: $isotime) on $pubtime, $views views"
  61.             }
  62.         }
  63.  
  64.         proc getinfo { url } {
  65.             for { set i 1 } { $i <= 5 } { incr i } {
  66.                 set rawpage [::http::data [::http::geturl "$url" -timeout 5000]]
  67.                 if {[string length rawpage] > 0} { break }
  68.             }
  69.             putlog "m00nie::youtube::getinfo Rawpage length is: [string length $rawpage]"
  70.             if {[string length $rawpage] == 0} { error "youtube returned ZERO no data :( or we couldnt connect properly" }
  71.             set ids [dict get [json::json2dict $rawpage] items]
  72.             putlog "m00nie::youtube::getinfo IDS are $ids"
  73.             return $ids
  74.         }
  75.  
  76.         proc search {nick uhost hand chan text} {
  77.             if {![channel get $chan youtube] } {
  78.                 return
  79.             }
  80.             if {![channel get $chan youtube_maxresult] } {
  81.                 channel set $chan youtube_maxresult $m00nie::youtube::maxresult
  82.             }
  83.             putlog "m00nie::youtube::search is running"
  84.             regsub -all {\s+} $text "%20" text
  85.             set url "https://www.googleapis.com/youtube/v3/search?part=snippet&fields=items(id(videoId),snippet(title))&key=$m00nie::youtube::key&q=$text"
  86.             set ids [getinfo $url]
  87.             set output "\002\00301,00You\00300,04Tube\003\002 "
  88.             for {set i 0} {$i < [channel get $chan youtube_maxresult]} {incr i} {
  89.                 set id [lindex $ids $i 1 1]
  90.                 set desc [encoding convertfrom [lindex $ids $i 3 1]]
  91.                 set yout "https://youtu.be/$id"
  92.                 append output "\002" $desc "\002 - " $yout " | "
  93.             }
  94.             set output [string range $output 0 end-2]
  95.             puthelp "PRIVMSG $chan :$output"
  96.         }
  97.     }
  98. }
  99. putlog "m00nie::youtube $m00nie::youtube::version loaded"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement