Skip to content

Instantly share code, notes, and snippets.

@nick3499
Created July 13, 2023 23:25
Show Gist options
  • Save nick3499/34626297f52779334d24950ef8b9fce7 to your computer and use it in GitHub Desktop.
Save nick3499/34626297f52779334d24950ef8b9fce7 to your computer and use it in GitHub Desktop.
IPGeolocation’s Astronomy API: json.loads(), requests.get(), pendulum.parse(), pendulum.parsing.ParserError()
#!/bin/python3
'''Get data from IPGeolocation’s Astronomy API.'''
from json import loads
from requests import get
from pendulum import parse
from pendulum.parsing import ParserError
API_KEY: str = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
LATITUDE: str = '00.000000'
LONGITUDE: str = '-00.000000'
URL: str = f'https://api.ipgeolocation.io/astronomy?apiKey={API_KEY}\
&lat={LATITUDE}&long={LONGITUDE}'
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, \
like Gecko) Chrome/96.0.4664.9 Safari/537.36'}
response = get(URL, headers=headers).text
data = loads(response)
dark_orange: str = f'\x1b[1;7;38;5;208m'
orange_1: str = f'\x1b[1;7;38;5;214m'
gold_1: str = f'\x1b[1;7;38;5;220m'
yellow_1: str = f'\x1b[1;7;38;5;226m'
reset: str = f'\x1b[0m'
# date/time
print(f"{dark_orange} Date {reset} \
{parse(data['date'], exact=True).format('dddd, MMMM DD, YYYY')}")
print(f"{dark_orange} Time {reset} \
{parse(data['current_time'], exact=True).format('h:mm:ss A')}")
# sunrise/sunset
print(f"{orange_1} Sunrise {reset} \
{parse(data['sunrise'], exact=True).format('h:mm A')}")
print(f"{orange_1} Sunset {reset} \
{parse(data['sunset'], exact=True).format('h:mm A')}")
# length of day
print(f"{gold_1} Day length {reset} {data['day_length']}")
# moonrise/moonset
try:
print(f"{yellow_1} Moonrise {reset} \
{parse(data['moonrise'], exact=True).format('h:mm A')}")
except ParserError:
print(' Moonrise n/a')
try:
print(f"{yellow_1} Moonset {reset} \
{parse(data['moonset'], exact=True).format('h:mm A')}")
except ParserError:
print(' Moonset n/a')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment