Skip to content

Commit 9be1072

Browse files
Add remaining python files
1 parent c53bc00 commit 9be1072

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

consuming-apis-python/covid.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from datetime import date, timedelta
2+
import requests
3+
4+
today = date.today()
5+
yesterday = today - timedelta(days=1)
6+
country = "germany"
7+
endpoint = "https://api.covid19api.com/country/%s/status/confirmed" % country
8+
params = {"from": str(yesterday), "to": str(today)}
9+
10+
response = requests.get(endpoint, params=params).json()
11+
total_confirmed = 0
12+
for region in response:
13+
cases = region.get("Cases", 0)
14+
total_confirmed += cases
15+
16+
print("Total Confirmed COVID cases in %s: %s" % (country, total_confirmed))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import requests
2+
3+
# Replace the following with the API key generated above.
4+
api_key = "API_KEY"
5+
endpoint = "https://api.giphy.com/v1/gifs/search"
6+
search_term = "shrug"
7+
params = {"api_key": api_key, "limit": 1, "q": search_term, "rating": "g"}
8+
9+
response = requests.get(endpoint, params=params).json()
10+
for gif in response["data"]:
11+
title = gif["title"]
12+
url = gif["url"]
13+
print("%s | %s" % (title, url))

consuming-apis-python/github.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import requests
2+
3+
# Replace the CLIENT_ID and CLIENT_SECRET below with your own.
4+
client_id = "CLIENT_ID"
5+
client_secret = "CLIENT_SECRET"
6+
redirect_uri = "https://httpbin.org/anything"
7+
oauth_base_url = "https://github.com/login/oauth"
8+
api_base_url = "https://api.github.com"
9+
10+
11+
def get_code():
12+
params = {
13+
"client_id": client_id,
14+
"redirect_uri": redirect_uri,
15+
"scope": "user",
16+
"response_type": "code",
17+
}
18+
19+
endpoint = oauth_base_url + "/authorize"
20+
url = requests.get(endpoint, params=params).url
21+
print("Go to this URL and follow the OAuth flow there: %s" % url)
22+
code = input("code: ")
23+
return code
24+
25+
26+
def get_token(code=None):
27+
endpoint = oauth_base_url + "/access_token"
28+
params = {
29+
"client_id": client_id,
30+
"client_secret": client_secret,
31+
"redirect_uri": redirect_uri,
32+
"code": code,
33+
}
34+
headers = {"Accept": "application/json"}
35+
response = requests.post(endpoint, data=params, headers=headers).json()
36+
return response["access_token"]
37+
38+
39+
def get_user(token=None):
40+
headers = {"Authorization": "token %s" % token}
41+
42+
endpoint = "https://api.github.com/user"
43+
response = requests.get(endpoint, headers=headers).json()
44+
return response
45+
46+
47+
custom_code = get_code()
48+
access_token = get_token(code=custom_code)
49+
user = get_user(token=access_token)
50+
51+
name = user["name"]
52+
username = user["login"]
53+
private_repos_count = user["total_private_repos"]
54+
print("%s (%s) | private repos: %s" % (name, username, private_repos_count))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import requests
2+
3+
endpoint = "https://www.googleapis.com/books/v1/volumes"
4+
params = {"q": "moby dick", "maxResults": 3}
5+
6+
response = requests.get(endpoint, params=params).json()
7+
for book in response["items"]:
8+
volume = book["volumeInfo"]
9+
title = volume["title"]
10+
published = volume["publishedDate"]
11+
description = volume["description"]
12+
13+
print("%s (%s) | %s" % (title, published, description))

0 commit comments

Comments
 (0)