This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

adding throttle in m00nie's youtube tcl

Support & discussion of released scripts, and announcements of new releases.
s
simo
Revered One
Posts: 1107
Joined: Sun Mar 22, 2015 2:41 pm

adding throttle in m00nie's youtube tcl

Post by simo »

i was wondering how to integrate a throttle check in this youtube title grabber of m00nies

ive seen examples for regular scripts but never in a namespace eval

Code: Select all

#########################################################################################
# Name          m00nie::youtube
# Description       Uses youtube v3 API to search and return videos
#   
# Version       1.8 - Chanset +youtube now controls search access!
#           1.7 - Modify SSL params (fixes issues on some systems)
#           1.6 - Small correction to "stream" categorisation.....
#           1.5 - Added UTF-8 support thanks to CatboxParadox (Requires eggdrop
#               to be compiled with UTF-8 support)
#           1.4 - Correct time format and live streams gaming etc
#                   1.3 - Updated output to be RFC compliant for some IRCDs
#           1.2 - Added auto info grabber for spammed links
#          1.1 - Fixing regex!
#          1.0 - Initial release
# Website       https://www.m00nie.com
# Notes         Grab your own key @ https://developers.google.com/youtube/v3/
#########################################################################################
namespace eval m00nie {
   namespace eval youtube {
    package require http
    package require json
    package require tls
        tls::init -tls1 true -ssl2 false -ssl3 false
    http::register https 443 tls::socket
    bind pub - !yt m00nie::youtube::search
    bind pubm - * m00nie::youtube::autoinfo
    variable version "1.8"
    setudef flag youtube
    variable key "GET-YOUR-OWN"
    variable regex {(?:http(?:s|).{3}|)(?:www.|)(?:youtube.com\/watch\?.*v=|youtu.be\/)([\w-]{11})}
    ::http::config -useragent "Mozilla/5.0 (X11; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0"

proc autoinfo {nick uhost hand chan text} {
    if {[channel get $chan youtube] && [regexp -nocase -- $m00nie::youtube::regex $text url id]} {
        putlog "m00nie::youtube::autoinfo is running"
        putlog "m00nie::youtube::autoinfo url is: $url and id is: $id"
        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))"
        set ids [getinfo $url]
        set title [encoding convertfrom [lindex $ids 0 1 3]]
        set pubiso [lindex $ids 0 1 1]
        regsub {\.000Z} $pubiso "" pubiso
        set pubtime [clock format [clock scan $pubiso]]
        set user [encoding convertfrom [lindex $ids 0 1 5]]
        # Yes all quite horrible...
        set isotime [lindex $ids 0 3 1]
        regsub -all {PT|S} $isotime "" isotime
                regsub -all {H|M} $isotime ":" isotime
        if { [string index $isotime end-1] == ":" } {
            set sec [string index $isotime end]
                        set trim [string range $isotime 0 end-1]
                        set isotime ${trim}0$sec
        } elseif { [string index $isotime 0] == "0" } {
            set isotime "stream"
        } elseif { [string index $isotime end-2] != ":" } {
            set isotime "${isotime}s"
        }
        set views [lindex $ids 0 5 1]
        puthelp "PRIVMSG $chan :\002\00301,00You\00300,04Tube\003\002 \002$title\002 by $user (duration: $isotime) on $pubtime, $views views"

    }
}

proc getinfo { url } {
    for { set i 1 } { $i <= 5 } { incr i } {
            set rawpage [::http::data [::http::geturl "$url" -timeout 5000]]
            if {[string length rawpage] > 0} { break }
        }
        putlog "m00nie::youtube::getinfo Rawpage length is: [string length $rawpage]"
        if {[string length $rawpage] == 0} { error "youtube returned ZERO no data :( or we couldnt connect properly" }
        set ids [dict get [json::json2dict $rawpage] items]
    putlog "m00nie::youtube::getinfo IDS are $ids"
    return $ids

}

proc search {nick uhost hand chan text} {
        if {![channel get $chan youtube] } {
                return
        }
    putlog "m00nie::youtube::search is running"
    regsub -all {\s+} $text "%20" text
    set url "https://www.googleapis.com/youtube/v3/search?part=snippet&fields=items(id(videoId),snippet(title))&key=$m00nie::youtube::key&q=$text"
    set ids [getinfo $url]
    set output "\002\00301,00You\00300,04Tube\003\002 "
    for {set i 0} {$i < 5} {incr i} {
        set id [lindex $ids $i 1 1]
        set desc [encoding convertfrom [lindex $ids $i 3 1]]
        set desc [string map -nocase [list "&" "&" "'" "'" """ "\""] $desc ]
        set yout "https://youtu.be/$id"
        append output "\002" $desc "\002 - " $yout " | "
    }
    set output [string range $output 0 end-2]
    puthelp "PRIVMSG $chan :$output"
}
}
}
putlog "m00nie::youtube $m00nie::youtube::version loaded"


i usually use this took from member user on the forum

Code: Select all


proc throttled2 {id time} {
   global throttled
   if {[info exists throttled($id)]} {
      return 1
   } {
      set throttled($id) [utimer $time [list unset throttled($id)]]
      return 0
   }
}	
and check with if

Code: Select all


if {![throttled2 $chan:$chan 10]} { return 0 }

i altered it a bit
im not sure if this is the best and most reliable way of checking throttle channel wide and not per user
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

$chan instead of $chan:$chan in your if line will be more than enough and should work as you wanted.
Once the game is over, the king and the pawn go back in the same box.
s
simo
Revered One
Posts: 1107
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

Tnx for your reply caesar ive tried integrating it but couldn't get it to work it always returned errors i couldn't figure how to solve if i have time i will reproduce and post the errors here

Tnx.
s
simo
Revered One
Posts: 1107
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

this is how i have it atm

Code: Select all

#########################################################################################
# Name          m00nie::youtube
# Description       Uses youtube v3 API to search and return videos
#   
# Version       1.8 - Chanset +youtube now controls search access!
#           1.7 - Modify SSL params (fixes issues on some systems)
#           1.6 - Small correction to "stream" categorisation.....
#           1.5 - Added UTF-8 support thanks to CatboxParadox (Requires eggdrop
#               to be compiled with UTF-8 support)
#           1.4 - Correct time format and live streams gaming etc
#                   1.3 - Updated output to be RFC compliant for some IRCDs
#           1.2 - Added auto info grabber for spammed links
#          1.1 - Fixing regex!
#          1.0 - Initial release
# Website       https://www.m00nie.com
# Notes         Grab your own key @ https://developers.google.com/youtube/v3/
#########################################################################################
namespace eval m00nie {
   namespace eval youtube {
    package require http
    package require json
    package require tls
        tls::init -tls1 true -ssl2 false -ssl3 false
    http::register https 443 tls::socket
    bind pub - !yt m00nie::youtube::search
    bind pubm - * m00nie::youtube::autoinfo

    variable version "1.8"
    setudef flag youtube
    variable key "XXXXXXXXXXX"
    variable regex {(?:http(?:s|).{3}|)(?:www.|)(?:youtube.com\/watch\?.*v=|youtu.be\/)([\w-]{11})}
    ::http::config -useragent "Mozilla/5.0 (X11; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0"
 
 proc autoinfo {nick uhost hand chan text} {
if {![m00nie::throttled2 $chan 10]} { return 0 } 
    if {[regexp -nocase -- $m00nie::youtube::regex $text url id]} {
        putlog "m00nie::youtube::autoinfo is running"
        putlog "m00nie::youtube::autoinfo url is: $url and id is: $id"
        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))"
        set ids [getinfo $url]
        set title [encoding convertfrom [lindex $ids 0 1 3]]
        set pubiso [lindex $ids 0 1 1]
        regsub {\.000Z} $pubiso "" pubiso
        set pubtime [clock format [clock scan $pubiso]]
        set user [encoding convertfrom [lindex $ids 0 1 5]]
        # Yes all quite horrible...
        set isotime [lindex $ids 0 3 1]
        regsub -all {PT|S} $isotime "" isotime
                regsub -all {H|M} $isotime ":" isotime
        if { [string index $isotime end-1] == ":" } {
            set sec [string index $isotime end]
                        set trim [string range $isotime 0 end-1]
                        set isotime ${trim}0$sec
        } elseif { [string index $isotime 0] == "0" } {
            set isotime "stream"
        } elseif { [string index $isotime end-2] != ":" } {
            set isotime "${isotime}s"
        }
        set views [lindex $ids 0 5 1]
        #puthelp "PRIVMSG $chan :\002\00301,00You\00300,04Tube\003\002 \002$title\002 by $user (duration: $isotime) on $pubtime, $views views"
	puthelp "PRIVMSG $chan :\00301,00You\00300,04Tube\003\017 $title by $user (duration: $isotime) on $pubtime, $views views"
    }
}



proc throttled2 {id time} {
   global throttled
   if {[info exists throttled($id)]} {
      return 1
   } {
      set throttled($id) [utimer $time [list unset throttled($id)]]
      return 0
   }
}   


proc getinfo { url } {
    for { set i 1 } { $i <= 1 } { incr i } {
            set rawpage [::http::data [::http::geturl "$url" -timeout 5000]]
            if {[string length rawpage] > 0} { break }
        }
#       putlog "m00nie::youtube::getinfo Rawpage length is: [string length $rawpage]"
        if {[string length $rawpage] == 0} { error "youtube returned ZERO no data :( or we couldnt connect properly" }
        set ids [dict get [json::json2dict $rawpage] items]
#   putlog "m00nie::youtube::getinfo IDS are $ids"
    return $ids

}

proc search {nick uhost hand chan text} {
    putlog "m00nie::youtube::search is running"
    regsub -all {\s+} $text "%20" text
    set url "https://www.googleapis.com/youtube/v3/search?part=snippet&fields=items(id(videoId),snippet(title))&key=$m00nie::youtube::key&q=$text"
    set ids [getinfo $url]
    set output "\002\00301,08You\00308,05Tube\003\002\017 "
    for {set i 0} {$i < 1} {incr i} {
        set id [lindex $ids $i 1 1]
        set desc [encoding convertfrom [lindex $ids $i 3 1 ]]
        set desc [string map -nocase [list "&" "&" "'" "'" """ "\""] $desc ]
        set yout "https://youtu.be/$id"
        append output "" "" $desc " - " $yout " | "
        #append output "\002" "\0035" $desc "\002 - " \0035 $yout " | "
    }
    set output [string range $output 0 end-2]
    puthelp "NOTICE $nick :$output"
    #puthelp "PRIVMSG $chan :$output"
    } 
  }
}

putlog "m00nie::youtube $m00nie::youtube::version loaded"

only this time it doesnt throttle and no errors
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

If you look at the initial post at user's example here you will notice that you got that:

Code: Select all

if {![m00nie::throttled2 $chan 10]} { return 0 } 
wrong.
Once the game is over, the king and the pawn go back in the same box.
s
simo
Revered One
Posts: 1107
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

isnt that how u suppose to integrate that into namespace eval as im not really familiar with that
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

I meant if the throttled process returns 1 then you should 'pause' not the other way around.
Once the game is over, the king and the pawn go back in the same box.
s
simo
Revered One
Posts: 1107
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

i tried with return 1 and it didnt seem to throttle
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

I meant you should drop the ! (exclamation mark) in the if line.
Once the game is over, the king and the pawn go back in the same box.
s
simo
Revered One
Posts: 1107
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

excellent that did it tnx caesar
User avatar
m4s
Halfop
Posts: 97
Joined: Mon Jan 30, 2017 3:24 pm

The final script

Post by m4s »

Hi simo!

Can you pls share the final script here?
Thank you!
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Just remove the ! (exclamation mark) from this line:

Code: Select all

if {![m00nie::throttled2 $chan 10]} { return 0 } 
cos that's what I told him to do. :)
Once the game is over, the king and the pawn go back in the same box.
s
simo
Revered One
Posts: 1107
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

changing this:

Code: Select all

if {![m00nie::throttled2 $chan 10]} { return 0 }
 
into this:

Code: Select all

if {[m00nie::youtube::throttled2 $chan 10]} { return 0 }
and you should be good to go

Note: keep in mind this checks channel wide to throttle (not per user)

if u want to check per user u might want to do like this

Code: Select all

 
if {[m00nie::youtube::throttled2 $uhost:$chan 10]} { return 0 }
tnx again caesar.
User avatar
m4s
Halfop
Posts: 97
Joined: Mon Jan 30, 2017 3:24 pm

Post by m4s »

Thanks caesar, simo.

This throttling means the users has 10 seconds to send youtube links to the channel?
m
m00nie
Voice
Posts: 14
Joined: Sat Mar 28, 2020 2:02 pm

Post by m00nie »

Hi all 😁

I saw this and updated the script to include it as a configurable option for a per user and per channel limit. Newest version has the variables at the top
Hope it helps a little
Cheers

m00nie
Post Reply