External Exam Download Resources Web Applications Games Recycle Bin

Querying existing API (JSON)

The first example uses the qldtraffic-website-api-specification-V1-1

qldtraffic.py

import requests
import json
from datetime import datetime

apikey = "3e83add325cbb69ac4d8e5bf433d770b"
#^^ public apikey, limited to 100 requests/minute.

baseURL = "https://api.qldtraffic.qld.gov.au/v1/"
url = baseURL + "events?apikey=" + apikey
answer = requests.get(url)
#^^ this could take some time..

#convert to Python dictionary:
result = json.loads(answer.text)
for feature in result["features"]:
    description = feature["properties"]["description"]
    information = feature["properties"]["information"]
    eventType = feature["properties"]["event_type"]
    impact = feature["properties"]["impact"]["impact_type"]
    start_raw = feature["properties"]["duration"]["start"]
    end_raw = feature["properties"]["duration"]["end"]

    #--------------------------CLEANING----------
    #clean up date strings from raw time strings (e.g.: 2018-10-29T02:30:00+10:00)
    if start_raw:
        start_raw = start_raw[:-9].replace("T","-")
        #^^ strip the last 9 characters, including seconds and +10:00 time zone
        start = datetime.strptime(start_raw, "%Y-%m-%d-%H:%M").strftime("%A %d %B %Y")
        #^^ %A = day name, %B = month name
        
    if end_raw:
        end_raw = end_raw[:-9].replace("T","-")
        end = datetime.strptime(end_raw, "%Y-%m-%d-%H:%M").strftime("%A %d %B %Y")

    #check there are no NULL strings:
    if not description:
        description = "No description"
    if not information:
        information = "No information"
    #---------------------------------------------
        
    print(description)
    print(eventType)
    print(information)
    print("Start: " + start)
    print(impact)
    print("End: " + end)
    print("--------------------------------------------")
    print("")

starwars1.py

#SWAPI = The Star Wars API..
#documentation: https://swapi.co/documentation

import requests
import json
#--------------------------------
baseurl = "https://swapi.dev/api/"
resource = "people" # films, people, planets, species, starships, vehicles
function = "?search=" # "", "?search=", "schema"
value = "Luke" # should be empty string i.e. "" if viewing schema
#--------------------------------
url = baseurl + resource + "/" + function + value
#https://swapi.co/api/people/?search=Luke

response = requests.get(url)
print("Response code:", response.status_code)

#Response codes:
#200 = good
#301 = redirected to a different endpoint
#400 = bad request
#401 = not authenticated
#403 = forbidden access
#404 = not found

parsed = json.loads(response.text)
print(json.dumps(parsed, indent=2, sort_keys=True))

starwars2.py

#SWAPI = The Star Wars API..
#documentation: https://swapi.co/documentation

import requests
import json
#--------------------------------
baseurl = "https://swapi.dev/api/"
resource = "people" # films, people, planets, species, starships, vehicles
function = "schema" # "", "?search=", "schema"
value = "" # should be empty string i.e. "" if viewing schema
#--------------------------------
url = baseurl + resource + "/" + function + value
#https://swapi.co/api/people/schema

response = requests.get(url)
print("Response code:", response.status_code)

#Response codes:
#200 = good
#301 = redirected to a different endpoint
#400 = bad request
#401 = not authenticated
#403 = forbidden access
#404 = not found

parsed = json.loads(response.text)
print(json.dumps(parsed, indent=2, sort_keys=True))

wildnet.py

#https://data.qld.gov.au/dataset/qld-wildlife-data-api

import requests
import json

#--------------------------------

searchTerm = "spider"
searchEndPoint = "http://environment.ehp.qld.gov.au/species/?op=speciessearch&kingdom=animals&species="

#--------------------------------

response = requests.get(searchEndPoint+searchTerm)
print("Response code:", response.status_code)

#Response codes:
#200 = good
#301 = redirected to a different endpoint
#400 = bad request
#401 = not authenticated
#403 = forbidden access
#404 = not found

parsed = json.loads(response.text)
print(json.dumps(parsed, indent=2, sort_keys=True))