Categories: API

Sunrise and Sunset Data API

Sunrise and Sunset

This Sunrise and Sunset API will return interesting data about the day like: sunset, sunrise, day length, nautical twilight, civil twilight and astronomical twilight. One thing to be careful about is times will be GMT so depending on the location you’re looking up you might want to adjust the times returned.

Here is a simply code that uses requests, json and webbrowser libraries harmoniously.

Code below demonstrates a simple API query based on Central Park coordinates in New York and date of November 27 2019.

import requests
import json
import webbrowser
params = {"lat":40.779659, "lng":-73.968995, "date":"2019-11-27"}

f = r"https://api.sunrise-sunset.org/json?"

def sunrisesunset(f):
    a = requests.get(f, params=params)
    a = json.loads(a.text)
    a = a["results"]
    return (a["sunrise"], a["sunset"], a["day_length"])

print(sunrisesunset(f))

Output example:

Central Park, New York

November 27, 2019

Sunrise

6 am
hour
57
minute
1
second

Sunset

4 pm
hour
30
minute
1
second

Day Length

9
hour
33
minute
1
second

If you need a refresher on user defined functions lesson in Python here is a link to our Defining Functions Lesson.

Source of API: Sunrise and Sunset API

Usag1r

Share
Published by
Usag1r

Recent Posts

Benefits of Bokeh over Python visualization libraries like Seaborn, Matplotlib & Plotly

ContentsIntroductionBokeh vs Seaborn & MatplotlibBokeh vs PlotlySummary Introduction As computer science activity booms, you might…

12 months ago

History of Speech-to-Text AI models

History of Speech-to-Text Models​ & Current Infrastructure As speech technology continues to advance, so too…

1 year ago

Advanced Level Python Tips & Tricks

Python Tips & Tricks (Part III) Advanced Level HolyPython.com Here are more Python tips about…

1 year ago

Intermediate Level Python Tips & Tricks

Python Tips & Tricks (Part II) Intermediate Level HolyPython.com The list continues with slightly more…

1 year ago

When is Python 4 Coming Out?

Almost 15 years after the release of Python 3 many people are wondering when Python…

2 years ago

How to Create an app with a 3D model using VIKTOR

Introduction We are going to need smart engineering solutions to solve our planet's problems (and…

2 years ago