CodeGym /Courses /Python SELF EN /Working with Public APIs

Working with Public APIs

Python SELF EN
Level 24 , Lesson 3
Available

9.1 Working with Google Maps API

Let's work a bit with public APIs of popular services.

For instance, Google Maps API offers various services like geocoding, getting routes, and location. To use Google Maps API, you need to register an API key.

Geocoding (getting coordinates by address)


import requests
API_KEY = 'YOUR_GOOGLE_MAPS_API_KEY'
address = '1600 Amphitheatre Parkway, Mountain View, CA'
url = f'https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={API_KEY}'
response = requests.get(url)
data = response.json()
if data['status'] == 'OK':
    location = data['results'][0]['geometry']['location']
    lat = location['lat']
    lng = location['lng']
    print(f'Coordinates: {lat}, {lng}')
else:
    print('Geocoding error')

9.2 Working with OpenWeatherMap API

Another great example is getting the weather for any place on Earth.

The OpenWeatherMap API provides weather data for locations worldwide. You need to register and obtain an API key to use it.

Getting current weather


import requests
API_KEY = 'YOUR_OPENWEATHERMAP_API_KEY'
city = 'London'
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric'
response = requests.get(url)
data = response.json()
if response.status_code == 200:
    weather = data['weather'][0]['description']
    temp = data['main']['temp']
    print(f'Weather in {city}: {weather}, temperature: {temp}°C')
else:
    print('Error fetching weather data')

9.3 Working with GitHub API

You're programmers, so let's do something more techy. For example, we can explore GitHub repositories.

The GitHub API provides access to info about repositories, users, and organizations.

Getting repository information


import requests
repo_owner = 'octocat'
repo_name = 'Hello-World'
url = f'https://api.github.com/repos/{repo_owner}/{repo_name}'
response = requests.get(url)
data = response.json()
if response.status_code == 200:
    print(f"Repository: {data['name']}")
    print(f"Description: {data['description']}")
    print(f"Stars: {data['stargazers_count']}")
else:
    print('Error fetching repository information')

9.4 Working with YouTube Data API

The YouTube Data API lets you get information about videos, channels, and playlists. You need to get an API key to use it.


import requests
API_KEY = 'YOUR_YOUTUBE_API_KEY'
video_id = 'Ks-_Mh1QhMc'
url = f'https://www.googleapis.com/youtube/v3/videos?id={video_id}&key={API_KEY}&part=snippet,contentDetails,statistics'
response = requests.get(url)
data = response.json()
if 'items' in data and len(data['items']) > 0:
    video_info = data['items'][0]
    title = video_info['snippet']['title']
    views = video_info['statistics']['viewCount']
    print(f'Video title: {title}')
    print(f'Views: {views}')
else:
    print('Error fetching video information')

The Open Notify API provides data on the current location of the International Space Station (ISS).

Getting the current ISS location


import requests
url = 'http://api.open-notify.org/iss-now.json'
response = requests.get(url)
data = response.json()
if response.status_code == 200:
    position = data['iss_position']
    print(f"The ISS is at coordinates: longitude {position['longitude']}, latitude {position['latitude']}")
else:
    print('Error fetching data')
2
Task
Python SELF EN, level 24, lesson 3
Locked
Google Maps API
Google Maps API
2
Task
Python SELF EN, level 24, lesson 3
Locked
OpenWeatherMap API
OpenWeatherMap API
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION