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.

Need some help on announce data

Help for those learning Tcl or writing their own scripts.
Post Reply
H
Henkie2
Voice
Posts: 34
Joined: Fri Sep 25, 2015 2:55 pm

Need some help on announce data

Post by Henkie2 »

Hi all, i'm learning some basic tcl/regexp /json and right now i'm trying to get some data from web site and it all works.
Only it announce everything to 2times., One good and one include the regexp lines.

What i'm doing wrong? (trying to get data and announce that as list to chan)
And the bind time is also not working, how can i fix that?

Can anyone help or give me some advice. Thx

Code: Select all

# -----------------------------------
# This week games from gameinformer.com
# -----------------------------------

bind pub - !newgames gameinformerprocp
bind pub - !thisweek gameinformerprocp
bind time - "00 00 * * *" gameinformerprocp

package require http
package require tls

proc gameinformerprocp {nick host hand chan txt} {
 ::http::register https 443 [list ::tls::socket -servername gameinformer.com]
 
 # Get page data
 set url "https://www.gameinformer.com/this-week" 
 set token [::http::geturl $url -timeout 1000]
 set page [::http::data $token]
 upvar 0 $token state
 if {$state(status) ne "ok"} {
 putserv "PRIVMSG $chan :\00303GAMEINFORMER:\003 $state(url) made $state(status) error"
  return
 }
 ::http::cleanup $token
 ::http::unregister https
 
 # REGEXP page
 set regex_title {hreflang="en">(.*?)</a>} 
 set titles [regexp -all -line -inline $regex_title $page]
 
 set regex_platform {class="field__item">(.+?)<\/div>}
 set platforms [regexp -all -line $regex_platform $page]
 
 set regex_date {class="datetime">(.*?)</time>}   
 set dates [regexp -all -line $regex_date $page]
 
 # Change date lay-out
 set date [clock format [clock scan $dates] -format {%d-%m-%Y}]

 # Announce to Chan
 putserv "PRIVMSG $chan :\00303GAMES:\003 This week's Game releases:"
 foreach (date $dates regex_title $titles regex_platform $platforms) {
  putserv "PRIVMSG $chan :$date $regex_title (Rating: n/a) - Platform: $regex_platform"
   }
}
User avatar
CrazyCat
Revered One
Posts: 1236
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Re: Need some help on announce data

Post by CrazyCat »

This is due to the -all flag in combination with the -inline:
When using -inline, match variables may not be specified. If used with -all, the list will be concatenated at each iteration, such that a flat list is always returned. For each match iteration, the command will append the overall match data, plus one element for each subexpression in the regular expression.
I think you may not work like that, because you'll have 3 arrays of datas which are independant.
You can probably do something like:
package require http
package require tls

proc gameinformerprocp {nick host hand chan txt} {
   ::http::register https 443 [list ::tls::socket -servername gameinformer.com]
 
   # Get page data
   set url "https://www.gameinformer.com/this-week" 
   set token [::http::geturl $url -timeout 1000]
   set page [::http::data $token]
   upvar 0 $token state
   if {$state(status) ne "ok"} {
      putserv "PRIVMSG $chan :\00303GAMEINFORMER:\003 $state(url) made $state(status) error"
      return
   }
   ::http::cleanup $token
   ::http::unregister https
   
   set blocks [regexp -all -inline -- {<h3 class="page-title">.+?<\/time>} $page]
   if {[llength $blocks]>0} {
      putserv "PRIVMSG $chan :\00303GAMES:\003 This week's Game releases:"
      foreach block $blocks {
         regexp {hreflang="en">(.+?)<\/a>.+class="field__item">(.+?)<\/div>.+?<time datetime="(.+?)"} $block -> title item date
         set date [clock format [clock scan $date] -format {%d-%m-%Y}]
         putserv "PRIVMSG $chan :$date $title (Rating: n/a) - Platform: $platform"
      }
   }
}

bind pub - !newgames gameinformerprocp
bind pub - !thisweek gameinformerprocp
bind time - "00 00 * * *" gameinformerprocp
I extract all interesting blocks (begins which <h3 class="page_title", ends at </time>) and for each, I use a regexp to extract the 3 interesting fields.
H
Henkie2
Voice
Posts: 34
Joined: Fri Sep 25, 2015 2:55 pm

Re: Need some help on announce data

Post by Henkie2 »

CrazyCat wrote: Sun Dec 24, 2023 11:05 am This is due to the -all flag in combination with the -inline:
When using -inline, match variables may not be specified. If used with -all, the list will be concatenated at each iteration, such that a flat list is always returned. For each match iteration, the command will append the overall match data, plus one element for each subexpression in the regular expression.
I think you may not work like that, because you'll have 3 arrays of datas which are independant.
You can probably do something like:
package require http
package require tls

proc gameinformerprocp {nick host hand chan txt} {
   ::http::register https 443 [list ::tls::socket -servername gameinformer.com]
 
   # Get page data
   set url "https://www.gameinformer.com/this-week" 
   set token [::http::geturl $url -timeout 1000]
   set page [::http::data $token]
   upvar 0 $token state
   if {$state(status) ne "ok"} {
      putserv "PRIVMSG $chan :\00303GAMEINFORMER:\003 $state(url) made $state(status) error"
      return
   }
   ::http::cleanup $token
   ::http::unregister https
   
   set blocks [regexp -all -inline -- {<h3 class="page-title">.+?<\/time>} $page]
   if {[llength $blocks]>0} {
      putserv "PRIVMSG $chan :\00303GAMES:\003 This week's Game releases:"
      foreach block $blocks {
         regexp {hreflang="en">(.+?)<\/a>.+class="field__item">(.+?)<\/div>.+?<time datetime="(.+?)"} $block -> title item date
         set date [clock format [clock scan $date] -format {%d-%m-%Y}]
         putserv "PRIVMSG $chan :$date $title (Rating: n/a) - Platform: $platform"
      }
   }
}

bind pub - !newgames gameinformerprocp
bind pub - !thisweek gameinformerprocp
bind time - "00 00 * * *" gameinformerprocp
I extract all interesting blocks (begins which <h3 class="page_title", ends at </time>) and for each, I use a regexp to extract the 3 interesting fields.
Ah that explains a lot, i tried to to start with "while {[regexp and use extra regsubs" is that also works like some kind of block than as well right?
And tried your one, it's not showing any data. Thx again CrazyCat, best wishes ^^
User avatar
CrazyCat
Revered One
Posts: 1236
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Re: Need some help on announce data

Post by CrazyCat »

I didn't modify your binds, but the bind time won't work, because the proc is done for binds pub.

And actually, https://www.gameinformer.com/this-week is empty, so the script can't display anything.
H
Henkie2
Voice
Posts: 34
Joined: Fri Sep 25, 2015 2:55 pm

Re: Need some help on announce data

Post by Henkie2 »

CrazyCat wrote: Tue Dec 26, 2023 12:26 pm I didn't modify your binds, but the bind time won't work, because the proc is done for binds pub.

And actually, https://www.gameinformer.com/this-week is empty, so the script can't display anything.
Ohw but how can i combine the bind time on the proc? Because i got other scripts with bind times and bind pubs and they are using same proc.
Need to set the $chan? or add min hour etc on proc? or make a extra proc?
And added extra line on if {[llength $blocks] to get a msg about empy list :D
User avatar
CrazyCat
Revered One
Posts: 1236
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Re: Need some help on announce data

Post by CrazyCat »

You have set a variable with the chan you want to announce and to create a new proc, called by your bind time, which will call your main proc itself:
set gichan "#mychan"
proc gameinformertimed {min hour day month dow} {
   ::gameinformerprocp $::botnick * "$::botnick@127.0.0.1" $::gichan ""
}
bind time - "00 00 * * *" gameinformertimed
H
Henkie2
Voice
Posts: 34
Joined: Fri Sep 25, 2015 2:55 pm

Re: Need some help on announce data

Post by Henkie2 »

@CrazyCat thanks for all your useful info en support. Much appreciated, thanks once more.
Post Reply