Skip to main content

Look up information about photos and collections of photos from Flickr

Project description

flickr-photos-api

This is a library for using the Flickr API at the Flickr Foundation.

It's not a general-purpose Flickr API library. It provides a subset of Flickr API methods with the following goals:

  • Provide reusable code that can be called across all our projects.
  • Keep all the details of dealing with the Flickr API in one place.
  • Apply types to all results, so the Flickr API can be used safely in a typed context.

Design

Using the Flickr API is fairly simple: you make an HTTP GET to https://api.flickr.com/services/rest/?api_key={api_key} and pass one or more URL query parameters. One of those query parameters must be method, then you add other parameters depending on the API method.

There's an abstract class that represents this interface:

import abc
from xml.etree import ElementTree as ET


class FlickrApi(abc.ABC):
    @abc.abstractmethod
    def call(self, method: str, params: dict[str, str] | None = None) -> ET.Element:
        return NotImplemented

The idea is that you can extend this class with "method" classes that wrap specific API methods, and make HTTP GET calls through this call() method:

class GetSinglePhotoMethods(FlickrApi):
    def get_single_photo(self, photo_id: str) -> ET.Element:
        return self.call(method="flickr.photos.getInfo", params={"photo_id": photo_id})

This separates the code for making HTTP requests and separating the responses.

The library includes a single implementation of FlickrApi for making HTTP requests, using httpx, but you could swap it out if you wanted to use e.g. requests or urllib3. This httpx implementation is the default implementation.

Examples

>>> from flickr_api import FlickrApi
>>> api = FlickrApi.with_api_key(api_key="…", user_agent="…")

>>> photo = api.get_single_photo(photo_id="14898030836")

>>> photo
{'id': '14898030836', 'title': 'NASA Scientists Says', …}

>>> photo["license"]
{'id': 'cc-by-2.0', 'label': 'CC BY 2.0', 'url': 'https://creativecommons.org/licenses/by/2.0/'}

>>> photo["url"]
'https://www.flickr.com/photos/lassennps/14898030836/'

Usage

  1. Install flickr-photos-api from PyPI:

    $ pip install flickr-photos-api
    
  2. Construct an instance of FlickrApi.

    There are two approaches you can use:

    • You can make unauthenticated requests by passing a Flickr API key in the headers:

      import httpx
      
      client = httpx.Client(params={"api_key": api_key})
      
      api = FlickrApi(cleint)
      
    • You can make authenticated requests by using OAuth 1.0a:

      from authlib.integrations.httpx_client import OAuth1Client
      
      client=OAuth1Client(
          client_id=client_id,
          client_secret=client_secret,
          token=token,
          token_secret=token_secret,
      )
      
      api = FlickrApi(client)
      
  3. Call methods on FlickrApi. There's no complete list of methods right now; look at the files X_methods.py in the api directory.

    Methods that return collections of photos also support page and per_page parameters to control pagination.

Development

If you want to make changes to the library, there are instructions in CONTRIBUTING.md.

License

This project is dual-licensed as Apache-2.0 and MIT.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flickr_photos_api-3.12.1.tar.gz (40.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flickr_photos_api-3.12.1-py3-none-any.whl (42.0 kB view details)

Uploaded Python 3

File details

Details for the file flickr_photos_api-3.12.1.tar.gz.

File metadata

  • Download URL: flickr_photos_api-3.12.1.tar.gz
  • Upload date:
  • Size: 40.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for flickr_photos_api-3.12.1.tar.gz
Algorithm Hash digest
SHA256 494e45ad8259b11c738c2f5935038e653af9356e61266a99f71c660f34da6122
MD5 580c19d730be770b54a68bbd6f8075c6
BLAKE2b-256 7b14e00ba90cc0f38fe73057b14ff5b54387e52da5f3b8661debfd6eca54ff3e

See more details on using hashes here.

File details

Details for the file flickr_photos_api-3.12.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flickr_photos_api-3.12.1-py3-none-any.whl
Algorithm Hash digest
SHA256 90e5735051ad1bbab034c74491baa0f1d9c5b417bd3706018e5e528213994bfe
MD5 0b4799004a0200759ec5a0b757548a27
BLAKE2b-256 85f1c16ecf0dfa790664ccd6807464e6d594932da94a12eb3dbf7601897092e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page