-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeopicme-pl
executable file
·184 lines (157 loc) · 4.18 KB
/
geopicme-pl
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
#!/usr/bin/env perl
use Mojolicious::Lite;
use FindBin qw($Bin);
use lib "$Bin/lib", "lib";
use Flickr::API2::Cached;
use Data::Dumper;
use List::Util qw(shuffle);
# flickr key: 76904a42e0e0a2b7eeaf037c8ec2e00b
# flickr secret: b4d1a53c3463051a
my $flickr = Flickr::API2::Cached->new(
{'key' => '76904a42e0e0a2b7eeaf037c8ec2e00b',
'secret' => 'b4d1a53c3463051a'},
( -d "./geopics" ? ("./geopics") : ())
);
sub index {
my $self = shift;
$self->render('index');
}
get '/' => \&index;
get '/perl/' => \&index;
sub about {
my $self = shift;
$self->render('about');
};
get '/perl/about' => \&about;
get '/about' => \&about;
sub contact {
my $self = shift;
$self->render('contact');
};
get '/perl/contact' => \&contact;
get '/contact' => \&contact;
sub get_image_list {
my ($lat, $lon, $size) = @_;
my $images = $flickr->cached_method(
sprintf("@%.2f+%.2f", $lat, $lon), #cache to nearest mile or so
'flickr.photos.search', {
lat => $lat,
lon => $lon,
min_taken_date => time - 2 * 86400 * 365,
accuracy => 3,
has_geo => 1,
}
);
my @photos = @{$images->{photos}{photo}};
if ( @photos > 20 ) {
# get one each per user first
my @got;
while ( @got < 20 ) {
my %seen;
for my $photo ( @photos ) {
next if !defined $photo;
next if $seen{$photo->{owner}}++;
push @got, $photo;
undef($photo);
last if @got == 20;
}
}
@photos = @got;
}
#print STDERR Dumper(\@photos);
my $res;
my @urls = map { $flickr->get_url_from_image( $_, $size ) }
(@photos);
#print STDERR "Returning: @urls\n";
return @urls;
}
get '/perl/find/(*lat)/(*lon)' => sub {
my $self = shift;
my $lat = $self->stash('lat');
my $lon = $self->stash('lon');
$self->render_json(
[ get_image_list($lat, $lon, "t") ]
);
};
sub get_images {
my @rv;
while ( my $img = shift ) {
print STDERR "Fetching: $img\n";
my $filename = $flickr->get_image_from_cache( $img );
push @rv, $filename;
}
@rv;
}
use Time::HiRes qw(gettimeofday tv_interval);
use Digest::MD5 qw(md5_hex);
use threads::tbb;
use Geopicme;
our $tbb = threads::tbb->new( modules => [ "Geopicme" ] );
get '/perl/montage/(*method)/(*lat)/(*lon)/(*seed)' => sub {
my $self = shift;
my $method = $self->stash("method");
my $lat = $self->stash('lat');
my $lon = $self->stash('lon');
my $seed = $self->stash('seed');
my @images = get_image_list($lat, $lon, "z"); # 640px images
my $t0 = [gettimeofday];
my @files = get_images(@images);
if ( $seed ) { srand $seed; @files = shuffle @files }
my $t1 = [gettimeofday];
my $montage = Image::Magick->new(size => "800x640", debug => "Exception");
$montage->Read("xc:white");
my $label;
if ( $method eq "single" ) {
@images = map { resize_image($_) }
@files;
$label = "single";
}
else {
@images = $tbb->map_list_func("Geopicme::resize_image", @files);
$label = "tbb";
}
my $x_pos = 0;
my $y_pos = 0;
my $t2 = [gettimeofday];
for my $image ( @images ) {
$montage->Composite(
image => $image,
compose => "over",
x => $x_pos * 160, y => $y_pos * 160,
gravity => "NorthWest",
);
$x_pos++;
if ( $x_pos == 5 ) {
$y_pos++;
$x_pos = 0;
}
}
my $public = ( -d "public" ? "public" : "$Bin/public" );
my $img_filename = "$public/img/"
.md5_hex(sprintf("@%.2f+%.2f", $lat, $lon))."_montage_${label}_$seed.jpg";
#jopen my $out, ">", $img_filename;
print STDERR "Writing $img_filename\n";
$montage->Set(quality => 75);
if ( -f $img_filename ) {
unlink($img_filename);
}
my $x = $montage->Write("jpg:$img_filename");
warn $x if $x;
print STDERR "...?\n";
#jclose $out;
my $t3 = [gettimeofday];
my $fetch_time = tv_interval $t0, $t1;
my $resize_time = tv_interval $t1, $t2;
my $composite_time = tv_interval $t2, $t3;
(my $rel_url = $img_filename) =~ s{.*?img/}{img/};
my $rv =
{
fetch_time => sprintf("%.3s", $fetch_time),
resize_time => sprintf("%.3s", $resize_time),
composite_time => sprintf("%.3s", $composite_time),
image_url => $rel_url,
};
print STDERR "Returning ".Dumper($rv);
$self->render_json($rv);
};
app->start;