Skip to content

Instantly share code, notes, and snippets.

@eloydegen
Last active October 21, 2022 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eloydegen/fca827e2f662f92c1b14d26118bd59fe to your computer and use it in GitHub Desktop.
Save eloydegen/fca827e2f662f92c1b14d26118bd59fe to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import requests
import sys
import json
from datetime import date, timedelta
def time_calc(time_str):
if time_str.endswith("AM"):
time_str = time_str.strip("AM")
time_str_list = time_str.split(":")
time_int = list(map(int, time_str_list))
minutes = (time_int[0] * 60) + time_int[1]
return minutes
elif time_str.endswith("PM"):
time_str = time_str.strip("PM")
time_str_list = time_str.split(":")
time_int = list(map(int, time_str_list))
minutes = ((time_int[0] + 12) * 60) + time_int[1]
return minutes
start_date = date(2022, 1, 1)
end_date = date(2022, 12, 31)
delta = timedelta(days=1)
print("int a[365][3] = {")
counter = 0
while start_date <= end_date:
cur_date = start_date.strftime("%Y-%m-%d")
url = "https://api.sunrise-sunset.org/json?lat=36.7201600&lng=4.4968241&date=" + cur_date
r = requests.get(url)
decoded_json = json.loads(r.content.decode("utf-8"))
sunrise = decoded_json["results"]["sunrise"]
sunset = decoded_json["results"]["sunset"]
#print(str(counter) + " " + str(time_calc(sunrise)) + " " + str(time_calc(sunset)))
print(" {" + str(counter) + ", " + str(time_calc(sunrise)) + ", " + str(time_calc(sunset)) + "},")
start_date += delta
counter += 1
print("};")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment