Skip to main content

A CKAN extension that enables users to post a tweet every time a dataset is created or updated.

Project description

ckanext-twitter

Travis Coveralls CKAN Python

A CKAN extension that enables users to post a tweet every time a dataset is created or updated.

Overview

This extension connects a CKAN instance to a Twitter account so that when a dataset is updated or created (i.e. the after_update hook is called), the user has the option to post a tweet about the activity.

Installation

Path variables used below:

  • $INSTALL_FOLDER (i.e. where CKAN is installed), e.g. /usr/lib/ckan/default
  • $CONFIG_FILE, e.g. /etc/ckan/default/development.ini
  1. Clone the repository into the src folder:
cd $INSTALL_FOLDER/src
git clone https://github.com/NaturalHistoryMuseum/ckanext-twitter.git
  1. Activate the virtual env:
. $INSTALL_FOLDER/bin/activate
  1. Install the requirements from requirements.txt:
cd $INSTALL_FOLDER/src/ckanext-twitter
pip install -r requirements.txt
  1. Run setup.py:
cd $INSTALL_FOLDER/src/ckanext-twitter
python setup.py develop
  1. Add 'twitter' to the list of plugins in your $CONFIG_FILE:
ckan.plugins = ... twitter
  1. Add a tweet block to read_base.html:

    {% block tweet %}
    {{ super() }}
    {% endblock %}
    
  2. Optionally, override the styling of the block by creating an ajax_snippets/edit_tweet.html file.

Configuration

These are the options that can be specified in your .ini config file. The only required options are the twitter credentials. Everything else has a sensible default set.

[REQUIRED]

Name Description Options
ckanext.twitter.consumer_key Your Twitter consumer key
ckanext.twitter.consumer_secret Your Twitter consumer secret
ckanext.twitter.token_key Your Twitter token key
ckanext.twitter.token_secret Your Twitter token secret

All of these can be obtained by creating a single-user app at apps.twitter.com. They can be found on the "keys and access tokens" tab when viewing your app.

Tweet Templates

Tweets are generated using Jinja2 and use tokens derived from the package dictionary. See Usage for more detail.

Name Description Default
ckanext.twitter.new Template for tweets about new datasets New dataset: "{{ title }}" by {{ author }} ({%- if records != 0 -%} {{ records }} records {%- else -%} {{ resources }} resource {%- endif -%}).
ckanext.twitter.updated Template for tweets about updated datasets Updated dataset: "{{ title }}" by {{ author }} ({%- if records != 0 -%} {{ records }} records {%- else -%} {{ resources }} resource {%- endif -%}).

If your config is created dynamically using Jinja2, you will have to wrap any custom template in {% raw %}{% endraw %} tags and add a newline after it, e.g.:

ckanext.twitter.new = {% raw %}{{ title }} by {{ author }} ({{ records }} records) has just been published!{% endraw %}

ckanext.twitter.consumer_key = {{ twitter.consumer_key }}
ckanext.twitter...

Other options

Name Description Options Default
ckanext.twitter.debug Is in debug mode; overrides global debug flag if specified True, False False
ckanext.twitter.hours_between_tweets Number of hours between tweets about the same dataset (to prevent spamming) 24
ckanext.twitter.disable_edit If true, users will not be able to edit the tweet about their dataset before it is posted (though they can still decide not to post it) True, False False

Usage

Tweet Templates

Token values for the tweet templates will come from a simplified package dictionary. In these, any collection values (i.e. lists and dictionaries) have been replaced with the number of items, the author list has been significantly shortened, and any long strings will be shortened to fit into the tweet character limit (currently set at 140).

For example, if the package dictionary is:

{
  'author': 'Dippy Diplodocus, Sophie Stegosaurus',
  'author_email': None,
  'dataset_category': ['Citizen Science'],
  'doi': 'DOI_VALUE',
  'license_title': 'Creative Commons Attribution',
  'organization': {'description': '', 'name': 'nhm', 'is_organization': True, 'state': 'active'},
  'resources': [
                  {'mimetype': 'image/jpeg', 'name': 'resource_1.jpg', 'format': 'JPEG'},
                  {'mimetype': 'image/jpeg', 'name': 'resource_2.jpg', 'format': 'JPEG'}
               ],
  'title': 'Dataset Name'
}

Then the tokenised dictionary would be:

{
  'author': 'Diplodocus et al.',  # just the surname of the first author
  # author_email was None so it's excluded
  'dataset_category': 1,  # lists are counted
  'doi': 'DOI_VALUE',  # simple string values stay the same
  'license_title': 'Creative Commons Attribution',
  'organization': 4,  # dicts are also counted
  'resources': 2,
  'title': 'Dataset Name'
}

And if you had defined the tweet template as:

New dataset: "{{ title }}" by {{ author }} ({{ resources }} resources).

Your tweet would then read:

New dataset: "Dataset Name" by Diplodocus et al. (2 resources)

Testing

Note that the tests shouldn't make any calls to Twitter's API and won't post any tweets!

To run the tests in this extension, there is a Docker compose configuration available in this repository to make it easy.

To run the tests against ckan 2.9.x on Python3:

  1. Build the required images
docker-compose build
  1. Then run the tests. The root of the repository is mounted into the ckan container as a volume by the Docker compose configuration, so you should only need to rebuild the ckan image if you change the extension's dependencies.
docker-compose run ckan

The ckan image uses the Dockerfile in the docker/ folder which is based on openknowledge/ckan-dev:2.9.

Project details


Download files

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

Source Distribution

ckanext-twitter-2.1.0.tar.gz (37.2 kB view details)

Uploaded Source

Built Distributions

ckanext_twitter-2.1.0-py3.10.egg (15.7 kB view details)

Uploaded Source

ckanext_twitter-2.1.0-py3-none-any.whl (29.2 kB view details)

Uploaded Python 3

File details

Details for the file ckanext-twitter-2.1.0.tar.gz.

File metadata

  • Download URL: ckanext-twitter-2.1.0.tar.gz
  • Upload date:
  • Size: 37.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for ckanext-twitter-2.1.0.tar.gz
Algorithm Hash digest
SHA256 dd728595130de912a2d12af0811ad148463557ec64342326879b6533e2340db4
MD5 0de510692d98fb582ac289b601b78009
BLAKE2b-256 d016cef9a1ef2fba2f3c74f83ecb79af75278bf4f55e447e77fdd8bb64f9df1a

See more details on using hashes here.

File details

Details for the file ckanext_twitter-2.1.0-py3.10.egg.

File metadata

  • Download URL: ckanext_twitter-2.1.0-py3.10.egg
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for ckanext_twitter-2.1.0-py3.10.egg
Algorithm Hash digest
SHA256 0691b29c4ef81022b6036fd9effd3c0ddfd5f130c9c70c958f1570c0cbd5f5f8
MD5 e3407cc61ec9b072d92b3d41ab5190ce
BLAKE2b-256 7bae6be1ccdf443af9292633964cf44006aaefcfd5100444b05b795c59c2a635

See more details on using hashes here.

File details

Details for the file ckanext_twitter-2.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ckanext_twitter-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b002476c99f5b848721c6fa305881aa6d3735f2083a9e139aa97c2e109b2b059
MD5 deef670954c86e2cccf22e65a2d7d6e7
BLAKE2b-256 971d5145d1847d8d42992054f4fcd9953c6bb15a6b0b5e6bcbeb7bfc058dd3d8

See more details on using hashes here.

Supported by

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