-
Notifications
You must be signed in to change notification settings - Fork 0
/
flickr_getphotolist.pl
executable file
·168 lines (136 loc) · 5.35 KB
/
flickr_getphotolist.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
#!/usr/bin/perl -s
#
# Adapted Hack #33 (chapter 5) from Flickr Hacks, by Paul Bausch, Jim Bumgardner
# O'Reilly, ISBN: 0-596-10245-3.
# Get list of public photos with 'flickr.people.getPublicPhotos
# As method's name suggests: _only public photos are returned_
#
# $verbose = 1;
use Flickr::API;
use XML::Simple;
use Data::Dumper;
$Data::Dumper::Terse = 1; # avoids $VAR1 = * ; in dumper output
$Data::Dumper::Indent = $verbose? 1 : 0; # more concise output
use Compress::Zlib ;
require 'login2flickr.rc';
print "verbose = $verbose\n" if $verbose;
$syntax = <<EOT;
Get photo descriptions from flickr, store them in a file <tag>.ph
getPhotoList.pl [options] <tags> [<tags...>]
getPhotoList.pl [options] -g group_id [<tag>]
getPhotoList.pl [options] -u username [<tags>]
Options:
-all Photos must match all tags (tag search only)
-recent=X Only provide photos posted within the last X days (tag searches only)
-limit=X Provide no more than X photos
-license=X Provide photos with license X
EOT
die $syntax if @ARGV == 0;
my $api = new Flickr::API({'key' => $api_key, secret => $sharedsecret});
# determine method to use
$method = 'flickr.photos.search';
if ($g) {
$group_id = shift;
$tags = shift;
$method = 'flickr.groups.pools.getPhotos';
print "Searching for photos in group $group_id\n";
# determine output filename
$ofname = $group_id . '.ph';
}
elsif ($u) {
$username = shift;
$tags = join ',', @ARGV;
$method = 'flickr.people.getPublicPhotos' if !$tags;
print "Searching for photos by user $username using $method\n";
# determine output filename
$ofname = "$FLICKRCFG/" . $username; ## --tp-zmienil--
$ofname .= '_' . $tags if $tags;
$ofname =~ s/,\s*/_/g;
$ofname .= '.ph';
}
else {
$tags = join ',', @ARGV;
print "Searching for photos with tags=$tags ...\n";
# determine output filename
$ofname = $tags . '.ph';
$ofname =~ s/,\s*/_/g;
}
$nbrPages = 0;
$photoIdx = 0;
$limit = 5000 if $limit;
print STDERR "Writing to $ofname\n";
open (OFILE, ">$ofname");
print OFILE "## created with flickr_getphotolist.pl\n";
print OFILE "\@photos = (\n";
$user_id = '';
$min_taken_date = '';
$max_taken_date = '';
$xmlp = new XML::Simple ( );
if ($recent)
{
die "-recent option only valid for tag search\n" if $method ne 'flickr.photos.search';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
gmtime(time - $recent*24*60*60);
$min_taken_date = sprintf "%04d-%02d-%02d 00:00:00",1900+$year,$mon+1,$mday;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
gmtime(time);
$max_taken_date = sprintf "%04d-%02d-%02d 00:00:00",1900+$year,$mon+1,$mday;
}
if ($username) {
# look up user ID if a username was provided
#
my $response = $api->execute_method('flickr.people.findByUsername', { username => $username } );
die "Problem determining user_id: $response->{error_message}\n" if !$response->{success};
print Dumper($response) if $verbose; # explore results of call using -verbose
## to co ponizej jest `q&d' hack zakladajacy ze czasami _content jest plain txt a czasami jest gziped
## print STDERR "** A >>> **" . $response->{_headers}->{content-encoding} . "\n";
## 2011/08/15
my $content_encoding = $response->{_headers}->{'content-encoding'} ;
## print STDERR "Content encoding: $content_encoding\n";
##print STDERR "** A >>> **" . Compress::Zlib::memGunzip( $response->{_content}) . "\n";
my $plain_content;
if ($content_encoding =~ /gzip/ ) {##
$plain_content = Compress::Zlib::memGunzip( $response->{_content});
} else { $plain_content = $response->{_content}; }
$xm = $xmlp->XMLin($plain_content);
$user_id = $xm->{user}->{id};
print "Userid: $user_id\n";
}
do
{
my $params = { per_page => 500,
page => $nbrPages+1};
$params->{tags} = $tags if $tags;
$params->{user_id} = $user_id if $user_id;
$params->{group_id} = $group_id if $group_id;
$params->{min_taken_date} = $min_taken_date if $min_taken_date;
$params->{max_taken_date} = $max_taken_date if $max_taken_date;
$params->{license} = $license if $license;
$params->{extras} = $extras if $extras;
$params->{tag_mode} = 'all' if $all;
my $response = $api->execute_method($method, $params );
die "Problem: $response->{error_message}\n" if !$response->{success};
print Dumper($response) if $verbose; # explore results of call using -verbose
## sprawdz czy _content nie jest gzipniety, jezeli to rozpakuj:
my $content_encoding = $response->{_headers}->{'content-encoding'} ;
my $plain_content ;
if ($content_encoding =~ /gzip/ ) {##
$plain_content = Compress::Zlib::memGunzip( $response->{_content});
} else { $plain_content = $response->{_content}; }
#my $xm = $xmlp->XMLin($response->{_content},forcearray=>['photo']);
my $xm = $xmlp->XMLin($plain_content, forcearray=>['photo']);
$photos = $xm->{photos};
print "Page $photos->{page} of $photos->{pages}\n";
# loop thru photos
$photoList = $xm->{photos}->{photo};
foreach $id (keys %{$photoList})
{
my $photo = $photoList->{$id};
$photo->{id} = $id;
print OFILE ($photoIdx++? ",\n" : "") . Dumper($photo);
}
++$nbrPages;
} while ($photos->{page} < $photos->{pages} && (!$limit || $photoIdx < $limit));
print OFILE "\n);\n1;\n";
close OFILE;
print "$photoIdx photos found matching tags $tags written to $ofname\n";