#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 11/22/2022 DS 2000 Lecture 21 - JSON and APIs Logistics: - Do what you need to do for yourself over break - OH for the rest of the semester - 4 - 8pm - we're happy to help you with DS 2001 projects AND expect to explain your project/goal to the TA a bit to get help to start with :) - remote attendance (https://bit.ly/remote-ds2000-muzny) Three ways to participate in multiple choice questions 1) via the PollEverywhere website: https://pollev.com/muzny 2) via text: text "muzny" to the number 22333 to join the session 3) via Poll Everywhere app (available for iOS or Android) """ """ Warm-up 0 ---- What is your favorite kind of pie? A. A savory pie! (12%) B. Apple! (52%) C. I'm team berries! D. Peaches! E. I don't love pie. (20%) """ """ This lecture: what is it most useful for? -> if you're interested in web development -> in getting data dynamically from web sources """ """ The internet: what is it? --- - a means of communication/information exchange - how does it work? - if I ask for www.myfavoritewebsite.com -> look information up in databases around the world (where does this website live?) -> ask for the content of the website from the server -> typically the server will give you back a response that is a webpage (.html) - when you want to submit information to a web application -> you need to give information to that server """ """ Getting information from the internet: APIs --- Application Programming Interface - how we ask a web server for information (or give it information) - this will be similar to having functions defined on a web server Let's talk about Twitter (No, for real, let's talk about Twitter) - Twitter lets users post (140 character tweets) -> there is extremely high usage of this service - Elon Musk bought Twitter & fired a bunch of people -> speculation that the app will shut down - Twitter is a great example of the application really being two things: 1) the software application 2) the API -> lets devs and researchers access tweets -> this is a huge portion of how twitter is used -> lets researchers study how misinformation and disinformation spreads -> lets marketing firms study crowd opinions about different products/issues -> as regular people we can access a "reasonable" amount of tweets via the API -> academic researchers and companies can pay $$$$ to access the "firehose" -> requires user authentication -> you have to sign up, twitter essentially gives you a password A. I am twitterer B. I know what twitter is C. Twitter is for dinosaurs D. I am a twitter influencer To use an API: 1) Look up the url that you'll be talking to ("endpoint") 2) Look up the kind of request that you'll be making -> GET: give me information -> POST: I'm giving you (the web server) information 3) Look up whether or not you need to authenticate yourself first -> https://requests.readthedocs.io/en/latest/user/authentication/ 4) Send your request and hope that your response comes back To send requests in python: requests library documentation: https://pypi.org/project/requests/ Felix also likes this documentation: https://requests.readthedocs.io/en/latest/ The API that we'll be talking to today: Poke API documentation: https://pokeapi.co/docs/v2#pokemon """ import requests r = requests.get('https://pokeapi.co/api/v2/pokemon/squirtle') # this is a Response object # with code 200 (success) # this looks an awful lot like a python dictionary # print(r.text) # poke_dict = r.json() """ JSON --- Javascript Object Notation - essentially a way to encode complex data structures for your data! - note that often you'll have lists within the dictionary (and also dictionaries inside the dictionary) python_object = json.loads(string) """ import json poke_dict = json.loads(r.text) print(poke_dict.keys()) print() # make a pokemon based on the actual stats of a pokemon print(poke_dict["stats"][0]) health = 0 attack = 0 for statistic in poke_dict["stats"]: # a dictionary print(statistic) print() if statistic["stat"]["name"] == "hp": health = statistic["base_stat"] if statistic["stat"]["name"] == "attack": attack = statistic["base_stat"] print(health) print(attack) # make it so that we can give it different health, attack, and name """ Writing a larger program - pokemon_part2.py --- Update our Pokemon battle program so that it: 1) prompts the user for the names of two pokemon to battle 2) Creates Pokemon with the appropriate health, attack, and name from the information given by the Pokemon API 3) Battles the Pokemon and gives a nicely formatted display """ """ For Post-break DS 2000, would you prefer lecture focus on... (there will be some of all of these, but I can shift the *focus*) --- A. Programming concepts (13) B. Real world data science applications (58) C. Visualization concepts (21) D. idk? *shrug* (8) """ # Next Time # --- # - Cool data science applications # - Pandas # - DataFrames