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