Giter VIP home page Giter VIP logo

fp1's Introduction

Final Project Assignment 1: Exploration (FP1)

My Library: net/url and json

My Name: Jason Downing

What I did

I decided to mess around with two racket libraries, net/url and json. I used net/url to make HTTP GET requests to various APIs, and json to parse the JSON response text from these APIs. The three APIs I used were:

ipify - IP Address lookup API

freegeoip.net - geolocation of an IP Address API

OpenWeatherMap - current weather conditions for thousands of cities

The idea behind this was to first lookup the IP address of my PC using ipify, then feed that IP address into freegeoip.net to get the lat/long of my current location. I then did a API call to OpenWeatherMap to get the current weather conditions for my location. All of this is stored using racket variables, and printed out the screen.

An example run of my program:

alt text

Code highlights

Getting the IP Address.

;; Let's first get the current IP address for this PC.
;; This is used to get the user's location for the weather API call.
(define ipify (string->url "https://api.ipify.org?format=json"))
(define get_ip (get-pure-port ipify))
(define ip_response (port->string get_ip))
(close-input-port get_ip)

; Now ip_response has a string that looks like "{\"ip\":\"129.63.253.73\"}"
; Let's convert his string into a JSON object
(define ip_obj (string->jsexpr ip_response))

Getting the geolocation.

;; Create the API string
(define get_loc (string-append "https://freegeoip.net/json/" ip_addr))

;; Make the HTTP GET request, store the response in "geo_response"
(define geo_ip (string->url get_loc))
(define get_geo (get-pure-port geo_ip))
(define geo_response (port->string get_geo))
(close-input-port get_geo)

;; Convert geo_response to a JSON object, just like before.
(define geo_obj (string->jsexpr geo_response))

Finally, getting the current weather data.

;; Now we can use this URL string to get weather data!
(define weather_data (string->url get_weather))
(define get_data (get-pure-port weather_data))
(define weather_response (port->string get_data))
(close-input-port get_data)

;; Convert geo_response to a JSON object, just like before.
(define weather_obj (string->jsexpr weather_response))

Output it all to the screen using printf

(printf "CURRENT CITY: ~a\n" city_name)
(printf "Lat/Long: ~a / ~a\n" coord_lat coord_long)
(printf "Weather conditions: ~a\n" conditions)
(printf "Wind speed: ~a MPH\n" wind_speed)

fp1's People

Contributors

jasond94 avatar marksherman avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.