#!/usr/bin/perl -w use strict; use warnings; use Text::CSV; use Data::Dumper; binmode STDIN, ':utf8'; binmode STDOUT, ':utf8'; my $csv = Text::CSV->new ({ binary => 1 }) or die "Cannot use CSV: ".Text::CSV->error_diag(); my $out = \*STDOUT; $csv->eol("\n"); open my $inst_fh, "<", "inst-ids.txt" or die "can't open inst-ids.txt"; my @inst_ids = <$inst_fh>; chomp for @inst_ids; my %inst_ids = map { $_ => 1 } @inst_ids; close $inst_fh; open my $linux_fh, "<", "linux-ids.txt" or die "can't open linux-ids.txt"; my @linux_ids = <$linux_fh>; chomp for @linux_ids; close $linux_fh; open my $inst_linux_fh, "<", "inst-ids-linux.txt" or die "can't open inst-ids-linux.txt"; my @inst_ids_linux = <$inst_linux_fh>; chomp for @inst_ids_linux; close $inst_linux_fh; my %linux_ids = map { $_ => 1 } @linux_ids, @inst_ids_linux; open my $cat_fh, "<", "id,cat.csv" or die "can't open id,cat.csv"; my $default_cat = "untried"; my %cats; while (my $line = <$cat_fh>) { chomp $line; my ($id, $cat) = split ',', $line, 2; $cats{$id} = $cat; } close $cat_fh; open my $genre_fh, "<", "id,genre.csv" or die "can't open id,genre.csv"; my %genres; while (my $line = <$genre_fh>) { chomp $line; my ($id, $genre) = split ',', $line, 2; $genres{$id} = $genre; } close $genre_fh; open my $comm_tags_fh, "<", "id,comm_tags.csv" or die "can't open id,comm_tags.csv"; my %comm_tags; while (my $line = <$comm_tags_fh>) { chomp $line; $csv->parse($line) or die "can't parse CSV line from id,comm_tags.csv: $line"; my ($id, $comm_tags) = $csv->fields(); $comm_tags{$id} = $comm_tags; } close $comm_tags_fh; open my $comm_tags_cancel_fh, "<", "id,comm_tags_cancel.csv" or die "can't open id,comm_tags_cancel.csv"; my %comm_tags_cancel; while (my $line = <$comm_tags_cancel_fh>) { chomp $line; $csv->parse($line) or die "can't parse CSV line from id,comm_tags_cancel.csv: $line"; my ($id, $comm_tags_cancel) = $csv->fields(); $comm_tags_cancel{$id} = $comm_tags_cancel; } close $comm_tags_cancel_fh; open my $dev_fh, "<", "id,dev.csv" or die "can't open id,dev.csv"; my %dev; while (my $line = <$dev_fh>) { chomp $line; $csv->parse($line) or die "can't parse CSV line from id,dev.csv: $line"; my ($id, $dev) = $csv->fields(); $dev{$id} = $dev; } close $dev_fh; open my $mytags_fh, "<", "games-mytags.csv" or die "can't open games-mytags.csv"; my %mytags; while (my $line = <$mytags_fh>) { chomp $line; $csv->parse($line) or die "can't parse CSV line from games-mytags.csv: $line"; my ($id, $name, $mytags) = $csv->fields(); $mytags{$id} = $mytags; } close $mytags_fh; open my $appdetails_fh, "<", "appdetails.csv" or die "can't open appdetails.csv"; my %appdetails; while (my $line = <$appdetails_fh>) { chomp $line; $csv->parse($line) or die "can't parse CSV line from games-appdetails.csv: $line"; my ($id, $required_age, $metascore, $metacritic_name) = $csv->fields(); $appdetails{$id} = [$required_age, $metascore, $metacritic_name]; } close $appdetails_fh; my ($info); while ($_ = ) { chomp; if (/^$/) { my $id = $info->{appID}; my @my_tags; my @sys_tags; my @store_tags; my @comm_tags; my %in_comm_tags_cancel; my @dev_tags; my $cat = $cats{$id} || $default_cat; $cat =~ s/ /-/g; push @my_tags, $cat; push @my_tags, split / /, $mytags{$id} if $mytags{$id}; push @sys_tags, "linux" if $linux_ids{$id}; push @sys_tags, "inst" if $inst_ids{$id}; # TODO adult tag / or age rating tags my @genres = split / /, $genres{$id} if $genres{$id}; push @store_tags, @genres; @comm_tags = split / /, $comm_tags{$id} if $comm_tags{$id}; %in_comm_tags_cancel = map {$_=>1} split / /, $comm_tags_cancel{$id} if $comm_tags_cancel{$id}; my %in_genres = map { $_ => 1 } @genres; @comm_tags = grep { !$in_genres{$_} && !$in_comm_tags_cancel{$_} } @comm_tags; @dev_tags = split / /, $dev{$id} if $dev{$id}; my ($required_age, $metascore, $metacritic_name) = @{$appdetails{$id}||[]}; unshift @store_tags, "age:$required_age" if $required_age; unshift @store_tags, "$metascore%" if $metascore; $info->{metacritic_name} = $metacritic_name; $info->{my_tags} = "@my_tags"; $info->{sys_tags} = "@sys_tags"; $info->{store_tags} = "@store_tags"; $info->{dev_tags} = "@dev_tags"; $info->{comm_tags} = "@comm_tags"; $csv->print($out, [@$info{qw(appID name metacritic_name store_tags dev_tags sys_tags my_tags comm_tags)}]); $info = {}; } else { my ($k, $v) = split /: /, $_, 2; $info->{$k} = $v; } }