-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_flickr_photo_details.pl
139 lines (102 loc) · 3.74 KB
/
store_flickr_photo_details.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
#!/usr/bin/perl
#
use strict;
use Compress::Zlib;
use XML::LibXML;
use LWP::Simple;
use Data::Dumper;
require 'login2flickr.rc';
our $api_key;
my $numc = 0;
my $verbose = 0 ;
my $buffer;
my $xml;
my $gz = undef;
my $success = 1;
my $photo_id = '';
my $owner_id = '';
my $photo_title = '';
my ($n, $t);
my $method = '';
my $flickr_url = '';
my $resp;
my $infile = $ARGV[0];
## Construct a node with text `Probably deleted!':
my $deleted_text = XML::LibXML::Text->new( 'Probably deleted!' );
my $ele_deleted = XML::LibXML::Element->new('TP_deleted');
$ele_deleted->addChild($deleted_text);
## ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
my $parser = XML::LibXML->new;
if ($gz = gzopen($infile, "rb") ) {
while ($gz->gzreadline($buffer) > 0) { $xml .= $buffer; }
$gz->gzclose ;
my $doc = $parser->parse_string($xml);
my @elems = $doc->getElementsByTagName('photo' ) ;
if ($verbose > -1 ) { print STDERR "$infile :: $#elems\n"; }
for $n ( @elems ) {
$photo_id = $n->getAttribute( 'id' );
$owner_id = $n->getAttribute( 'owner' );
$photo_title = $n->getAttribute( 'title' );
if ($verbose > -1 ) { print STDERR " $infile :: $numc/$#elems :: ID : $photo_id ; Own : $owner_id ; Title : $photo_title\n"; }
## Get tags ::
$method = 'flickr.tags.getListPhoto';
$resp = query_flickr( "method=$method&api_key=$api_key&photo_id=$photo_id" );
## check if photo is not deleted _in the_ meantime
unless (check_flickr_resp($resp) eq 'ok') {
$ele_deleted = $n->appendChild( $ele_deleted );
if ($verbose > -1 ) { print STDERR " ID = $photo_id probably deleted...\n"; }
next ; ## photo probably deleted do not process further!!!
}
my $doc = $parser->parse_string($resp);
if ( my @tags = $doc->getElementsByTagName('tags' ) ) {
for $t (@tags ) { $t = $n->appendChild( $t ); }
}
## Get Pools and sets :: ;;;;;;;;;;;
$method = 'flickr.photos.getAllContexts';
$resp = query_flickr( "method=$method&api_key=$api_key&photo_id=$photo_id" );
my $doc = $parser->parse_string($resp);
if ( my @pools = $doc->getElementsByTagName('pool' ) ) {
my $new_pools = XML::LibXML::Element->new('pools');
$new_pools = $n->appendChild( $new_pools );
for $t (@pools ) { $t = $new_pools->appendChild( $t ); }
}
if ( my @sets = $doc->getElementsByTagName('set' ) ) {
my $new_sets = XML::LibXML::Element->new('sets');
$new_sets = $n->appendChild( $new_sets );
for $t (@sets ) { $t = $new_sets->appendChild( $t ); }
}
## Get Geo locations :: ;;;;;;;;;
$method = 'flickr.photos.geo.getLocation';
$resp = query_flickr( "method=$method&api_key=$api_key&photo_id=$photo_id" );
my $doc = $parser->parse_string($resp);
if (my @locations = $doc->getElementsByTagName('location' ) ) {
for $t (@locations ) { $t = $n->appendChild( $t ); }
}
##if ($numc > 3 ) { last }; ## one iteration
##
$numc++;
print STDERR ".";
}
## http://www.bytemycode.com/snippets/snippet/164/
my $outfile = "x_$infile";
if ($gz = gzopen($outfile, "wb") ) { $buffer = $doc->toString() ;
$gz->gzwrite( $buffer ) or die "error writing: $gzerrno\n" ; }
$gz->gzclose ; ## close
###
}
##
sub query_flickr {
my $url = shift ; ## np. "http://www.flickr.com/services/rest/?method=$method&api_key=$api_key&photo_id=$photo_id";
return ( get ( "http://www.flickr.com/services/rest/?" . $url ) ); ## resp is an XML file
}
sub check_flickr_resp {
my $resp = shift ;
my $doc = $parser->parse_string($resp);
if ( my $root = $doc->getElementsByTagName('rsp' )->[0] ) {
return ( $root->getAttribute( 'stat' ) ); }
}
###
#<?xml version="1.0" encoding="utf-8" ?>
#<rsp stat="fail">
# <err code="1" msg="Photo not found" />
#</rsp>