So going on with the code, I tried to slice the outcome in to sections of year, month day to process it in a "nicely printed format"
like 29 september 2019 - 16:34 hours. My attempts gave (again
) some nice error messages, who'm I took to the search engines and read up on things. On those trial and search I stumbled upon another piece of code for MP what I applied to the risetimes (timestamps) it was written for:
Code: Select all
response = urlreq.urlopen("http://api.open-notify.org/iss-pass.json?lat=53.0145&lon=6.5862")
content = json.loads(response.read())
current_year, current_month, current_day, current_hour, current_minute, current_seconds, current_weekday, current_yearday = localtime()
if content['message'] == 'success':
for overhead in content['response']:
ts = overhead['risetime']
duration = overhead['duration']
year, month, day, hour, minute, seconds, weekday, yearday = localtime(ts)
print("Datum: {}/{} - Tijdstip: {}:{} ".format(day, month, hour, minute,))
What gives the output printed: Datum: 28/9 - Tijdstip: 18:32
Nice! but when I tried to use the above code on the first part of the original message under the header "request" I got an error message: TypeError: string indices must be integers, not str
After trying and reading and the answers from here I finally got it working with:
Code: Select all
response = urlreq.urlopen("http://api.open-notify.org/iss-pass.json?lat=53.0145&lon=6.5862")
content = json.loads(response.read())
current_year, current_month, current_day, current_hour, current_minute, current_seconds, current_weekday, current_yearday = localtime()
dt = content['request']['datetime']
year, month, day, hour, minute, seconds, weekday, yearday = localtime(dt)
print('ISS zal weer zichtbaar zijn om:')
print("Datum: {}/{} - Tijdstip: {}:{} ".format(day, month, hour, minute,))
print("=====================================")
So what I don't understand completely is what makes it, that I need different code? I assume it's the source JSON (example below) what makes it that I need to use different code. What I did notice is that the "request" section is wrapped in { } and the response in [{ }]
Is the difference because the "datetime" is one result and "risetime" is a list of 5?
Code: Select all
{
"message": "success",
"request": {
"altitude": 100,
"datetime": 1569774892,
"latitude": 53.0145,
"longitude": 6.5862,
"passes": 5
},
"response": [
{
"duration": 639,
"risetime": 1569776155
},
{
"duration": 648,
"risetime": 1569781945
},
{
"duration": 635,
"risetime": 1569787744
},
{
"duration": 537,
"risetime": 1569793558
},
{
"duration": 512,
"risetime": 1569853953
}
]
}
Below is my complete main.py, made from the first answers I got from here till now, so there is some progress
Code: Select all
def computer_position():
datapos = urlreq.urlopen("http://ip-api.com/json/")
data_pos = json.loads(datapos.read())
print("=====================================")
print("Mijn lokatie is : ", data_pos["city"])
print("=====================================")
print("Breedte graad : ", data_pos["lat"])
print("Lengte graad : ", data_pos["lon"])
print("=====================================")
print('-------------------------------------')
return data_pos["lat"], data_pos["lon"]
computer_position()
def iss_position():
dataiss = urlreq.urlopen("http://api.open-notify.org/iss-now.json")
data_iss = json.loads(dataiss.read())
print("=====================================")
print("Huidige Positie ISS is:")
print('-------------------------------------')
print("Breedtegraad (Latitude) ISS:", data_iss["iss_position"]["latitude"])
print("Lengtegraad (Longitude) ISS:", data_iss["iss_position"]["longitude"])
print('-------------------------------------')
print("=====================================")
return data_iss["iss_position"]["latitude"], data_iss["iss_position"]["longitude"]
iss_position()
response = urlreq.urlopen("http://api.open-notify.org/iss-pass.json?lat=53.0145&lon=6.5862")
content = json.loads(response.read())
current_year, current_month, current_day, current_hour, current_minute, current_seconds, current_weekday, current_yearday = localtime()
dt = content['request']['datetime']
year, month, day, hour, minute, seconds, weekday, yearday = localtime(dt)
print('ISS zal weer zichtbaar zijn om:')
print("Datum: {}/{} - Tijdstip: {}:{} ".format(day, month, hour, minute,))
print("=====================================")
response = urlreq.urlopen("http://api.open-notify.org/iss-pass.json?lat=53.0145&lon=6.5862")
content = json.loads(response.read())
current_year, current_month, current_day, current_hour, current_minute, current_seconds, current_weekday, current_yearday = localtime()
if content['message'] == 'success':
for overhead in content['response']:
ts = overhead['risetime']
duration = overhead['duration']
year, month, day, hour, minute, seconds, weekday, yearday = localtime(ts)
print("Datum: {}/{} - Tijdstip: {}:{} ".format(day, month, hour, minute,))
print("=====================================")
print('\n')
Still have some ideas to include and work on, It's nice that you get the longitude and latitude from the ISS, who you could plot on a map, but just like my own hometown gets displayed , I want to see if that is possible with the current position of the ISS. Also next to show all the relevant info on a small(oled) display, I have some neopixels who needs to be driven and do something fancy when the ISS is close to my location