Skip to main content

PredictHQ Event Intelligence

Project description

For the latest source, discussions, bug reports, etc., please visit the GitHub repository

PredictHQ logo

PredictHQ API Client for Python

Version build PyPI package Coverage Status

PredictHQ is the demand intelligence company combining real-world events into one global source of truth to help businesses better understand demand and plan for the future.

Installation

The PredictHQ Python client is distributed as a pip package. You can simply install it by running

pip install predicthq

Usage

We support all the endpoints available in our API.

  • oauth2
  • accounts
  • broadcasts
  • events
  • features
  • places

Please refer to our API Documentation for a description of each endpoint.

The usecases/ folder is a good starting point to get familiar with the Python SDK. You can also review the tests for a kitchen sink of all the parameters available per endpoint.

Pagination

Additional examples are available in usecases/pagination.py file.

By default the search() method only returns the first page of results, with a default page size of 10.

from predicthq import Client

phq = Client(access_token="abc123")


for event in phq.events.search():
    print(event.rank, event.category, event.title, event.start.strftime("%Y-%m-%d"))

You can chain the iter_all() generator to iterate over all your events.

for event in phq.events.search().iter_all():
    print(event.rank, event.category, event.title, event.start.strftime("%Y-%m-%d"))

Events endpoint

Additional examples are available in usecases/events folder.

The following example searches for the 'Katy Perry' events (full text search) with rank level of 4 or 5 (rank >= 60) in the concerts category.

from predicthq import Client

phq = Client(access_token="abc123")


for event in phq.events.search(q='Katy Perry', rank_level=[4, 5], category='concerts'):
    print(event.rank, event.category, event.title, event.start.strftime("%Y-%m-%d"))

Please refer to our Events endpoint documentation for the lists of search parameters and event fields available.

Broadcasts endpoint

Additional examples are available in usecases/broadcasts folder.

The following example searches for the broadcasts with PHQ viewership gte 100 and with event (the physical event the broadcast links to) label 'nfl'.

from predicthq import Client

phq = Client(access_token="abc123")


for broadcast in phq.broadcasts.search(phq_viewership__gte=100, event__label='nfl'):
    print(broadcast.event.title, broadcast.phq_viewership, broadcast.event.labels, broadcast.dates.start.strftime("%Y-%m-%d"))

Please refer to our Broadcasts endpoint documentation for the lists of search parameters and broadcast fields available.

Places endpoint

Additional examples are available in usecases/places.py file.

The following example searches for the 'New York' places (full text search) in the US.

from predicthq import Client

phq = Client(access_token="abc123")


for place in phq.places.search(q="New York", country="US"):
    print(place.id, place.name, place.type, place.location)

Please refer to our Places endpoint documentation for the lists of search parameters and place fields available.

Features endpoint

The following example obtain features of events which are active between 2017-12-31 and 2018-01-02, with place_id 4671654.

Requested features:

  • rank_levels for public_holidays
  • count and median of sporting events which has a phq_rank greater than 50

By place_id list (e.g. Austin):

from predicthq import Client

phq = Client(access_token="abc123")


for feature in phq.features.obtain_features(
        active__gte="2017-12-31",
        active__lte="2018-01-02",
        location__place_id=[4671654],
        phq_rank_public_holidays=True,
        phq_attendance_sports__stats=["count", "median"],
        phq_attendance_sports__phq_rank={
            "gt": 50
        }
):
    print(feature.date, feature.phq_attendance_sports.stats.count, feature.phq_rank_public_holidays.rank_levels)

by geo:

from predicthq import Client

phq = Client(access_token="abc123")


for feature in phq.features.obtain_features(
        active__gte="2017-12-31",
        active__lte="2018-01-02",
        location__geo={
            "lon": -97.74306,
            "lat": 30.26715,
            "radius": "150km"
        },
        phq_rank_public_holidays=True,
        phq_attendance_sports__stats=["count", "median"],
        phq_attendance_sports__phq_rank={
            "gt": 50
        }
):
    print(feature.date, feature.phq_attendance_sports.stats.count, feature.phq_rank_public_holidays.rank_levels)

The following example obtains features of broadcasts which are active between 2017-12-31 and 2018-01-02, with place_id 4671654

Requested features:

  • count and median of broadcasts which start between 9am - 11am and have a phq_rank greater than 50
from predicthq import Client

phq = Client(access_token="abc123")


for feature in phq.features.obtain_features(
        active__gte="2017-12-31",
        active__lte="2018-01-02",
        hour_of_day_start__gt=9,
        hour_of_day_start__lte=11,
        location__place_id=[4671654],
        phq_viewership_sports_american_football__stats=["count", "median"],
        phq_viewership_sports_american_football__phq_rank={
            "gt": 50
        }
):
    print(feature.date, feature.phq_viewership_sports_american_football.stats.count, feature.phq_viewership_sports_american_football.stats.median)

Please refer to our Features endpoint documentation for the lists of supported features and response fields available.

Config parameters

We support some config parameters for additional flexibility.

Supported config parameters:

  • verify_ssl
from predicthq import Client

phq = Client(access_token="abc123")


# double underscore syntax
for event in phq.events.search(config__verify_ssl=False):
    print(event.rank, event.category, event.title, event.start.strftime("%Y-%m-%d"))

# dictionary syntax
for event in phq.events.search(config={"verify_ssl": False}):
    print(event.rank, event.category, event.title, event.start.strftime("%Y-%m-%d"))

Running Tests

pip install tox
tox

Found a Bug?

Please log an issue.

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

predicthq-1.11.3.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

predicthq-1.11.3-py2.py3-none-any.whl (22.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file predicthq-1.11.3.tar.gz.

File metadata

  • Download URL: predicthq-1.11.3.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for predicthq-1.11.3.tar.gz
Algorithm Hash digest
SHA256 6a8dead7be4f3fd9a6dd8cb10b4a6dab62113a3e7e35f0e834fd194efba9909a
MD5 7da8aff24294104f09a5cd391dbb6fc5
BLAKE2b-256 e7844bb92df563b87e054a1f4779299c1ce80f788a3e4d169127818f00acc966

See more details on using hashes here.

File details

Details for the file predicthq-1.11.3-py2.py3-none-any.whl.

File metadata

  • Download URL: predicthq-1.11.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for predicthq-1.11.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 432ca60f0edae889ed6fe9a2bb116ab565c8e9024b1994e7471e678d05d5d859
MD5 0f5891d81b35db8fb808f98c5b89d0aa
BLAKE2b-256 158c8fc2a25b4351d306c368243f77930b13181d92df81a57b0512d5c84948c5

See more details on using hashes here.

Supported by

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