· 4 years ago · Jul 17, 2021, 05:40 AM
1import requests
2import sqlite3
3from datetime import datetime
4import time
5
6connection = sqlite3.connect('ISSData.db')
7cursor = connection.cursor()
8
9# creating the table
10cursor.execute(
11 'Create table if not exists TrackData(time text, latitude float, longitude float, place text)')
12
13# insert, read, delete
14
15
16def insert(time, latitude, longitude, place):
17 with connection:
18 cursor.execute('insert into TrackData values(?,?,?,?)',
19 (time, latitude, longitude, place))
20
21
22def current_location():
23 # http://api.open-notify.org/iss-now.json
24 url = 'http://api.open-notify.org/iss-now.json'
25 # API calling
26 response = requests.get(url)
27 data = response.json()
28
29 longitude = data['iss_position']['longitude']
30 latitude = data['iss_position']['latitude']
31 timestamp = data['timestamp']
32 timestamp = datetime.fromtimestamp(timestamp)
33
34 longitude = float(longitude)
35 latitude = float(latitude)
36
37 url = 'https://us1.locationiq.com/v1/reverse.php?key=pk.7510fb98384ba4f451338c713b61ef9f'
38 parameters = {'lat': latitude, 'lon': longitude, 'format': 'json'}
39
40 # API calling
41 response = requests.get(url, params=parameters)
42 data = response.json()
43 place = 'Ocean'
44 if response.status_code == 200:
45 place = data['address']['country']
46
47 insert(timestamp, latitude, longitude, place)
48 # print(read())
49
50
51print('Data Collector running')
52print('Collecting every 54 seconds')
53print('Data worth of 1.5 revolutions will be collected')
54# 1.5 = 150 points
55# 1 = 100 points
56for i in range(150):
57 current_location()
58 print(f'{i} data collected!')
59 time.sleep(54)
60