Created
January 7, 2020 12:09
-
-
Save siancu/0b00946bdf8f5a1f3ae01ff7edc2965c to your computer and use it in GitHub Desktop.
Python examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import sqlite3 | |
import sys | |
import time | |
from collections import namedtuple | |
DB_LOCATION = 'ISS.db' | |
TABLE_CREATE_SQL = """ | |
CREATE TABLE IF NOT EXISTS iss_position | |
( | |
timestamp TEXT, | |
latitude INT, | |
longitude INT | |
) | |
""" | |
INSERT_LOC_SQL = """ | |
INSERT INTO iss_position | |
(timestamp, | |
latitude, | |
longitude) | |
VALUES (?, ?, ?) | |
""" | |
CoordinateData = namedtuple('coordinate_data', ['latitude', 'longitude', 'timestamp']) | |
def insert_to_db(conn, query, data=None): | |
"""Inserts query and an optional data set to a database supplied via the conn argument.""" | |
cursor = conn.cursor() | |
try: | |
print('Saving to database.') | |
if data: | |
cursor.execute(query, data) | |
else: | |
cursor.execute(query) | |
finally: | |
conn.commit() | |
cursor.close() | |
print('Done.') | |
def get_json(response, err_msg): | |
try: | |
return response.json() | |
except ValueError: | |
print(f'No JSON data available for {err_msg}') | |
def get_iss_loc_data(): | |
"""Track the ISS in real time and log the coordinates to the database.""" | |
iss_json = get_json(requests.get("http://api.open-notify.org/iss-now.json"), 'ISS data') | |
if iss_json: | |
iss_data = CoordinateData(iss_json['iss_position']['latitude'], iss_json['iss_position']['longitude'], iss_json["timestamp"]) | |
print(f"The ISS is currently at Latitude: {iss_data.latitude} and Longitude: {iss_data.longitude} Timestamp: {time.ctime(iss_data.timestamp)}") | |
return iss_data | |
def get_astronaut_data(): | |
"""Check who and how many are on board and print to screen""" | |
astronauts_json = get_json(requests.get("http://api.open-notify.org/astros.json"), 'astronauts') | |
if astronauts_json: | |
print(f"There are currently {astronauts_json['number']} people on board the ISS. They are...") | |
for astronaut in astronauts_json["people"]: | |
print(f"Astronaut {astronaut['name']}, who is on board the {astronaut['craft']}") | |
def api_service_check(): | |
"""Checks if the api service is currently up by returning a boolean.""" | |
if requests.get("http://api.open-notify.org"): | |
return True | |
return False | |
def main(): | |
# no need for new-lines here, as print will automatically end the line with a new-line | |
print("Starting tracking, press 'Ctrl + C' to stop...") | |
with sqlite3.connect(DB_LOCATION) as sql_conn: | |
insert_to_db(sql_conn, TABLE_CREATE_SQL) | |
if not api_service_check(): | |
sys.exit() | |
while True: | |
try: | |
iss_loc = get_iss_loc_data() | |
if iss_loc: | |
insert_to_db(sql_conn, INSERT_LOC_SQL, data=[iss_loc.timestamp, iss_loc.latitude, iss_loc.longitude]) | |
get_astronaut_data() | |
except KeyboardInterrupt: | |
print('Program terminating. Thank you!') | |
sys.exit() | |
time.sleep(5) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment