egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

adding throttle in m00nie's youtube tcl
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Support & Releases
View previous topic :: View next topic  
Author Message
simo
Revered One


Joined: 22 Mar 2015
Posts: 1027

PostPosted: Fri Mar 13, 2020 4:18 pm    Post subject: adding throttle in m00nie's youtube tcl Reply with quote

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:

#########################################################################################
# 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 "&amp;" "&" "'" "'" "&quot;" "\""] $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:


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:


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
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3767
Location: Mint Factory

PostPosted: Sat Mar 14, 2020 4:10 am    Post subject: Reply with quote

$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.
Back to top
View user's profile Send private message
simo
Revered One


Joined: 22 Mar 2015
Posts: 1027

PostPosted: Sat Mar 14, 2020 8:42 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
simo
Revered One


Joined: 22 Mar 2015
Posts: 1027

PostPosted: Sat Mar 14, 2020 9:24 am    Post subject: Reply with quote

this is how i have it atm

Code:

#########################################################################################
# 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 "&amp;" "&" "'" "'" "&quot;" "\""] $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
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3767
Location: Mint Factory

PostPosted: Sat Mar 14, 2020 4:33 pm    Post subject: Reply with quote

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

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

wrong.
_________________
Once the game is over, the king and the pawn go back in the same box.
Back to top
View user's profile Send private message
simo
Revered One


Joined: 22 Mar 2015
Posts: 1027

PostPosted: Sat Mar 14, 2020 7:46 pm    Post subject: Reply with quote

isnt that how u suppose to integrate that into namespace eval as im not really familiar with that
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3767
Location: Mint Factory

PostPosted: Sun Mar 15, 2020 4:32 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
simo
Revered One


Joined: 22 Mar 2015
Posts: 1027

PostPosted: Sun Mar 15, 2020 8:41 am    Post subject: Reply with quote

i tried with return 1 and it didnt seem to throttle
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3767
Location: Mint Factory

PostPosted: Sun Mar 15, 2020 3:57 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
simo
Revered One


Joined: 22 Mar 2015
Posts: 1027

PostPosted: Sun Mar 15, 2020 5:00 pm    Post subject: Reply with quote

excellent that did it tnx caesar
Back to top
View user's profile Send private message
m4s
Halfop


Joined: 30 Jan 2017
Posts: 97

PostPosted: Tue Mar 17, 2020 1:52 am    Post subject: The final script Reply with quote

Hi simo!

Can you pls share the final script here?
Thank you!
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3767
Location: Mint Factory

PostPosted: Tue Mar 17, 2020 2:08 am    Post subject: Reply with quote

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

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

cos that's what I told him to do. Smile
_________________
Once the game is over, the king and the pawn go back in the same box.
Back to top
View user's profile Send private message
simo
Revered One


Joined: 22 Mar 2015
Posts: 1027

PostPosted: Tue Mar 17, 2020 1:43 pm    Post subject: Reply with quote

changing this:

Code:

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


into this:

Code:

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:
 
if {[m00nie::youtube::throttled2 $uhost:$chan 10]} { return 0 }


tnx again caesar.
Back to top
View user's profile Send private message
m4s
Halfop


Joined: 30 Jan 2017
Posts: 97

PostPosted: Wed Mar 18, 2020 2:10 pm    Post subject: Reply with quote

Thanks caesar, simo.

This throttling means the users has 10 seconds to send youtube links to the channel?
Back to top
View user's profile Send private message
m00nie
Voice


Joined: 28 Mar 2020
Posts: 14

PostPosted: Sat Mar 28, 2020 2:05 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Support & Releases All times are GMT - 4 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber