root/yapcasia2008/misc/party-prioritize.pl

Revision 2705 (checked in by miyagawa, 11 years ago)

use Text::CSV to generate csv as well

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Encode;
5 use Text::CSV_XS;
6
7 my @payments = read_csv('payments.csv');
8 my @users    = read_csv('export.csv');
9 my @talks    = read_csv('exported_talks.csv');
10
11 my %payments = map { $_->{user_id} => $_ } @payments;
12 my %talks    = map { $_->{user_id} => 1 } grep $_->{accepted}, @talks;
13
14 # schwertzian transform
15 my $i;
16 my @sorted = map {
17     $_->[6]->{party} = ++$i < 300 ? '1' : '0';
18     $_->[6];
19 } sort {
20     $b->[0] cmp $a->[0] || # is staff?
21     $b->[1] cmp $a->[1] || # is speaker?
22     $b->[2] cmp $a->[2] || # has paid?
23     $b->[3] cmp $a->[3] || # from abroad?
24     $b->[4] cmp $a->[4] || # from outside Tokyo?
25     $a->[5] cmp $b->[5]    # paid earlier?
26 } map {
27     $_->{has_accepted_talk} = exists $talks{$_->{user_id}};
28     $_->{paid_date}         = $payments{$_->{user_id}}->{datetime} || "9999-99-99 99:99:99";
29     $_->{not_tokyo}         = $_->{town} && !is_tokyo($_->{town});
30     $_->{payment_means}     = $payments{$_->{user_id}}->{means} || 'UNPAID';
31     $_->{has_really_paid}   = $_->{has_paid} && $_->{payment_means} eq 'ONLINE';
32     [ $_->{is_staff} || $_->{is_orga},
33       $_->{has_accepted_talk},
34       $_->{has_really_paid},
35       $_->{country} ne 'jp',
36       $_->{not_tokyo},
37       $_->{paid_date},
38       $_ ];
39 } @users;
40
41 my $csv = Text::CSV_XS->new({ binary => 1 });
42
43 my @cols = qw( user_id first_name last_name is_staff has_accepted_talk country town not_tokyo payment_means paid_date party );
44
45 $csv->combine(@cols);
46 print $csv->string, "\n";
47 for my $u (sort { $a->{user_id} <=> $b->{user_id} } @sorted) {
48     $csv->combine(@$u{@cols});
49     print $csv->string, "\n";
50 }
51
52 sub is_tokyo {
53     my $town = decode_utf8 shift;
54     $town =~ /tokyo|\x{6771}\x{4eac}|Hongo|chiyoda|shibuya|shinagawa/i;
55 }
56
57 sub read_csv {
58     open my $fh, "<", shift or die $!;
59
60     my $csv = Text::CSV_XS->new({ binary => 1 });
61     my $header = $csv->getline($fh);
62     $csv->column_names(@$header);
63
64     my @list;
65     while (!$csv->eof) {
66         my $ref = $csv->getline_hr($fh);
67         next unless $ref->{user_id};
68         push @list, $ref;
69     }
70
71     return @list;
72 }
Note: See TracBrowser for help on using the browser.