This file is indexed.

/usr/share/doc/flickrapi-1.2/flickrapi.html is in python-flickrapi 1.2-3.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.12: http://docutils.sourceforge.net/" />
<title>Python FlickrAPI</title>
<meta name="author" content="Sybren Stüvel" />
<style type="text/css">

@import url(html4css1.css);

html {
    color: black;
    background-color: white;
}

body {
    margin-left: 10ex;
    margin-top: 5ex;
    padding-left: 1ex;
    border-left: 1px solid #006;

    width: 75ex;
    background-color: white;
}

h1 {
    border-bottom: 2px solid #006;
}

p {
    text-align: justify;
}

dt {
    font-weight: bold;
}

</style>
</head>
<body>
<div class="document" id="python-flickrapi">
<h1 class="title">Python FlickrAPI</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Version:</th>
<td>1.2</td></tr>
<tr><th class="docinfo-name">Author:</th>
<td>Sybren Stüvel</td></tr>
</tbody>
</table>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="auto-toc simple">
<li><a class="reference internal" href="#introduction" id="id1">1&nbsp;&nbsp;&nbsp;Introduction</a><ul class="auto-toc">
<li><a class="reference internal" href="#concepts" id="id2">1.1&nbsp;&nbsp;&nbsp;Concepts</a></li>
</ul>
</li>
<li><a class="reference internal" href="#calling-api-functions" id="id3">2&nbsp;&nbsp;&nbsp;Calling API functions</a><ul class="auto-toc">
<li><a class="reference internal" href="#parsing-the-return-value" id="id4">2.1&nbsp;&nbsp;&nbsp;Parsing the return value</a></li>
<li><a class="reference internal" href="#response-parser-elementtree" id="id5">2.2&nbsp;&nbsp;&nbsp;Response parser: ElementTree</a></li>
<li><a class="reference internal" href="#elementtree-in-python-2-4" id="id6">2.3&nbsp;&nbsp;&nbsp;ElementTree in Python 2.4</a></li>
<li><a class="reference internal" href="#response-parser-xmlnode" id="id7">2.4&nbsp;&nbsp;&nbsp;Response parser: XMLNode</a></li>
<li><a class="reference internal" href="#erroneous-calls" id="id8">2.5&nbsp;&nbsp;&nbsp;Erroneous calls</a></li>
<li><a class="reference internal" href="#unparsed-response-formats" id="id9">2.6&nbsp;&nbsp;&nbsp;Unparsed response formats</a></li>
</ul>
</li>
<li><a class="reference internal" href="#authentication" id="id10">3&nbsp;&nbsp;&nbsp;Authentication</a><ul class="auto-toc">
<li><a class="reference internal" href="#authenticating-web-applications" id="id11">3.1&nbsp;&nbsp;&nbsp;Authenticating web applications</a></li>
<li><a class="reference internal" href="#token-handling-in-web-applications" id="id12">3.2&nbsp;&nbsp;&nbsp;Token handling in web applications</a></li>
<li><a class="reference internal" href="#preventing-usage-of-on-disk-token-cache" id="id13">3.3&nbsp;&nbsp;&nbsp;Preventing usage of on-disk token cache</a></li>
<li><a class="reference internal" href="#example-using-django" id="id14">3.4&nbsp;&nbsp;&nbsp;Example using Django</a></li>
</ul>
</li>
<li><a class="reference internal" href="#uploading-or-replacing-images" id="id15">4&nbsp;&nbsp;&nbsp;Uploading or replacing images</a><ul class="auto-toc">
<li><a class="reference internal" href="#flickr-upload" id="id16">4.1&nbsp;&nbsp;&nbsp;flickr.upload(...)</a></li>
<li><a class="reference internal" href="#flickr-replace" id="id17">4.2&nbsp;&nbsp;&nbsp;flickr.replace(...)</a></li>
</ul>
</li>
<li><a class="reference internal" href="#unicode-and-utf-8" id="id18">5&nbsp;&nbsp;&nbsp;Unicode and UTF-8</a></li>
<li><a class="reference internal" href="#caching-of-flickr-api-calls" id="id19">6&nbsp;&nbsp;&nbsp;Caching of Flickr API calls</a><ul class="auto-toc">
<li><a class="reference internal" href="#using-the-django-caching-framework" id="id20">6.1&nbsp;&nbsp;&nbsp;Using the Django caching framework</a></li>
</ul>
</li>
<li><a class="reference internal" href="#requirements-and-compatibility" id="id21">7&nbsp;&nbsp;&nbsp;Requirements and compatibility</a></li>
<li><a class="reference internal" href="#links" id="id22">8&nbsp;&nbsp;&nbsp;Links</a></li>
</ul>
</div>
<div class="section" id="introduction">
<h1><a class="toc-backref" href="#id1">1&nbsp;&nbsp;&nbsp;Introduction</a></h1>
<p><a class="reference external" href="http://www.flickr.com/">Flickr</a> is one of the most popular photo sharing websites. Their
public API makes it very easy to write applications that use Flickr
some way or another. The possibilities are limitless. This document
describes how to use the Flickr API in your Python programs using the
<a class="reference external" href="http://flickrapi.sourceforge.net/">Python Flickr API interface</a>.</p>
<p>This documentation does not specify what each Flickr API function
does, nor what it returns. The <a class="reference external" href="http://www.flickr.com/services/api/">Flickr API documentation</a> is the
source for that information, and will most likely be more up-to-date
than this document could be. Since the Python Flickr API uses dynamic
methods and introspection, you can call new Flickr methods as soon as
they become available.</p>
<div class="section" id="concepts">
<h2><a class="toc-backref" href="#id2">1.1&nbsp;&nbsp;&nbsp;Concepts</a></h2>
<p>To keep things simple, we do not write &quot;he/she&quot; or &quot;(s)he&quot;. We know
that men and women can all be fine programmers and end users. Some
people will be addressed as male, others as female.</p>
<p>To be able to easily talk about Flickr, its users, programmers and
applications, here is an explanation of some concepts we use.</p>
<dl class="docutils">
<dt>you</dt>
<dd>The reader of this document. We assume you are a programmer and
that you are using this Python Flickr API to create an
application. In this document we shall address you as male.</dd>
<dt>application</dt>
<dd>The Python application you are creating, that has to interface
with Flickr.</dd>
<dt>user</dt>
<dd>The user of the application, and thus (either directly or
indirectly via your application) a Flickr user. In this document
we shall address the user as female.</dd>
</dl>
</div>
</div>
<div class="section" id="calling-api-functions">
<h1><a class="toc-backref" href="#id3">2&nbsp;&nbsp;&nbsp;Calling API functions</a></h1>
<p>You start by creating a FlickrAPI object with your API key. This key
can be obtained at <a class="reference external" href="http://www.flickr.com/services/api/keys/apply/">Flickr Services</a>. Once you have that key, the
cool stuff can begin. Calling a Flickr function is very easy. Here are
some examples:</p>
<pre class="literal-block">
import flickrapi

api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

flickr = flickrapi.FlickrAPI(api_key)
photos = flickr.photos_search(user_id='73509078&#64;N00', per_page='10')
sets = flickr.photosets_getList(user_id='73509078&#64;N00')
</pre>
<p>There is a simple naming scheme here. If the flickr function is called
<tt class="docutils literal">flickr.photosets.getList</tt> just call <tt class="docutils literal">photosets_getList</tt> on your
<tt class="docutils literal">flickr</tt> object. In other words: replace the dots with underscores.</p>
<div class="section" id="parsing-the-return-value">
<h2><a class="toc-backref" href="#id4">2.1&nbsp;&nbsp;&nbsp;Parsing the return value</a></h2>
<p>Flickr sends back XML when you call a function. This XML is parsed and
returned to you. There are two parsers available: ElementTree and
XMLNode. ElementTree was introduced in version 1.1, and replaced
XMLNode as the default parser as of version 1.2.</p>
<p>In the following sections, we'll use a <tt class="docutils literal">sets =
<span class="pre">flickr.photosets_getList(...)</span></tt> call and assume this was the response
XML:</p>
<pre class="literal-block">
&lt;rsp stat='ok'&gt;
    &lt;photosets cancreate=&quot;1&quot;&gt;
        &lt;photoset id=&quot;5&quot; primary=&quot;2483&quot; secret=&quot;abcdef&quot;
                server=&quot;8&quot; photos=&quot;4&quot;&gt;
            &lt;title&gt;Test&lt;/title&gt;
            &lt;description&gt;foo&lt;/description&gt;
        &lt;/photoset&gt;
        &lt;photoset id=&quot;4&quot; primary=&quot;1234&quot; secret=&quot;832659&quot;
                server=&quot;3&quot; photos=&quot;12&quot;&gt;
            &lt;title&gt;My Set&lt;/title&gt;
            &lt;description&gt;bar&lt;/description&gt;
        &lt;/photoset&gt;
    &lt;/photosets&gt;
&lt;/rsp&gt;
</pre>
</div>
<div class="section" id="response-parser-elementtree">
<h2><a class="toc-backref" href="#id5">2.2&nbsp;&nbsp;&nbsp;Response parser: ElementTree</a></h2>
<p>The old XMLNode parser had some drawbacks. A better one is Python's
standard <a class="reference external" href="http://docs.python.org/lib/module-xml.etree.ElementTree.html">ElementTree</a>. If you create the <tt class="docutils literal">FlickrAPI</tt> instance like
this, you'll use ElementTree:</p>
<pre class="literal-block">
flickr = flickrapi.FlickrAPI(api_key)
</pre>
<p>or explicitly:</p>
<pre class="literal-block">
flickr = flickrapi.FlickrAPI(api_key, format='etree')
</pre>
<p>The <a class="reference external" href="http://docs.python.org/lib/module-xml.etree.ElementTree.html">ElementTree documentation</a> is quite clear, but to make things
even easier, here are some examples using the same call and response
XML as in the XMLNode example:</p>
<pre class="literal-block">
sets = flickr.photosets_getList(user_id='73509078&#64;N00')

sets.attrib['stat'] =&gt; 'ok'
sets.find('photosets').attrib['cancreate'] =&gt; '1'

set0 = sets.find('photosets').findall('photoset')[0]

+-------------------------------+-----------+
| variable                      | value     |
+-------------------------------+-----------+
| set0.attrib['id']             | u'5'      |
| set0.attrib['primary']        | u'2483'   |
| set0.attrib['secret']         | u'abcdef' |
| set0.attrib['server']         | u'8'      |
| set0.attrib['photos']         | u'4'      |
| set0.title[0].text            | u'Test'   |
| set0.description[0].text      | u'foo'    |
| set0.find('title').text       | 'Test'    |
| set0.find('description').text | 'foo'     |
+-------------------------------+-----------+

... and similar for set1 ...
</pre>
<p>ElementTree is a more mature, better thought out XML parsing
framework. It has several advantages over the old XMLNode parser:</p>
<blockquote>
<ol class="arabic simple">
<li>As a standard XML representation, ElementTree will be easier to
plug into existing software.</li>
<li>Easier to iterate over elements. For example, to list all
&quot;title&quot; elements, you only need to do
<tt class="docutils literal"><span class="pre">sets.getiterator('title')</span></tt>.</li>
<li>Developed by the Python team, which means it's subject to more
rigorous testing and has a wider audience than the Python
Flickr API module. This will result in a higher quality and
less bugs.</li>
</ol>
</blockquote>
</div>
<div class="section" id="elementtree-in-python-2-4">
<h2><a class="toc-backref" href="#id6">2.3&nbsp;&nbsp;&nbsp;ElementTree in Python 2.4</a></h2>
<p>Python 2.5 comes shipped with ElementTree. To get it running on Python
2.4 you'll have to install ElementTree yourself. The easiest way is to
get setuptools and then just type:</p>
<pre class="literal-block">
easy_install elementtree
easy_install flickrapi
</pre>
<p>That'll get you both ElementTree and the latest version of the Python
Flickr API.</p>
<p>Another method is to get the Python FlickrAPI source and run:</p>
<pre class="literal-block">
python setup.py install
easy_install elementtree
</pre>
<p>As a last resort, you can <a class="reference external" href="http://effbot.org/downloads/#elementtree">download ElementTree</a> and install it
manually.</p>
</div>
<div class="section" id="response-parser-xmlnode">
<h2><a class="toc-backref" href="#id7">2.4&nbsp;&nbsp;&nbsp;Response parser: XMLNode</a></h2>
<p>The XMLNode objects are quite simple. Attributes in the XML are
converted to dictionary keys with unicode values. Subelements are
stored in properties.</p>
<p>We assume you did <tt class="docutils literal">sets = <span class="pre">flickr.photosets_getList(...)</span></tt>. The
<tt class="docutils literal">sets</tt> variable will be structured as such:</p>
<pre class="literal-block">
sets['stat'] = 'ok'
sets.photosets[0]['cancreate'] = u'1'
sets.photosets[0].photoset = &lt; a list of XMLNode objects &gt;

set0 = sets.photosets[0].photoset[0]
set1 = sets.photosets[0].photoset[1]

+--------------------------+-----------+
| variable                 | value     |
+--------------------------+-----------+
| set0['id']               | u'5'      |
| set0['primary']          | u'2483'   |
| set0['secret']           | u'abcdef' |
| set0['server']           | u'8'      |
| set0['photos']           | u'4'      |
| set0.title[0].text       | u'Test'   |
| set0.description[0].text | u'foo'    |
+--------------------------+-----------+
| set1['id']               | u'4'      |
| set1['primary']          | u'1234'   |
| set1['secret']           | u'832659' |
| set1['server']           | u'3'      |
| set1['photos']           | u'12'     |
| set1.title[0].text       | u'My Set' |
| set1.description[0].text | u'bar'    |
+--------------------------+-----------+
</pre>
<p>Every <tt class="docutils literal">XMLNode</tt> also has a <tt class="docutils literal">name</tt> property. The content of this
property is left as an exercise for the reader.</p>
<p>As of version 1.2 of the Python Flickr API this XMLNode parser is no
longer the default parser, in favour of the ElementTree parser.
XMLNode is still supported, though.</p>
</div>
<div class="section" id="erroneous-calls">
<h2><a class="toc-backref" href="#id8">2.5&nbsp;&nbsp;&nbsp;Erroneous calls</a></h2>
<p>When something has gone wrong Flickr will return an error code and a
description of the error. In this case, a <tt class="docutils literal">FlickrError</tt> exception
will be thrown.</p>
<p>The old behaviour of the Python Flickr API was to simply return the
error code in the XML. However, this is deprecated behaviour as we
strive to notice an error condition as soon as possible. Checking the
return value of every call is not Pythonic. For backward compatibility
you can pass <tt class="docutils literal">fail_on_error=False</tt> to the <tt class="docutils literal">FlickrAPI</tt> constructor,
but this behaviour is deprecated and will be removed in version 1.2.</p>
</div>
<div class="section" id="unparsed-response-formats">
<h2><a class="toc-backref" href="#id9">2.6&nbsp;&nbsp;&nbsp;Unparsed response formats</a></h2>
<p>Flickr supports different response formats, such as JSON and XML-RPC.
If you want, you can use such a different response format. Just add a
<tt class="docutils literal"><span class="pre">format=&quot;json&quot;</span></tt> option to the Flickr call. The Python Flickr API
won't parse that format for you, though, so you just get the raw
response:</p>
<pre class="literal-block">
&gt;&gt;&gt; f = flickrapi.FlickrAPI(api_key)
&gt;&gt;&gt; f.test_echo(boo='baah', format='json')
'jsonFlickrApi({&quot;format&quot;:{&quot;_content&quot;:&quot;json&quot;},
  &quot;auth_token&quot;:{&quot;_content&quot;:&quot;xxxxx&quot;},
  &quot;boo&quot;:{&quot;_content&quot;:&quot;baah&quot;},
  &quot;api_sig&quot;:{&quot;_content&quot;:&quot;xxx&quot;},
  &quot;api_key&quot;:{&quot;_content&quot;:&quot;xxx&quot;},
  &quot;method&quot;:{&quot;_content&quot;:&quot;flickr.test.echo&quot;},
  &quot;stat&quot;:&quot;ok&quot;})'
</pre>
<p>If you want all your calls in a certain format, you can also use the
<tt class="docutils literal">format</tt> constructor parameter:</p>
<pre class="literal-block">
&gt;&gt;&gt; f = flickrapi.FlickrAPI(api_key, format='json')
&gt;&gt;&gt; f.test_echo(boo='baah')
'jsonFlickrApi({&quot;format&quot;:{&quot;_content&quot;:&quot;json&quot;},
  &quot;auth_token&quot;:{&quot;_content&quot;:&quot;xxxxx&quot;},
  &quot;boo&quot;:{&quot;_content&quot;:&quot;baah&quot;},
  &quot;api_sig&quot;:{&quot;_content&quot;:&quot;xxx&quot;},
  &quot;api_key&quot;:{&quot;_content&quot;:&quot;xxx&quot;},
  &quot;method&quot;:{&quot;_content&quot;:&quot;flickr.test.echo&quot;},
  &quot;stat&quot;:&quot;ok&quot;})'
</pre>
<p>If you use an unparsed format, FlickrAPI won't check for errors. Any
format not described in the &quot;Response parser&quot; sections is considered
to be unparsed.</p>
</div>
</div>
<div class="section" id="authentication">
<h1><a class="toc-backref" href="#id10">3&nbsp;&nbsp;&nbsp;Authentication</a></h1>
<p>Her photos may be private. Access to her account is private for sure.
A lot of Flickr API calls require the application to be authenticated.
This means that the user has to tell Flickr that the application is
allowed to do whatever it needs to do.</p>
<p>The Flickr document <a class="reference external" href="http://www.flickr.com/services/api/misc.userauth.html">User Authentication</a> explains the authentication
process; it's good to know what's in there before you go on.</p>
<p>The document states &quot;The auth_token and api_sig parameters should then
be passed along with each request&quot;. You do <em>not</em> have to do this - the
Python Flickr API takes care of that.</p>
<p>Here is a simple example of Flickr's two-phase authentication:</p>
<pre class="literal-block">
import flickrapi

api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
api_secret = 'YYYYYYYYYYYYYYYY'

flickr = flickrapi.FlickrAPI(api_key, api_secret)

(token, frob) = flickr.get_token_part_one(perms='write')
if not token: raw_input(&quot;Press ENTER after you authorized this program&quot;)
flickr.get_token_part_two((token, frob))
</pre>
<p>The <tt class="docutils literal">api_key</tt> and <tt class="docutils literal">api_secret</tt> can be obtained from
<a class="reference external" href="http://www.flickr.com/services/api/keys/">http://www.flickr.com/services/api/keys/</a>.</p>
<p>The call to <tt class="docutils literal"><span class="pre">flickr.get_token_part_one(...)</span></tt> does a lot of things.
First, it checks the on-disk token cache. After all, the application
may be authenticated already.</p>
<p>If the application isn't authenticated, a browser opens the Flickr
page, on which the user can grant the application the appropriate
access. The application has to wait for the user to do this, hence the
<tt class="docutils literal"><span class="pre">raw_input(&quot;Press</span> ENTER after you authorized this program&quot;)</tt>. A GUI
application can use a popup for this, or some other way for the user
to indicate she has performed the authentication ritual.</p>
<p>Once this step is done, we can continue to store the token in the
cache and remember it for future API calls. This is what
<tt class="docutils literal"><span class="pre">flickr.get_token_part_two(...)</span></tt> does.</p>
<div class="section" id="authenticating-web-applications">
<h2><a class="toc-backref" href="#id11">3.1&nbsp;&nbsp;&nbsp;Authenticating web applications</a></h2>
<p>When working with web applications, things are a bit different. The
user using the application (through a browser) is likely to be
different from the user running the server-side software.</p>
<p>We'll assume you're following Flickr's <a class="reference external" href="http://www.flickr.com/services/api/auth.howto.web.html">Web Applications How-To</a>, and
just tell you how things are splified when working with the Python
Flickr API.</p>
<blockquote>
<ol class="arabic simple" start="3">
<li>Create a login link. Use <tt class="docutils literal">flickr.web_login_url(perms)`</tt> for
that.  It'll return the login link for you, given the
permissions you passed in the <tt class="docutils literal">perms</tt> parameter.</li>
</ol>
<ol class="arabic simple" start="5">
<li>Don't bother understanding the signing process; the
<tt class="docutils literal">FlickrAPI</tt> module takes care of that for you. Once you
received the frob from Flickr, use
<tt class="docutils literal"><span class="pre">flickr.get_token(&quot;the_frob&quot;)</span></tt>. The FlickrAPI module will
remember the token for you.</li>
<li>You can safely skip this, and just use the FlickrAPI module as
usual. Only read this if you want to understand how the
FlickrAPI module signs method calls for you.</li>
</ol>
</blockquote>
</div>
<div class="section" id="token-handling-in-web-applications">
<h2><a class="toc-backref" href="#id12">3.2&nbsp;&nbsp;&nbsp;Token handling in web applications</a></h2>
<p>Web applications have two kinds of users: identified and anonymous
users. If your users are identified, you can pass their name (or other
means of identification) as the <tt class="docutils literal">username</tt> parameter to the
<tt class="docutils literal">FlickrAPI</tt> constructor, and get a FlickrAPI instance that's bound
to that user. It will keep track of the authentication token for that
user, and there's nothing special you'll have to do.</p>
<p>When working with anonymous users, you'll have to store the
authentication token in a cookie. In step 5. above, use this:</p>
<pre class="literal-block">
token = flickr.get_token(&quot;the_frob&quot;)
</pre>
<p>Then use your web framework to store the token in a cookie. When
reading a token from a cookie, pass it on to the FlickrAPI constructor
like this:</p>
<pre class="literal-block">
flickr = flickrapi.FlickrAPI(api_key, api_secret, token=token)
</pre>
<p>It won't be stored in the on-disk token cache - which is a good thing,
since</p>
<blockquote>
<ol class="upperalpha simple">
<li>you don't know who the user is, so you wouldn't be able to
retrieve the appropriate tokens for visiting users.</li>
<li>the tokens are stored in cookies, so there is no need to store
them in another place.</li>
</ol>
</blockquote>
</div>
<div class="section" id="preventing-usage-of-on-disk-token-cache">
<h2><a class="toc-backref" href="#id13">3.3&nbsp;&nbsp;&nbsp;Preventing usage of on-disk token cache</a></h2>
<p>Another way of preventing the storage of tokens is to pass
<tt class="docutils literal">store_token=False</tt> as the constructor parameter. Use this if you
want to be absolutely sure that the FlickrAPI instance doesn't use any
previously stored tokens, nor that it will store new tokens.</p>
</div>
<div class="section" id="example-using-django">
<h2><a class="toc-backref" href="#id14">3.4&nbsp;&nbsp;&nbsp;Example using Django</a></h2>
<p>Here is a simple example in <a class="reference external" href="http://www.djangoproject.com/">Django</a>:</p>
<pre class="literal-block">
import flickrapi
from django.conf import settings
from django.http import HttpResponseRedirect, HttpResponse

import logging
logging.basicConfig()

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

def require_flickr_auth(view):
    '''View decorator, redirects users to Flickr when no valid
    authentication token is available.
    '''

    def protected_view(request, *args, **kwargs):
        if 'token' in request.session:
            token = request.session['token']
            log.info('Getting token from session: %s' % token)
        else:
            token = None
            log.info('No token in session')

       f = flickrapi.FlickrAPI(settings.FLICKR_API_KEY,
               settings.FLICKR_API_SECRET, token=token,
               store_token=False)

        if token:
            # We have a token, but it might not be valid
            log.info('Verifying token')
            try:
                f.auth_checkToken()
            except flickrapi.FlickrError:
                token = None
                del request.session['token']

        if not token:
            # No valid token, so redirect to Flickr
            log.info('Redirecting user to Flickr to get frob')
            url = f.web_login_url(perms='read')
            return HttpResponseRedirect(url)

        # If the token is valid, we can call the decorated view.
        log.info('Token is valid')

        return view(request, *args, **kwargs)

    return protected_view

def callback(request):
    log.info('We got a callback from Flickr, store the token')

   f = flickrapi.FlickrAPI(settings.FLICKR_API_KEY,
           settings.FLICKR_API_SECRET, store_token=False)

    frob = request.GET['frob']
    token = f.get_token(frob)
    request.session['token'] = token

    return HttpResponseRedirect('/content')

&#64;require_flickr_auth
def content(request):
    return HttpResponse('Welcome, oh authenticated user!')
</pre>
<p>Every view that calls an authenticated Flickr method should be
decorated with <tt class="docutils literal">&#64;require_flickr_auth</tt>. For more information on
function decorators, see <a class="reference external" href="http://www.python.org/dev/peps/pep-0318/">PEP 318</a>.</p>
<p>The <tt class="docutils literal">callback</tt> view should be called when the user is sent to the
callback URL as defined in your Flickr API key. The key and secret
should be configured in your settings.py, in the properties
<tt class="docutils literal">FLICKR_API_KEY</tt> and <tt class="docutils literal">FLICKR_API_SECRET</tt>.</p>
</div>
</div>
<div class="section" id="uploading-or-replacing-images">
<h1><a class="toc-backref" href="#id15">4&nbsp;&nbsp;&nbsp;Uploading or replacing images</a></h1>
<p>Transferring images requires special attention since they have to
send a lot of data. Therefore they also are a bit different than
advertised in the Flickr API documentation.</p>
<div class="section" id="flickr-upload">
<h2><a class="toc-backref" href="#id16">4.1&nbsp;&nbsp;&nbsp;flickr.upload(...)</a></h2>
<p>The <tt class="docutils literal"><span class="pre">flickr.upload(...)</span></tt> method has the following parameters:</p>
<dl class="docutils">
<dt><tt class="docutils literal">filename</tt></dt>
<dd>The filename of the image. The image data is read from this file.</dd>
<dt><tt class="docutils literal">title</tt></dt>
<dd>The title of the photo</dd>
<dt><tt class="docutils literal">description</tt></dt>
<dd>The description of the photo</dd>
<dt><tt class="docutils literal">tags</tt></dt>
<dd><p class="first">Space-delimited list of tags. Tags that contain spaces need to be
quoted. For example:</p>
<pre class="literal-block">
tags='''Amsterdam &quot;central station&quot;'''
</pre>
<p class="last">Those are two tags, &quot;Amsterdam&quot; and &quot;central station&quot;.</p>
</dd>
<dt><tt class="docutils literal">is_public</tt></dt>
<dd>&quot;1&quot; if the photo is public, &quot;0&quot; if it is private. The default is
public.</dd>
<dt><tt class="docutils literal">is_family</tt></dt>
<dd>&quot;1&quot; if the private photo is visible for family, &quot;0&quot; if not. The
default is not.</dd>
<dt><tt class="docutils literal">is_friend</tt></dt>
<dd>&quot;1&quot; if the private photo is visible for friends, &quot;0&quot; if not. The
default is not.</dd>
<dt><tt class="docutils literal">callback</tt></dt>
<dd><p class="first">This should be a method that receives two parameters, <tt class="docutils literal">progress</tt>
and <tt class="docutils literal">done</tt>. The callback method will be called every once in a
while during uploading. Example:</p>
<pre class="last literal-block">
def func(progress, done):
    if done:
        print &quot;Done uploading&quot;
    else:
        print &quot;At %s%%&quot; % progress

flickr.upload(filename='test.jpg', callback=func)
</pre>
</dd>
<dt><tt class="docutils literal">format</tt></dt>
<dd>The response format. This <em>must</em> be either <tt class="docutils literal">rest</tt> or one of the
parsed formats <tt class="docutils literal">etree</tt> / <tt class="docutils literal">xmlnode</tt>.</dd>
</dl>
</div>
<div class="section" id="flickr-replace">
<h2><a class="toc-backref" href="#id17">4.2&nbsp;&nbsp;&nbsp;flickr.replace(...)</a></h2>
<p>The <tt class="docutils literal"><span class="pre">flickr.replace(...)</span></tt> method has the following parameters:</p>
<dl class="docutils">
<dt><tt class="docutils literal">filename</tt></dt>
<dd>The filename of the image.</dd>
<dt><tt class="docutils literal">photo_id</tt></dt>
<dd>The identifier of the photo that is to be replaced. Do not use
this when uploading a new photo.</dd>
<dt><tt class="docutils literal">format</tt></dt>
<dd>The response format. This <em>must</em> be either <tt class="docutils literal">rest</tt> or one of the
parsed formats <tt class="docutils literal">etree</tt> / <tt class="docutils literal">xmlnode</tt>.</dd>
</dl>
<p>Only the image itself is replaced, not the other data (title, tags,
comments, etc.).</p>
</div>
</div>
<div class="section" id="unicode-and-utf-8">
<h1><a class="toc-backref" href="#id18">5&nbsp;&nbsp;&nbsp;Unicode and UTF-8</a></h1>
<p>Flickr expects every text to be encoded in UTF-8. The Python Flickr
API can help you in a limited way. If you pass a <tt class="docutils literal">unicode</tt> string,
it will automatically be encoded to UTF-8 before it's sent to Flickr.
This is the preferred way of working, and is also forward-compatible
with the upcoming Python 3.</p>
<p>If you do not use <tt class="docutils literal">unicode</tt> strings, you're on your own, and you're
expected to perform the UTF-8 encoding yourself.</p>
<p>Here is an example:</p>
<pre class="literal-block">
flickr.photos_setMeta(photo_id='12345',
                      title=u'Money',
                      description=u'Around \u20ac30,-')
</pre>
<p>This sets the photo's title to &quot;Money&quot; and the description to &quot;Around
€30,-&quot;.</p>
</div>
<div class="section" id="caching-of-flickr-api-calls">
<h1><a class="toc-backref" href="#id19">6&nbsp;&nbsp;&nbsp;Caching of Flickr API calls</a></h1>
<p>There are situations where you call the same Flickr API methods over
and over again. An example is a web page that shows your latest ten
sets. In those cases caching can significantly improve performance.</p>
<p>The FlickrAPI module comes with its own in-memory caching framework.
By default it caches at most 200 entries, which time out after 5
minutes. These defaults are probably fine for average use. To use the
cache, just pass <tt class="docutils literal">cache=True</tt> to the constructor:</p>
<pre class="literal-block">
flickr = flickrapi.FlickrAPI(api_key, cache=True)
</pre>
<p>To tweak the cache, instantiate your own instance and pass it some
constructor arguments:</p>
<pre class="literal-block">
flickr = flickrapi.FlickrAPI(api_key, cache=True)
flickr.cache = flickrapi.SimpleCache(timeout=300, max_entries=200)
</pre>
<p><tt class="docutils literal">timeout</tt> is in seconds, <tt class="docutils literal">max_entries</tt> in number of cached
entries.</p>
<div class="section" id="using-the-django-caching-framework">
<h2><a class="toc-backref" href="#id20">6.1&nbsp;&nbsp;&nbsp;Using the Django caching framework</a></h2>
<p>The caching framework was designed to have the same interface as the
<a class="reference external" href="http://www.djangoproject.com/documentation/cache/#the-low-level-cache-api">Django low-level cache API</a> - thanks to those guys for designing a
simple and effective cache. The result is that you can simply plug the
Django caching framework into FlickrAPI, like this:</p>
<pre class="literal-block">
from django.core.cache import cache
flickr = flickrapi.FlickrAPI(api_key, cache=True)
flickr.cache = cache
</pre>
<p>That's all you need to enable a wealth of caching options, from
database-backed cache to multi-node in-memory cache farms.</p>
</div>
</div>
<div class="section" id="requirements-and-compatibility">
<h1><a class="toc-backref" href="#id21">7&nbsp;&nbsp;&nbsp;Requirements and compatibility</a></h1>
<p>The Python Flickr API only uses built-in Python modules. It is
compatible with Python 2.4 and newer.</p>
<p>Usage of the &quot;etree&quot; format requires Python 2.5 or newer.</p>
<p>Rendering the documentation requires <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a>.</p>
</div>
<div class="section" id="links">
<h1><a class="toc-backref" href="#id22">8&nbsp;&nbsp;&nbsp;Links</a></h1>
<ul class="simple">
<li><a class="reference external" href="http://flickrapi.sourceforge.net/">Python Flickr API interface</a></li>
<li><a class="reference external" href="http://www.flickr.com/">Flickr</a></li>
<li><a class="reference external" href="http://www.flickr.com/services/api/">Flickr API documentation</a></li>
</ul>
</div>
</div>
</body>
</html>