Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##
- ## AHLGOALBOT
- ##
- use Date::Parse;
- use Mojo::DOM;
- use Time::Piece;
- use Time::HiRes qw( sleep );
- use Data::Dumper;
- use feature "switch";
- use Irssi qw(command_bind signal_add timeout_remove timeout_add);
- use JSON;
- #use strict;
- use warnings;
- use constant {
- owner => "grateful",
- DEBUG => 1,
- BOT_OWNER_PASSWORD => "uticacomets",
- FOLLOW_PERIOD => 5000,
- };
- my $httpref;
- my $t;
- my $fp;
- my $h;
- my $Home;
- my $Away;
- use constant UA => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.26 Safari/537.36";
- my $presentgoallist;
- my $tag;
- my %follow;
- my %recentgoal;
- my $periodold;
- # initally owner's mask impossible to match
- my $bot_owner = '$@$';
- sub wget {
- my( $url, $extra ) = @_;
- my $UA = UA;
- return `LD_PRELOAD='' wget -t 3 -T 3 --no-proxy -q -O - '$url' -U '$UA' $extra 2>/dev/null`;
- }
- sub curlhead {
- my $url = shift;
- return `LD_PRELOAD='' curl -I '$url' 2>/dev/null`;
- }
- sub lget {
- my $extra = $httpref ? "-http.fake-referer '$httpref' -http.referer 2" : "";
- $_ = shift;
- $_ = `LD_PRELOAD='' links -receive-timeout 3 -unrestartable-receive-timeout 10 $extra -source '$_' 2>/dev/null | gunzip -f 2>/dev/null`;
- $_ = "" if( length($_) < 200 && /404|Not Found/i );
- return $_;
- }
- sub links {
- $_ = shift;
- return `LD_PRELOAD='' links -width 400 -receive-timeout 3 -dump '$_' 2>/dev/null`;
- }
- sub shorturl{
- my $url = shift;
- $_ = wget( 'https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyDVHP4V0bz7PZKwdpEitqwD5tlY03z-OYE', "--header 'Content-Type: application/json' --post-data='{\"longUrl\": \"$url\"}'" );
- return /"id":\s*"(http.*?)"/ ? $1 : $url;
- }
- our %httpcache;
- sub download {
- no warnings;
- my( $url, $nocache ) = @_;
- $url =~ s/'/'"'"'/g;
- return lget( $url ) if( $nocache );
- my $t = time;
- for my $c ( 1 .. 5 ) {
- if( $t > $httpcache{$c}{timer} && $httpcache{$c}{url} ) {
- $httpcache{$c}{url} = "";
- $httpcache{$c}{data} = "";
- $httpcache{$c}{timer} = 0;
- }
- }
- for my $c ( 1 .. 5 ) {
- if( $httpcache{$c}{url} eq $url ) {
- $httpcache{hits}++;
- return $httpcache{$c}{data};
- }
- }
- my $tmp = lget( $url );
- if( $tmp ) {
- $httpcache{index}++;
- $httpcache{index} = 1 if( $httpcache{index} > 5 );
- $httpcache{$httpcache{index}}{url} = $url;
- $httpcache{$httpcache{index}}{data} = $tmp;
- $httpcache{$httpcache{index}}{timer} = time + 300;
- }
- return $tmp;
- }
- sub GetDate {
- my( $date, $fmt ) = @_;
- $date =~ s/'//g;
- my( $ret ) = `date --date='$date' "+$fmt" 2>&1` =~ /(.*)/;
- return $ret;
- }
- require Irssi;
- Irssi::signal_add("event privmsg", "irssi_pre");
- sub irssi_pre {
- my ($shash, $ircdata, $nick, $mask) = @_;
- $ircdata =~ /(.*?) :(.*)/;
- %h = ( shash => $shash, nick => $nick, target => $1, text => $2, host => $mask );
- # don't process if target is not a channel, unless is "identify" private message from (perhaps) an owner
- if (!$shash->ischannel($h{target})) {
- if ($h{text} eq "identify ".BOT_OWNER_PASSWORD) {
- ahldebug("bot owner identified from: ".$h{nick}."!".$h{host});
- $bot_owner = $h{host};
- $shash->command("^notice ".$h{nick}." you are now identified as my owner");
- }
- return;
- }
- # for rud0lf's test purposes - work only on freenode/#ahltest
- # comment out if you want to run it everywhere
- # return if (lc($h{target}) ne "#ahltest" || lc($shash->{tag}) ne "freenode");
- my $t = ".";
- return if(
- $shash->ignore_check($nick, $mask, $h{target},$h{text}, MSGLEVEL_PUBLIC) ||
- $h{text} !~ /${t}\w/
- );
- $h{server} = $shash->{tag};
- $h{server_obj} = $shash;
- my $channel = $shash->channel_find( $h{target} );
- if ( $shash->ischannel( $h{target} ) ) {
- if (defined $fp{"$h{target}$h{server}"}) {
- return if( $fp{"$h{target}$h{server}"} > time );
- }
- } else {
- if(defined $fp{"priv$h{server}"} ) {
- return if( $fp{"priv$h{server}"} > time );
- }
- $fp{"priv$h{server}"} = time + 1;
- $h{target} = $nick;
- }
- event_privmsg( \%h );
- }
- ##cooldown to prevent flooding.
- my $not_until = 0;
- use constant COOLDOWN => 3;
- sub event_privmsg {
- my $params = shift;
- %h = %$params;
- my $noticereply;
- my @ret;
- my( $target, $text, $scorebot, $goalbot ) = ( $h{target}, $h{text}, $h{scorebot}, $h{goalbot} );
- my( $address ) = $h{host};
- my $t = qr/^\./;
- return if( $text !~ $t );
- return if ($not_until > time());
- $not_until = time() + COOLDOWN;
- foreach( lc( $text ) ) {
- if ( /^${t}followahl (\w.*)/ ) {
- my $string = $1;
- # get game id
- my $result = get_match_id($string);
- if (!defined($result)) {
- $h{shash}->command("msg $target $string: Game not found.");
- return;
- }
- my %data = %$result;
- # list of follows will be stored in hash "$follow" under key "server_tag;channel_name;game_id"
- # i.e. "freenode;#ahl;10133789"
- my $key = $h{server} . ";" . $target . ";" . $data{gameid};
- my $userhost = $h{host};
- if (defined($follow{$key})) {
- if (defined($follow{$key}{users}{$userhost})) {
- $h{shash}->command("msg $target You're already following this game. (".$data{shortname}.")");
- return;
- }
- }
- $follow{$key}{fullname} = $data{fullname};
- # don't add periodic info if another user is already watching this game on this server and channel,
- # just add the fact that another user is watching it to the hash
- if (!scalar(keys %{ $follow{$key}{users} } )) {
- $recentgoal{$key} = "-1";
- $follow{$key}{shortname} = $data{shortname};
- $follow{$key}{tag} = timeout_add(FOLLOW_PERIOD, "FollowAHL", $key);
- }
- else {
- ahldebug("Users already exists.");
- }
- $follow{$key}{users}{$userhost} = 1;
- $h{shash}->command("msg $target Now following ".$data{fullname});
- $h{shash}->command("msg $target ".$data{radiocast});
- FollowAHL($key);
- }
- if ( /^${t}listahl/ ) {
- ahllist(\%h);
- }
- if ( /^${t}unfollowahl (\w.*)/ ) {
- my $string = $1;
- # get game id
- my $result = get_match_id($string);
- if (!defined($result)) {
- $h{shash}->command("msg $target $string: Game not found.");
- return;
- }
- my %data = %$result;
- my $key = $h{server} . ";" . $target . ";" . $data{gameid};
- my $userhost = $h{host};
- if (defined($follow{$key})) {
- if (!defined($follow{$key}{users}{$userhost})) {
- if ($userhost eq $bot_owner) {
- $h{shash}->command("msg $target Bot owner ".$h{nick}." removed game watch ".$follow{$key}{shortname});
- timeout_remove($follow{$key}{tag});
- delete $follow{$key};
- delete $recentgoal{$key};
- return;
- }
- my $isop = 0;
- my $chan = $h{shash}->channel_find($target);
- if (defined $chan) {
- my $nick = $chan->nick_find($h{nick});
- if (defined $nick) {
- $isop = $nick{op};
- }
- }
- if ($isop) {
- $h{shash}->command("msg $target Chanop ".$h{nick}." removed game watch ".$follow{$key}{shortname});
- timeout_remove($follow{$key}{tag});
- delete $follow{$key};
- delete $recentgoal{$key};
- return;
- }
- }
- if (defined($follow{$key}{users}{$userhost})) {
- if (!$follow{$key}{users}{$userhost}) {
- # channel operator and bot owner can remove whole game watch (purge all user list)
- $h{shash}->command("msg $target You didn't follow this game.");
- return;
- }
- else {
- delete $follow{$key}{users}{$userhost};
- if (scalar(keys %{ $follow{$key}{users} } )) {
- $h{shash}->command("msg $target No longer following your game, but another user still is. (".$follow{$key}{shortname}.")");
- return;
- }
- else {
- $h{shash}->command("msg $target Not following ".$follow{$key}{shortname}." any more.");
- timeout_remove($follow{$key}{tag});
- delete $follow{$key};
- delete $recentgoal{$key};
- return;
- }
- }
- }
- }
- if (defined($follow{$key})) {
- $h{shash}->command("msg $target You didn't follow ".$follow{$key}{shortname}." and you have no rights to remove it from someone else.");
- }
- else {
- $h{shash}->command("msg $target You didn't follow ".$data{shortname}.".");
- }
- }
- }
- }
- sub FollowAHL {
- my $key = shift;
- my @goallist;
- # ahldebug("FollowAHL...");
- my ($servertag, $channel, $gameid) = split(/;/, $key);
- my $server = Irssi::server_find_tag($servertag);
- # not connected to server with tag stored in the key - don't go any further
- return unless (defined $server);
- # not on channel stored in the key - don't proceed
- return unless ($server->ischannel($channel));
- ##$ The below will pull the @goallist, what we will need to compare.
- my $url = 'https://lscluster.hockeytech.com/game_reports/official-game-report.php?client_code=ahl&game_id=' . $gameid;
- my $data = download( $url, 1 );
- if ($data eq "No such game") {
- #something went wrong
- ahldebug("Game not found on server, removing timeout (game ID: $gameid full name: ".$follow{$key}{fullname}.")");
- timeout_remove($follow{$key}{tag});
- delete $follow{$key};
- delete $recentgoal{$key};
- return;
- }
- my @period;
- my @jsinfolist;
- my @goals = ();
- my $url2 = "http://cluster.leaguestat.com/lsconsole/json.php?client_code=ahl";
- my $data2 = download( $url2 );
- my ($jsondata) = ( $data2 =~ /"ID":"$gameid".(.*?)}\s*}\s*,/gs);
- my (@jsondata) = ( $jsondata =~ /".*?":"(.*?)"/gs);
- foreach ($jsondata[1]) {
- my ($Number, $Letter, $Label, $Date, $Att, $Time, $Zone, $Ticket, $GameSummary, $GameReport, $PlayerReport, $TextReport, $Venue, $Status, $ShortStatus, $SmallStatus, $StatusID, $Clock, $Period, $SchedNote, $GameNote, $AID, $Away, $AwayCode, $AwayUrl, $AwayVidUrl, $AwayWebCast, $AwayScore, $AShootOut, $HID, $Home, $HomeCode, $HomeUrl, $HomeVidUrl, $HomeWebCast, $HomeScore, $HShootOut) = (@jsondata);
- $HomeUrl =~ s/\\//sg;
- $AwayUrl =~ s/\\//sg;
- $Home =~ s/\\//gs;
- $Away =~ s/\\//gs;
- push @jsinfolist, "$Away $AwayScore @ $Home $HomeScore";
- my $period = "$Status";
- print "@jsinfolist - S $Status | SM $SmallStatus | ID $StatusID | C $Clock | P $Period";
- }
- my ($shotstable) = ( $data =~ /SHOTS(.*?)<\/tbody>/s );
- my (@shotsonnet) = ( $shotstable =~ /<tr.*?>(.*?)<\/tr>/gs ) if ($shotstable);
- my @SOG;
- my @SOGT;
- foreach (@shotsonnet) {
- my (@SOG) = ( /<td.*?>(.*?)<\/td>/gs);
- push @SOGT, "$SOG[-1]";
- }
- my ($gameend) = ( $data =~ /Game End/s ); # every "/" inside needs "\"
- my ($scoringtable) = ( $data =~ /tSides">(.*?)<\/table>/s );
- if (defined $scoringtable) {
- my (@goals) = ( $scoringtable =~ /<tr.*?>(.*?)<\/tr>/gs );
- my $T1;
- my $T2;
- foreach (@goals) {
- my ($GVH, $GNum, $GPer, $GTeam, $GTime, $GGoal, $GAssists, $GType) = ( /<td.*?>(.*?)<\/td>/gs);
- my ($T1, $T2) = $GVH =~ /\d/g; if ($T1 > $T2) { $GVH = "\x02$T1\x02-$T2"; } elsif ($T2 > $T1) { $GVH = "$T1-\x02$T2\x02"; } else { $GVH = "$T1-$T2"; }
- push @goallist, "\x02$GTeam $GGoal\x02 $GAssists $GType ($GTime $GPer) [$GVH]";
- }
- }
- shift @goallist;
- ### used to announce period changes/starting/end game ####
- my ($gamestatus) = ( $data =~ /Game Status.*?<\/b><\/td><td valign="top">(.*?)<\/td><\/tr>/s);
- if ($gameend) { $gamestatus = "FINAL"; }
- ### tidy up the goalist
- s/ //gs for @goallist;
- tr/a-z/A-Z/ for @goallist;
- s/ / /g for @goallist;
- ahldebug("goallist: ".join("/",@goallist));
- my $lastgoal = $recentgoal{$key};
- if ($lastgoal < $#goallist) {
- for my $i ($#goallist) {
- $server->command("msg $channel \x02AHL GOAL\x02: ".$goallist[$i]);
- }
- $recentgoal{$key} = $#goallist;
- }
- if (defined $period eq "In Progress (0:00 remaining in 1st)" && (!$periodold)) {
- $server->command("msg $channel \x02AHL Game 1st Period Over!\x02 @jsinfolist");
- $periodold = $period;
- print "$period";
- } elsif (defined $period eq "In Progress (0:00 remaining in 2nd)" && (!$periodold)) {
- $server->command("msg $channel \x02AHL Game 2nd Period Over!\x02 @jsinfolist");
- $periodold = $period;
- }
- if (defined $gamestatus && $gamestatus eq "FINAL") {
- ahldebug("@jsinfolist Final - Timer has been removed.");
- $server->command("msg $channel \x02AHL Game has finished!\x02 @jsinfolist Final | SOG " . $SOGT[0] . "-" . $SOGT[1]);
- timeout_remove($follow{$key}{tag});
- delete $follow{$key};
- delete $recentgoal{$key};
- }
- }
- # return match_id for HomeCode Home AwayCode Away ID and other data or undef if not found
- sub get_match_id {
- my $tomatch = shift;
- my $date = GetDate( 'today', '%Y-%m-%d' );
- # my $url = "http://www.tanknation.xyz/test.json";
- my $url = "http://cluster.leaguestat.com/lsconsole/json.php?client_code=ahl&forcedate=$date";
- ahldebug($url);
- my $data = download( $url, 1 );
- my $GameStatus;
- my @gsinfo;
- ( $data ) = ( $data =~ /var todayData = '(.*?)'\;/gs );
- my ( @games ) = ( $data =~ /Game.*?"\:\{(.*?)\}\}/gs );
- foreach (@games) {
- my ($ID, $Number, $Letter, $Label, $Date, $Time, $Zone, $Status, $ShortStatus, $SmallStatus, $StatusID, $Clock, $Period, $Away, $AwayCode, $AwayUrl, $AwayScore, $Home, $HomeCode, $HomeUrl, $HomeScore) = ( /"ID"\:"(.*?)".*?Number"\:"(.*?)".*?Letter"\:"(.*?)".*?Label"\:"(.*?)".*?Date"\:"(.*?)".*?ScheduledTime"\:"(.*?)".*?Timezone"\:"(.*?)".*?Status"\:"(.*?)".*?ShortStatus"\:"(.*?)".*?SmallStatus"\:"(.*?)".*?StatusID"\:"(.*?)".*?GameClock"\:"(.*?)".*?"Period"\:"(.*?)".*?Name"\:"(.*?)".*?Code"\:"(.*?)".*?AudioUrl"\:"(.*?)".*?Score"\:"(.*?)".*?Name"\:"(.*?)".*?Code"\:"(.*?)".*?AudioUrl"\:"(.*?)".*?Score"\:"(.*?)"/gs );
- ### Change this to have both the string to search and the game_id, this'll make things easier later. you can also add in anything else from above if you need it.
- push @gsinfo, {game_id => $ID, search_string => "$HomeCode $Home $AwayCode $Away $ID"};
- }
- ### find all the games that matchl, and just get their game ids
- my @matched_games = map {$_->{game_id}} grep {$_->{search_string} =~ /$tomatch/i} @gsinfo;
- ### We'll just get the first game
- my $game = $matched_games[0];
- # return undef (nothing) if $game is empty
- return if (!$game);
- my ($jsondata) = ( $data =~ /"ID":"$game".(.*?)}\s*}\s*/gs);
- my (@jsondatalist) = ( $jsondata =~ /".*?":"(.*?)"/gs);
- my ($Number, $Letter, $Label, $Date, $Att, $Time, $Zone, $Ticket, $GameSummary, $GameReport, $PlayerReport, $TextReport, $Venue, $Status,
- $ShortStatus, $SmallStatus, $StatusID, $Clock, $Period, $SchedNote, $GameNote, $AID, $Away, $AwayCode, $AwayUrl, $AwayVidUrl, $AwayWebCast, $AwayScore,
- $AShootOut, $HID, $Home, $HomeCode, $HomeUrl, $HomeVidUrl, $HomeWebCast, $HomeScore, $HShootOut) = (@jsondatalist);
- $Date = GetDate( $Date, '%b %e, %Y' );
- my $TEAMSTOFOLLOW = "$Away vs $Home - $Time $Zone @ $Venue";
- my $shortname = "$Away vs $Home";
- # ahldebug("TEAMSTOFOLLOW: ".$TEAMSTOFOLLOW);
- $HomeUrl =~ s/\\//sg;
- $AwayUrl =~ s/\\//sg;
- my $RADIOCAST = "\x02Home Radio:\x02 $HomeUrl | \x02Away Radio:\x02 $AwayUrl ";
- my %return = ( gameid => $game, fullname => $TEAMSTOFOLLOW, shortname => $shortname, radiocast => $RADIOCAST);
- return \%return;
- }
- sub ahldebug {
- return unless DEBUG;
- my $txt = join(" ", @_);
- Irssi::print("[AHL Debug] ".$txt, MSGLEVEL_CLIENTCRAP);
- }
- sub ahllist {
- my $param = shift;
- my %h = %$param;
- my $channel_name = $h{target};
- my $server = $h{shash};
- my $server_tag = $server->{tag};
- my $channel = $server->channel_find($channel_name);
- my @result = ();
- foreach my $key (keys %follow) {
- next unless ($key =~ /^${server_tag};${channel_name}.*$/);
- my $game_name = $follow{$key}{shortname};
- foreach my $user (keys % { $follow{$key}{users} }) {
- my $username = $user;
- if (defined $channel) {
- # ahldebug($channel->{name});
- my $snick = $channel->nick_find_mask("*!".$username);
- if (defined $snick) {
- $username = $snick->{nick};
- }
- }
- push @result, "$game_name by $username";
- }
- }
- for my $i (0..$#result) {
- $server->command("msg $channel_name ".$result[$i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement