Skip to content

Instantly share code, notes, and snippets.

@codersquid
Last active October 18, 2015 23:52
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 codersquid/b10e579527c5fd335882 to your computer and use it in GitHub Desktop.
Save codersquid/b10e579527c5fd335882 to your computer and use it in GitHub Desktop.
Intro API Lesson
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This lesson teaches you how to make requests to an API. We will use the [Requests](http://docs.python-requests.org/en/latest/) library to send requests to pyvideo.org.\n",
"\n",
"## Prerequisites\n",
"\n",
"* Be familiar with variables, lists, dictionaries, loops, and conditionals. You can learn these concepts\n",
" by working through [Learn Python The Hard Way](http://learnpythonthehardway.org/book/).\n",
"* Install the [Requests](http://docs.python-requests.org/en/latest/) library."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import requests"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will use the pyvideo.org api for this lesson because pyvideo.org does not require authorization for GET requests.\n",
"\n",
"The [PyCon APAC 2014](http://pyvideo.org/category/59/pycon-apac-2014) conference is listed on pyvideo.org for people to look at.\n",
"To get information about the conference in a machine readable format, we will call the pyvideo.org `category` API.\n",
"\n",
"The URL to get category information about the conference is: http://pyvideo.org/api/v2/category/pycon-apac-2014\n",
"\n",
"## Learning Objective\n",
"\n",
"* Print a list of all the videos from the PyCon APAC 2014 conference"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"response = requests.get(\"http://pyvideo.org/api/v2/category/pycon-apac-2014\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before using the response, we should test whether the request was successful. Responses have status\n",
"codes, and 200 is a successful response.\n",
"[List of successful status codes](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success)\n",
" \n",
"The Requests library provides the status code in a variable called `status_code` and a boolean, `ok` that is\n",
"True if the response is success.\n",
"\n",
"We've saved the response to our request in a variable called `response`. You can use `response.ok` to test if the response was successful."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The request is a success. Now we can look for the videos\n"
]
}
],
"source": [
"# Check for a successful response.\n",
"if response.ok:\n",
" print(\"The requested is a success. Now we can look for the videos\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The pyvideo API replies to our request with JSON. The Requests library provides a method to convert json data to a python dictionary. We will save the dictionary to a variable called `conference`"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [],
"source": [
"conference = response.json()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict_keys(['slug', 'title', 'description', 'whiteboard', 'videos', 'start_date', 'url', 'added'])\n"
]
}
],
"source": [
"# Look at the keys of the dictionary. The 'videos' item has a list of all the videos for the conference.\n",
"print(conference.keys())"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"40"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# How many videos are in the conference?\n",
"len(conference['videos'])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'http://pyvideo.org/api/v2/video/3262'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# What is the url for the first video?\n",
"conference['videos'][0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As an example, we will make a request by hand to get information about the first video"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"video_response = requests.get('http://pyvideo.org/api/v2/video/3262')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"video = video_response.json()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict_keys(['video_flv_length', 'video_webm_length', 'source_url', 'recorded', 'id', 'video_webm_download_only', 'summary', 'video_mp4_download_only', 'title', 'video_ogv_length', 'whiteboard', 'video_mp4_length', 'copyright_text', 'updated', 'video_webm_url', 'state', 'tags', 'thumbnail_url', 'slug', 'video_flv_url', 'video_mp4_url', 'video_ogv_download_only', 'added', 'video_ogv_url', 'duration', 'embed', 'description', 'language', 'category', 'quality_notes', 'speakers', 'video_flv_download_only'])\n"
]
}
],
"source": [
"# video is a dictionary based on the response from pyvideo.org.\n",
"# What information has pyvideo.org returned?\n",
"print(video.keys())"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'from present import future.curriculum'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 'title' is one of the keys in the response for a video.\n",
"video['title']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Earlier we made a request to http://pyvideo.org/api/v2/category/pycon-apac-2014 to get information about the PyCon APAC 2014 conference. The information from the response is saved in a variable called `conference`. This dictionary has a key called `videos` which has a list of all the videos.\n",
"\n",
"Using what you know about lists and loops, how would you print a list of all the video titles for PyCon APAC 2015?\n",
"\n",
"You will need to go through all the videos in the list, and send a request to the video api to get a response with the title of the video."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment