Skip to main content

Twitter Accounts Activity API Client Library for Python

Project description

Twitivity

Premium PyPI - Python Version Downloads PyPI - License

Twitter Accounts Activity API Client Library for Python.

Account Activity API allows you to subscribe to user activities. Unlike Twitter's REST API or the Streaming API, the Account Activity API delivers data through webhook connections. Which makes it faster and allows it to deliver Twitter data to you in real-time. You can subscribe to these user activities.

Getting Started

  • Apply for a Twitter Developer Account
  • Create an application, fill in the required fields, the callback URL is your domain name with an added endpoint, for example https://yourdomain.com/listener. Twitter will later use this URL to send you account activity data. Make sure to enable "Read, Write and Direct messages" permission.
  • Navigate to the Dev environment section and setup a dev environment for the Account Activity API. Name a dev environment label of your choosing and select your app.

The next step is to register your webhook URL. Twitter will send a GET request with Challenge-Response Check or CRC token to verify you are the owner of the app and the webhook URL. To validate, an encrypted response token based on your consumer key and the CRC token has to be sent back to Twitter. Upon successful validation, registration of the webhook URL and subscription. Twitter will send data to this endpoint (the webhook URL) as a POST request.

Why Twitivity?

Twitivity does all the heavy lifting under the hood. All you have to do is to create an app and set up a dev environment. Run the application and concentrate on what's really important — building your app.

  • Performs challenge-response check validation
  • Registers webhook URL.
  • Subscribes to current user's context
  • Receives Twitter Account Activity in real-time

Usage

Ngrok is a handy tool to try out the API locally, on your machine. Install and run ngrok and replace your app's URL and callback URL with the link ngrok provides. Make sure to use the one with https.

~$ ./ngrok http 5000

Stream events in real time.

# stream_events.py

>>> from twitivity import Event
>>> import json

>>> class StreamEvent(Event):
     CALLBACK_URL: str = "https://yourdomain.com/listener"

     def on_data(self, data: json) -> None:
         # process data

>>> stream_events = StreamEvent()
>>> stream_events.listen()

Configuration

The configuration below only has to be done once before running the application for the first time.

Store the credentials as environment variables.

App :arrow_right: Details :arrow_right: Keys and Tokens

~$ export consumer_key=API_KEY
~$ export consumer_secret=API_SECRET_KEY
~$ export access_token=ACCESS_TOKEN
~$ export access_token_secret=ACCESS_TOKEN_SECRET
~$ export env_name=ENV_NAME # this is the dev environment label name you choose.

Register & Subscribe

To register the webhook URL and subscribe to activities, run both programs in parallel (first stream_events.py then configure.py). This will register the webhook URL and subscribe to the user's activities.

# configure.py
>>> from twitivity import Activity

>>> account_activity = Activity()
>>> account_activity.register_webhook("https://youdomain.com/listener")
>>> account_activity.subscribe()

# Response
{
  'id': '1198870971131686912', # webhook id
  'url': 'https://yourdomain.com/listener',
  'valid': True,
  'created_timestamp': '2019-11-25 07:48:08 +0000'
}

Installation

~$ pip3 install twitivity

Deploying

Documentation on how to deploy it on web servers.

Supported Versions: Python 3.6, Python 3.7 and Python 3.8

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

twitivity-0.3.5.tar.gz (5.4 kB view details)

Uploaded Source

File details

Details for the file twitivity-0.3.5.tar.gz.

File metadata

  • Download URL: twitivity-0.3.5.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for twitivity-0.3.5.tar.gz
Algorithm Hash digest
SHA256 76fc2541ea31cfd46932cd9d182f0f3703deda4a27cb75e1a3b6ab035830bf9b
MD5 ba6962ff91c6878a3b5281f84a2a9928
BLAKE2b-256 8171d4d7f1e94347aa16c6d2b1e4f02b9d04fe3228d33f43fb9d020eb92dc86f

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