gvellut / flickr2kml

Simple command-line tool to generate a KML from the georeferenced photos in a Flickr album
GNU General Public License v3.0
1 stars 0 forks source link
flickr geo kml

flickr2kml

Simple command-line tool to generate a KML from the georeferenced photos in a Flickr album

Install

The tool requires Python 3.6+.

To install, launch :

pip install flickr2kml

The command above will install the flickr2kml Python library and its dependencies. The library includes a command-line script, also named flickr2kml, whose functionality is described below.

Usage

Use the --help flag to get an overview of the arguments:

~$ flickr2kml --help
Usage: flickr2kml [OPTIONS] OUTPUT_KML

  Generate a KML file for the georeferenced photos in a Flickr album

Options:
  -f, --flickr_album TEXT   URL of Flickr album  [required]
  -t, --template TEXT       Choice of format for the placemark description,
                            either predefined (gearth [default], mymaps) or as
                            a path to a custom template

  -n, --name_template TEXT  Choice of format for the placemark name, as a path
                            to a custom template [default: empty]

  -a, --template_arg TEXT   Variable to pass to the template (multiple
                            possible)

  -p, --pushpin             Flag to make each placemark a simple pushpin
                            instead of a small image

  --config FILE             Path to optional config file for the Flickr API
                            credentials [default :
                            /Users/guilhem/Library/Application
                            Support/flickr2kml/flickr_api_credentials.txt]

  --api_key TEXT            Flickr API key  [required]
  --api_secret TEXT         Flickr API secret  [required]
  -d, --debug               Flag to activate debug mode
  --help                    Show this message and exit.

The URL of the Flickr album must be something like https://www.flickr.com/photos/o_0/albums/72157716704507802

Name and description

There are 2 different formats for the description fields in the KML placemarks. I personnally generate KML for use either in Google Earth (gearth format) or Google My Maps (mymaps format). They don't present the content of the fields the same way (nor support the same features). The default is the Google Earth format.

By default, the KML names are left empty.

Template

It is also possible to configure custom name and description formats by passing a path to a Jinja2 template file with the -t / --template option (for the description) and the -n / --name_template option (for the name).

Description

The description template must return a HTML fragment (a simple text wthout any markup will do though).

The 2 predefined templates can be used as a starting point. I have also added a sample that uses the photo description field from the Flickr API:

Name

The name template must return a text (HTML is not supported).

A sample for the name is available here:

It simply outputs a formatted date taken. The date format specification is the one used by Python from the datetime package.

Template arguments

Besides fields obtained from the Flickr API (which change for each photo; See section below), it is also possible to pass arguments to the template, which will stay the same for every photo. The -a / --template_arg option can be used multiple times for multiple arguments.

Example:

-a MYARG=value -a SIZE=350

The SIZE argument, if not redefined on the command-line, is set to 500 by default i.e. equivalent to -a SIZE=500. The reason is that it is used by the predefined templates: There would be an error if not present. Custom templates are free not to use it though.

Fields

Here are the main fields obtained from the Flickr API and available to the templates:

Additional fields (computed by flickr2kml) are:

API permission

Log in to Flickr and authorize the application

The first time the tool is run on the command-line, a token for accessing the API must be generated. It is pretty straightforward:

After that process, an access token will be cached inside an oauth-tokens.sqlite file stored on the same directory as the default location of the API key config file (which can vary depending on the OS ; See above).

As long as the token is cached, there will be no need no login again for subsequent runs (that is until the token expires).

The tool will run with the permission of the user that logged in. In order to switch user, the oauth-tokens.sqlite will need to be deleted.

Examples

Simple

flickr2kml -f https://www.flickr.com/photos/o_0/albums/72157716046011583 thiou2020.kml

If the API key and secret come from a config file, there is no need to pass them as argument.

With templates

flickr2kml thiou2020.kml -f https://www.flickr.com/photos/o_0/albums/72157716046011583 -n mynametemplate.txt -t mydesctemplate.html -a SIZE=350