Package: predicthq
For the latest source, discussions, bug reports, etc., please visit theGitHub repositoryPredictHQ API Client for PythonPredictHQis 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.InstallationThe PredictHQ Python client is distributed as a pip package. You can simply install it by runningpipinstallpredicthqMigrating from version <= 2.4.0If you are migrating to version 3.0.0 or above from an earlier version, please check thebreaking changes details.UsageWe support all the endpoints available in our API.oauth2accountsbroadcastseventsfeaturesplacesradiusPlease refer to ourAPI Documentationfor a description of each endpoint.Theusecases/folder is a good starting point to get familiar with the Python SDK.
You can also review thetestsfor a kitchen sink of all the parameters available per endpoint.PaginationAdditional examples are available inusecases/pagination.pyfile.By default thesearch()method only returns the first page of results, with a default page size of 10.frompredicthqimportClientphq=Client(access_token="abc123")foreventinphq.events.search():print(event.rank,event.category,event.title,event.start.strftime("%Y-%m-%d"))You can chain theiter_all()generator to iterate overallyour events.foreventinphq.events.search().iter_all():print(event.rank,event.category,event.title,event.start.strftime("%Y-%m-%d"))Events endpointAdditional examples are available inusecases/eventsfolder.The following example searches for theKaty Perryevents (full text search) with rank level of 4 or 5 (rank >= 60) in the concerts category.frompredicthqimportClientphq=Client(access_token="abc123")foreventinphq.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 ourEvents endpoint documentationfor the lists of search parameters and event fields available.Broadcasts endpointAdditional examples are available inusecases/broadcastsfolder.The following example searches for the broadcasts with PHQ viewership gte 100 and with event (the physical event the broadcast links to) labelnfl.frompredicthqimportClientphq=Client(access_token="abc123")forbroadcastinphq.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 ourBroadcasts endpoint documentationfor the lists of search parameters and broadcast fields available.Places endpointAdditional examples are available inusecases/places.pyfile.The following example searches for theNew Yorkplaces (full text search) in the US.frompredicthqimportClientphq=Client(access_token="abc123")forplaceinphq.places.search(q="New York",country="US"):print(place.id,place.name,place.type,place.location)Please refer to ourPlaces endpoint documentationfor the lists of search parameters and place fields available.Features endpointThe 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_holidayscount and median of sporting events which has a phq_rank greater than 50By place_id list (e.g. Austin):frompredicthqimportClientphq=Client(access_token="abc123")forfeatureinphq.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:frompredicthqimportClientphq=Client(access_token="abc123")forfeatureinphq.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 4671654Requested features:count and median of broadcasts which start between 9am - 11am and have aphq_rankgreater than 50frompredicthqimportClientphq=Client(access_token="abc123")forfeatureinphq.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 ourFeatures endpoint documentationfor the lists of supported features and response fields available.Radius endpointThe following example obtain suggested radius to be used when retrieving events for location {"lat": 45.5051, "lon": -122.6750} and generic industry.Additional examples are available inusecases/radiusfolder.frompredicthqimportClientphq=Client(access_token="abc123")suggested_radius=phq.radius.search(location__origin="45.5051,-122.6750")print(suggested_radius.radius,suggested_radius.radius_unit,suggested_radius.location.to_dict())Config parametersWe support someconfigparameters for additional flexibility.Supported config parameters:verify_sslfrompredicthqimportClientphq=Client(access_token="abc123")# double underscore syntaxforeventinphq.events.search(config__verify_ssl=False):print(event.rank,event.category,event.title,event.start.strftime("%Y-%m-%d"))# dictionary syntaxforeventinphq.events.search(config={"verify_ssl":False}):print(event.rank,event.category,event.title,event.start.strftime("%Y-%m-%d"))Running Testspipinstalltox
toxFound a Bug?Pleaselog an issue.