Skip to content

Commit 9c1e058

Browse files
committed
updated for new v4 API
1 parent 3dbd03c commit 9c1e058

13 files changed

+327
-1834
lines changed

MC01 - setup.ipynb

+24-32
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
"Media Cloud: Setting up Your Environment\n",
88
"========================================\n",
99
"\n",
10-
"[Media Cloud](https://mediacloud.org) is an open-source platform for media analysis. It is a collaborative academic project supported by various non-profit foundations since 2011. You can use our various online tools to investigate news coverage about your topic of interest, and all the same information is available via a rich API.\n",
10+
"[Media Cloud](https://mediacloud.org) is an open-source platform for media analysis. It is a collaborative academic project supported by various non-profit foundations since 2011. It is now led by a consortium of [University of Massachusetts Amherst](https://publicinfrastructure.org), [Northeastern University](http://dataculture.northeastern.edu), the [Media Ecosystems Analysis Group](https://www.mediaecosystems.org). You can use our various online tools to investigate news coverage about your topic of interest, and all the same information is available via a rich API.\n",
1111
"\n",
12-
"This set of notebooks is a brief introduction to the API. It covers many of the most common operations we see researchers performing. The API is fully featured, so much so that [all our web-based tools](https://tools.mediacloud.org/#/home) are built on top of it.\n",
12+
"This set of notebooks is a brief introduction to the API. It covers many of the most common operations we see researchers performing. The API is fully featured, so much so that [all our web-based tools](https://search.mediacloud.org/) are built on top of it.\n",
1313
"\n",
1414
"Relevant references:\n",
15-
"* Our [a Python client for the api on PyPi](https://pypi.org/project/mediacloud/)\n",
16-
"* The [general Media Cloud API specification](https://github.com/berkmancenter/mediacloud/blob/master/doc/api_2_0_spec/api_2_0_spec.md)\n",
17-
"* The [topic-mapper specific Media Cloud API Specification](https://github.com/berkmancenter/mediacloud/blob/master/doc/api_2_0_spec/topics_api_2_0_spec.md)"
15+
"* Our [a Python client for the api on PyPi](https://pypi.org/project/mediacloud/)"
1816
]
1917
},
2018
{
@@ -23,19 +21,14 @@
2321
"source": [
2422
"## Setup Your API Key for this Tutorial\n",
2523
"\n",
26-
"You need to instantiate a client with your **private** API key. This key is linked to your account, and has a quota attached to it so you don't blow up our servers. If you run into the quota then you will see errors returned in your API calls. Email us if you need to increase your quota.\n",
24+
"You need to instantiate a client with your **private** API key. This key is linked to your account, and has a quota attached to it so you don't blow up our servers. If you use up your quota then you will see errors returned in your API calls. It is reset automatically every week. Email us if you need to increase your quota.\n",
2725
"\n",
2826
"To obtain your api key, you can:\n",
29-
"1. [login to any of our tools](https://tools.mediacloud.org/)\n",
27+
"1. [login to any of our tools](https://search.mediacloud.org/sign-in)\n",
3028
"2. click the little person icon in the top right, then select \"profile\"\n",
3129
"3. copy your API key from where it is shown in the list of information about your account\n",
3230
"\n",
33-
"For this tutorial, we decided to use the commonly used [`python-dotenv`](https://pypi.org/project/python-dotenv/) library to load this magic string in each notebook file without exposing it.\n",
34-
"\n",
35-
"1. In this Jupyter Lab hosted on Binder, select File -> Open from Path from the menu bar\n",
36-
"2. Type in \".env\" and click Open\n",
37-
"3. Replace \"MY_MC_API_KEY\" with your API key (from your profile page)\n",
38-
"4. Select File -> Save"
31+
"For this tutorial, we decided to include a `MC_API_KEY` python constant at the top of each notebook. Replace the placeholder value with your api key and then you're off."
3932
]
4033
},
4134
{
@@ -55,8 +48,8 @@
5548
"source": [
5649
"# If you are running this locally (not on Binder), then you should install the requirements. If you are using this on\n",
5750
"# Binder then all of these will be installed for you automatically.\n",
58-
"#import sys\n",
59-
"#!{sys.executable} -m pip install -r requirements.txt"
51+
"import sys\n",
52+
"!{sys.executable} -m pip install -r requirements.txt"
6053
]
6154
},
6255
{
@@ -65,8 +58,12 @@
6558
"metadata": {},
6659
"outputs": [],
6760
"source": [
68-
"from dotenv import load_dotenv\n",
69-
"load_dotenv() # load config from .env file"
61+
"import os, mediacloud.api\n",
62+
"from importlib.metadata import version\n",
63+
"# Set your personal API KEY\n",
64+
"MC_API_KEY = 'MY_API_KEY'\n",
65+
"search_api = mediacloud.api.SearchApi(MC_API_KEY)\n",
66+
"f'Using Media Cloud python client v{version(\"mediacloud\")}'"
7067
]
7168
},
7269
{
@@ -75,12 +72,10 @@
7572
"metadata": {},
7673
"outputs": [],
7774
"source": [
78-
"import os, mediacloud.api\n",
79-
"# Read your personal API key from that .env file \n",
80-
"my_mc_api_key = os.getenv('MC_API_KEY')\n",
81-
"# A convention we use is to name your api client `mc`\n",
82-
"mc = mediacloud.api.MediaCloud(my_mc_api_key)\n",
83-
"mediacloud.__version__"
75+
"import datetime as dt\n",
76+
"# make sure your connection and API key work by asking for the total count of in 2023\n",
77+
"results = search_api.story_count('*', dt.date(2023,1,1), dt.date(2023,12,31))\n",
78+
"results"
8479
]
8580
},
8681
{
@@ -89,25 +84,22 @@
8984
"metadata": {},
9085
"outputs": [],
9186
"source": [
92-
"# make sure your connection and API key work by asking for the high-level system statistics\n",
93-
"mc.stats()"
87+
"# or print it out as a nice json tree - we'll use this later (only works in Jupyter Lab)\n",
88+
"from IPython.display import JSON\n",
89+
"JSON(results)"
9490
]
9591
},
9692
{
9793
"cell_type": "code",
9894
"execution_count": null,
9995
"metadata": {},
10096
"outputs": [],
101-
"source": [
102-
"# or print it out as a nice json tree - we'll use this later (only works in Jupyter Lab)\n",
103-
"from IPython.display import JSON\n",
104-
"JSON(mc.stats())"
105-
]
97+
"source": []
10698
}
10799
],
108100
"metadata": {
109101
"kernelspec": {
110-
"display_name": "Python 3",
102+
"display_name": "Python 3 (ipykernel)",
111103
"language": "python",
112104
"name": "python3"
113105
},
@@ -121,7 +113,7 @@
121113
"name": "python",
122114
"nbconvert_exporter": "python",
123115
"pygments_lexer": "ipython3",
124-
"version": "3.7.3"
116+
"version": "3.10.1"
125117
}
126118
},
127119
"nbformat": 4,

0 commit comments

Comments
 (0)