· 5 years ago · May 15, 2020, 06:38 AM
1#########################################################################################
2# Name m00nie::youtube
3# Description Uses youtube v3 API to search and return videos
4#
5# Version 2.2 - Reodering throttling to make it work better....
6# 2.1 - Adds throttling to spammed links themselves (link_throt)
7# Also includes a fix on some character output with !yt searching
8# Thanks to <AlbozZ> for this spot and fix!
9# 2.0 - Adds seperate flag for search and autoinfo (suggestion from m4s)
10# This is a change from previous versions
11# .chanset #chan +youtube = Enabled auto info grabbing on a URL spam
12# .chanset #chan +youtubesearch = Enabled access to search via !yt
13# 1.9 - Adding throttling controls (per user and per chan)
14# 1.8 - Chanset +youtube now controls search access!
15# 1.7 - Modify SSL params (fixes issues on some systems)
16# 1.6 - Small correction to "stream" categorisation.....
17# 1.5 - Added UTF-8 support thanks to CatboxParadox (Requires eggdrop
18# to be compiled with UTF-8 support)
19# 1.4 - Correct time format and live streams gaming etc
20# 1.3 - Updated output to be RFC compliant for some IRCDs
21# 1.2 - Added auto info grabber for spammed links
22# 1.1 - Fixing regex!
23# 1.0 - Initial release
24# Website https://www.m00nie.com/youtube-eggdrop-script-using-api-v3/
25# Notes Grab your own key @ https://developers.google.com/youtube/v3/
26#########################################################################################
27namespace eval m00nie {
28 namespace eval youtube {
29 # ----- CHANGE these variables -----
30 # This key is your own and shoudl remain a secret (e.g please dont email it to me! Obtain it on the link above in the notes)
31 variable key "my-clef"
32 # The two variables below control throttling in seconds. First is per user, second is per channel third is per link
33 variable user_throt 30
34 variable chan_throt 10
35 variable link_throt 300
36
37 # ---- Dont change things below this line -----
38 package require http
39 package require json
40
41 bind pub - !yt m00nie::youtube::search
42 bind pubm - * m00nie::youtube::autoinfo
43 variable version "2.2"
44 setudef flag youtube
45 setudef flag youtubesearch
46 variable regex {(?:http(?:s|).{3}|)(?:www.|)(?:youtube.com\/watch\?.*v=|youtu.be\/)([\w-]{11})}
47 ::http::config -useragent "Mozilla/5.0 (X11; CrOS x86_64 12739.105.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.158 Safari/537.36"
48 variable throttled
49
50#### Script Starts here #####
51
52proc autoinfo {nick uhost hand chan text} {
53 if {[channel get $chan youtube] && [regexp -nocase -- $m00nie::youtube::regex $text url id]} {
54 if {[throttlecheck $nick $chan $id]} { return 0 }
55 putlog "m00nie::youtube::autoinfo is running"
56 putlog "m00nie::youtube::autoinfo url is: $url and id is: $id"
57 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))"
58 set ids [getinfo $url]
59 set title [encoding convertfrom [lindex $ids 0 1 3]]
60 set pubiso [lindex $ids 0 1 1]
61 regsub {\.000Z} $pubiso "" pubiso
62 set pubtime [clock format [clock scan $pubiso]]
63 set user [encoding convertfrom [lindex $ids 0 1 5]]
64 # Yes all quite horrible...
65 set isotime [lindex $ids 0 3 1]
66 regsub -all {PT|S} $isotime "" isotime
67 regsub -all {H|M} $isotime ":" isotime
68 if { [string index $isotime end-1] == ":" } {
69 set sec [string index $isotime end]
70 set trim [string range $isotime 0 end-1]
71 set isotime ${trim}0$sec
72 } elseif { [string index $isotime 0] == "0" } {
73 set isotime "stream"
74 } elseif { [string index $isotime end-2] != ":" } {
75 set isotime "${isotime}s"
76 }
77 set views [lindex $ids 0 5 1]
78 puthelp "PRIVMSG $chan :\002\00301,00You\00300,04Tube\003\002 \002$title\002 by $user (duration: $isotime) on $pubtime, $views views"
79
80 }
81}
82
83proc throttlecheck {nick chan link} {
84 if {[info exists m00nie::youtube::throttled($link)]} {
85 putlog "m00nie::youtube::throttlecheck search term or video id: $link, is throttled at the moment"
86 return 1
87 } elseif {[info exists m00nie::youtube::throttled($chan)]} {
88 putlog "m00nie::youtube::throttlecheck Channel $chan is throttled at the moment"
89 return 1
90 } elseif {[info exists m00nie::youtube::throttled($nick)]} {
91 putlog "m00nie::youtube::throttlecheck User $nick is throttled at the moment"
92 return 1
93 } else {
94 set m00nie::youtube::throttled($nick) [utimer $m00nie::youtube::user_throt [list unset m00nie::youtube::throttled($nick)]]
95 set m00nie::youtube::throttled($chan) [utimer $m00nie::youtube::chan_throt [list unset m00nie::youtube::throttled($chan)]]
96 set m00nie::youtube::throttled($link) [utimer $m00nie::youtube::link_throt [list unset m00nie::youtube::throttled($link)]]
97 return 0
98 }
99}
100
101proc getinfo { url } {
102 if { [catch {package require tls 1.7.11}] } {
103 # We dont have an autoconfigure option for SNI
104 putlog "m00nie::youtube *** WARNING *** OLD Version of TLS package installed please update to 1.7.11+ ... "
105 http::register https 443 [list ::tls::socket -servername www.googleapis.com]
106 } else {
107 package require tls 1.7.11
108 http::register https 443 [list ::tls::socket -autoservername true]
109 }
110 for { set i 1 } { $i <= 5 } { incr i } {
111 set rawpage [::http::data [::http::geturl "$url" -timeout 5000]]
112 if {[string length rawpage] > 0} { break }
113 }
114 putlog "m00nie::youtube::getinfo Rawpage length is: [string length $rawpage]"
115 if {[string length $rawpage] == 0} { error "youtube returned ZERO no data :( or we couldnt connect properly" }
116 set ids [dict get [json::json2dict $rawpage] items]
117 ::http::unregister https
118 return $ids
119}
120
121proc search {nick uhost hand chan text} {
122 if {![channel get $chan youtubesearch] } {
123 return
124 }
125 putlog "m00nie::youtube::search is running"
126 regsub -all {\s+} $text "%20" text
127 if {[throttlecheck $nick $chan $text]} { return 0 }
128 set url "https://www.googleapis.com/youtube/v3/search?part=snippet&fields=items(id(videoId),snippet(title))&key=$m00nie::youtube::key&q=$text"
129 set ids [getinfo $url]
130 set output "\002\00301,00You\00300,04Tube\003\002 "
131 for {set i 0} {$i < 5} {incr i} {
132 set id [lindex $ids $i 1 1]
133 set desc [encoding convertfrom [lindex $ids $i 3 1]]
134 set desc [string map -nocase [list "&" "&" "'" "'" """ "\""] $desc ]
135 set yout "https://youtu.be/$id"
136 append output "\002" $desc "\002 - " $yout " | "
137 }
138 set output [string range $output 0 end-2]
139 puthelp "PRIVMSG $chan :$output"
140}
141}
142}
143putlog "m00nie::youtube $m00nie::youtube::version loaded"