File: cpan-faces.pl

package info (click to toggle)
libparallel-iterator-perl 1.002-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 184 kB
  • sloc: perl: 520; makefile: 2
file content (221 lines) | stat: -rwxr-xr-x 5,344 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/perl

use strict;
use warnings;
use HTML::Tiny;
use LWP::UserAgent;
use File::Spec;
use File::Path;
use PerlIO::gzip;
use YAML qw<DumpFile LoadFile>;
use Getopt::Long;
use Parallel::Iterator qw( iterate );

$| = 1;

use constant MAIL_RC   => 'http://cpan.perl.org/authors/01mailrc.txt.gz';
use constant ICON_BASE => 'http://search.cpan.org/gravatar';
use constant AUTHOR    => 'http://search.cpan.org/~';
use constant OUTPUT    => 'cpan-faces';
use constant STATE     => File::Spec->catfile( OUTPUT, 'work.yml' );
use constant SIZE      => 80;

my $UPDATE = 0;

GetOptions( 'update' => \$UPDATE ) or die "cpan-gravatar.pl [--update]\n";

my $ua = LWP::UserAgent->new;

mkpath( OUTPUT );

my $icons = -f STATE ? LoadFile( STATE ) : {};

$SIG{INT} = sub {
    print "Got SIGINT, stopping\n";
    exit;
};

my $pid = $$;

END {
    if ( $$ == $pid ) {
        print "Saving ", STATE, "\n";
        DumpFile( STATE, $icons );

        my $index = File::Spec->catfile( OUTPUT, 'index.html' );
        open my $ih, '>', $index or die "Can't write $index ($!)\n";
        print $ih build_page( $icons );
        close $ih;
    }
}

update(
    $icons,
    $UPDATE
    ? sub {
        my ( $icons, $id ) = @_;
        return 0;
      }
    : sub {
        my ( $icons, $id ) = @_;
        return exists $icons->{$id}
          && $icons->{$id}->{state} eq 'done';
    }
);

sub update {
    my ( $icons, $skip_if ) = @_;
    print "Getting ", MAIL_RC, "\n";
    my $authors = get_authors( MAIL_RC );
    open my $ah, '<:gzip', $authors or die "Can't read $authors ($!)\n";

    my $iter = iterate(
        { workers => 20 },
        sub {
            my ( $id, undef ) = @_;
            print "Checking $id\n";
            return save_icon( lc( $id ) );
        },
        sub {
            while ( defined( my $line = <$ah> ) ) {
                next unless $line =~ /^alias\s+(\S+)/;
                return $1;
            }
            return;
        }
    );

    while ( my ( $id, $icon ) = $iter->() ) {
        $icons->{$id} = $icon;
        print "Icon saved as ", $icon->{name}, "\n"
          if $icon && $icon->{name};
    }
}

sub build_page {
    my $icons = shift;
    my $h     = HTML::Tiny->new;
    my @pic   = ();
    for my $id ( sort keys %$icons ) {
        my $icon = $icons->{$id};

        if ( my $img = $icon->{name} ) {
            push @pic,
              (
                $h->div(
                    { class => 'icon' },
                    $h->a(
                        { href => user_home( $id ) },
                        $h->img(
                            {
                                src    => File::Spec->abs2rel( $img, OUTPUT ),
                                width  => SIZE,
                                height => SIZE,
                                alt    => $id
                            }
                        ),
                    ),
                )
              );
        }
    }
    return $h->html(
        [
            $h->head(
                [
                    $h->title( 'The Faces of CPAN' ),
                    $h->link(
                        {
                            rel   => 'stylesheet',
                            href  => 'style.css',
                            type  => 'text/css',
                            media => 'screen'
                        }
                    )
                ]
            ),
            $h->body( [@pic] )
        ]
    );
}

sub get_authors {
    my $url  = shift;
    my $resp = $ua->get( $url );
    if ( $resp->is_success ) {
        my $name = File::Spec->catfile( OUTPUT, '01mailrc.txt.gz' );
        open my $ah, '>', $name or die "Can't write $name ($!)\n";
        binmode $ah;
        print $ah $resp->content;
        close $ah;
        return $name;
    }
    else {
        die $resp->status_line;
    }
}

sub user_home {
    my $id = shift;
    return AUTHOR . lc( $id );
}

sub save_icon {
    my $id = shift;
    my %ext_map = ( jpeg => 'jpg' );
    my ( $data, $type ) = eval { get_icon( $id ) };

    if ( $@ ) {
        return {
            error => $@,
            state => 'error'
        };
    }

    # if ( $data && $data ne $default_image && $type =~ m{ ^image/(\S+) }x ) {
    if ( $data && $type =~ m{ ^image/(\S+) }x ) {
        my $ext = $ext_map{$1} || $1;
        my $name = make_name( $id, $ext );
        open my $ih, '>', $name
          or die "Can't write $name ($!)\n";
        binmode $ih;
        print $ih $data;
        close $ih;
        return {
            name  => $name,
            state => 'done'
        };
    }

    return { state => 'done' };
}

sub make_name {
    my ( $email, $ext ) = @_;
    my %enc = (
        '@' => '-AT-',
        '.' => '-DOT-'
    );
    $email =~ s/([@.])/$enc{$1}||$1/eg;
    return File::Spec->catfile( OUTPUT, "$email.$ext" );
}

sub get_icon {
    my $id = shift;
    $id =~ s{^(((.).).*)$}{$3/$2/$1};
    TRY: for my $ext ( qw( jpg png ) ) {
        my $url  = ICON_BASE . '/' . $id . '.' . $ext;
        my $resp = $ua->get( $url );
        if ( $resp->is_success ) {
            return ( $resp->content, $resp->header( 'Content-Type' ) );
        }
        elsif ( $resp->code == 404 ) {
            next TRY;
        }
        else {
            die join ' ', $resp->code, $resp->message;
        }
    }
    return;
}