Skip to content

Instantly share code, notes, and snippets.

@andyreagan
Last active July 2, 2020 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyreagan/8e701064cfb3360173e5393b3c880481 to your computer and use it in GitHub Desktop.
Save andyreagan/8e701064cfb3360173e5393b3c880481 to your computer and use it in GitHub Desktop.
Hedonometer APIs

Hedonometer APIs

Happiness Time Series

This is the API link: http://hedonometer.org/api/v1/happiness/?format=json&timeseries__title=en_all&date__gte=2019-01-01&limit=1000

You can also query the rt, no_rt versions:

Here’s the top of http://hedonometer.org/api/v1/happiness/?format=json&timeseries__title=en_all&date__gte=2019-01-01&limit=2

{"meta": {"limit": 2, "next": "/api/v1/happiness/?format=json&timeseries__title=en_all&date__gte=2019-01-01&limit=2&offset=2", "offset": 0, "previous": null, "total_count": 525}, "objects": [{"date": "2020-03-19", "frequency": 239061347.0, "happiness": "5.748", "timeseries": "/api/v1/timeseries/3/"}, {"date": "2020-03-20", "frequency": 236448165.0, "happiness": "5.774", "timeseries": "/api/v1/timeseries/3/"}]}

They all have the frequency, that’s the total word count.

Languages

In the above, en denoted english. The API also provide time series of happiness for each of the other langauges on hedonometer.org. To see which are available, look in the drop-down list on hedonometer.org. To the get the shortcode (en, es, etc), look in the URL for each of the time series visualizations after clickig the drop down link.

Events

If you want the annotated events, they are available as well:

http://hedonometer.org/api/v1/events/?format=json&happs__timeseries__title=en_all&happs__date__gte=2020-05-15&limit=1000

(set the date back to 2019-01-01 or whenever you want them starting at).

Here’s what you get from the events api:

{"meta": {"limit": 1000, "next": null, "offset": 0, "previous": null, "total_count": 2}, "objects": [{"happs": {"date": "2020-05-26", "frequency": 249581473.0, "happiness": "5.870", "timeseries": "/api/v1/timeseries/3/"}, "id": 733, "importance": 5, "longer": "Murder of George Floyd during arrest in Minnesota", "resource_uri": "/api/v1/events/733/", "shorter": "Murder of,George Floyd", "wiki": "https://en.wikipedia.org/wiki/Death_of_George_Floyd", "x": -35, "y": 30}, {"happs": {"date": "2020-05-29", "frequency": 344542921.0, "happiness": "5.659", "timeseries": "/api/v1/timeseries/3/"}, "id": 735, "importance": 100, "longer": "Protests of police violence in response to the death of George Floyd", "resource_uri": "/api/v1/events/735/", "shorter": "Protests of,police violence", "wiki": "https://en.wikipedia.org/wiki/Twin_Cities_protests", "x": -50, "y": 50}]}

Using in Python

Example of pulling this down in Python:

import requests
import pandas as pd
import json
uri = 'http://hedonometer.org/api/v1/happiness/?format=json&timeseries__title=en_all&date__gte=2019-01-01&limit=1000'
r = requests.get(uri)
df = pd.DataFrame(json.loads(r.content)['objects'])
df.sort_values('date', inplace=True)
df.head()

The last line shows:

In [19]: df.head()
Out[19]:
          date    frequency happiness             timeseries
37  2019-01-01  145361578.0     6.095  /api/v1/timeseries/3/
38  2019-01-02  156353260.0     6.007  /api/v1/timeseries/3/
39  2019-01-03  161389001.0     6.002  /api/v1/timeseries/3/
40  2019-01-04  163318124.0     5.983  /api/v1/timeseries/3/
41  2019-01-05  159511070.0     5.984  /api/v1/timeseries/3/

Cheers! Direct any questions to @andyreagan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment