Skip to content

Instantly share code, notes, and snippets.

@Xom
Created October 7, 2013 01:32
Show Gist options
  • Save Xom/6861335 to your computer and use it in GitHub Desktop.
Save Xom/6861335 to your computer and use it in GitHub Desktop.
Inserts HSAS comments into IRC logs.
#!/usr/bin/env perl
use strict;
use warnings;
use lib '/home/frobozz/perl5/lib/perl5';
use Text::CSV;
use File::Slurp;
use JSON -support_by_pp;
use Time::Format qw( time_strftime );
my $names_file = '/home/frobozz/saltybookie/raw/players.csv';
my $hdata_dir = '/home/frobozz/saltybookie/raw/hdata/';
my $xchatlogs_dir = '/home/frobozz/saltybookie/xchatlogs/';
my @date_range = (20130815, 20130899);
# 0 hightower id
# 1 hightower name
my %names;
my $csv;
$csv = Text::CSV->new ( { binary => 1 } ) or die ('Cannot use CSV: '.Text::CSV->error_diag());
open my $fh, '<:encoding(utf8)', $names_file or die "$names_file: $!";
while ( my $row = $csv->getline( $fh ) ) {
my @kludge;
push @kludge, $row;
$names{$kludge[0][0]} = $kludge[0][1];
}
$csv->eof or $csv->error_diag();
close $fh;
# {"matches":[],
# "comments":[{"id":8912,
# "created":1377576610230,
# "body":"InBreastigator was here! :3",
# "flagged":false,
# "important":false},
# {"id":15252,
# "created":1381089908196,
# "body":"Test Comment Please Ignore",
# "flagged":false,
# "important":false}],
# "meta":{"zone":false, ... ... ... ,"glitch":false},
# "name":"Test1",
# "id":7546,
# "elo":50}
my @ids = keys %names;
my %days;
for (my $i = 0; $i < @ids; $i++) {
my $json_raw = read_file($hdata_dir.$ids[$i]);
my $json = new JSON;
my $json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($json_raw);
foreach my $comment (@{$json_text->{comments}}) {
my $comment_date = time_strftime('%Y%m%d', ($comment->{created} / 1000));
if (($date_range[0] <= $comment_date) && ($comment_date <= $date_range[1])) {
my $comment_body = 'HSAS[ '.$names{$ids[$i]}." ]\t";
if ($comment->{flagged} eq 'true') {
$comment_body .= '[flagged]';
}
if ($comment->{important} eq 'true') {
$comment_body .= '[important]';
}
$comment_body .= $comment->{body};
if (!(exists $days{$comment_date})) {
$days{$comment_date} = [];
}
push @{$days{$comment_date}}, [time_strftime('%b %d %H:%M:%S', ($comment->{created} / 1000)), $comment_body];
}
}
}
foreach my $day (keys %days) {
print "$day start\n";
my @chatlines = read_file($xchatlogs_dir.$day.'.#saltybet.log', binmode => ':raw');
my @comments = sort {$a->[0] cmp $b->[0]} @{$days{$day}};
my $cursor = 0;
my $last_line_hsas = 1;
foreach my $comment (@comments) {
while (($cursor < scalar(@chatlines))
&& ((substr($chatlines[$cursor], 0, 6) ne substr(@{$comment}[0], 0, 6))
|| (($chatlines[$cursor] cmp @{$comment}[0]) < 0))) {
$cursor += 1;
$last_line_hsas = 0;
}
splice(@chatlines, $cursor, 0, sprintf(($last_line_hsas ? "%s %s\n\n" : "\n%s %s\n\n"), @{$comment}[0], @{$comment}[1]));
$cursor += 1;
$last_line_hsas = 1;
}
write_file($xchatlogs_dir.$day.'.#saltybet.log', {binmode => ':utf8'}, @chatlines);
print "$day done\n\n";
}
exit 0;
# foreach my $comment (@comments) {
# print sprintf("%s %s\n", @{$comment}[0], @{$comment}[1]);
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment