In this post for Naas-Sallins Coder Dojo we are going to pick up where we left off last time but instead of getting the weather we are going to get some details about the International Space Station. Again we are going to use a free publically available web api as the source of our information, in this case we are going to use Open-Notify for our data. There are two main pieces of data available about the ISS which are its current position and the time and date of the next few overhead passes for a given location. So quickly adapting our code from our weather app last time out we get the following
""" read opennotify api""" import urllib2 import json import datetime def getiss(): """ call opennotify api""" response = urllib2.urlopen('http://api.open-notify.org/iss-pass.json?lat=53.22&lon=-6.67') mydata = response.read() return(mydata) def main(): iss = getiss() pos = json.loads(iss) passtimes =  for i in pos['response']: when = i['risetime'] passtimes.append(datetime.datetime.fromtimestamp(int(when)).strftime('%Y-%m-%d %H:%M:%S')) print passtimes if __name__ == '__main__': main()
We have put the latitude and longtitude for Naas in to the api call and used the techniques from the last time to extract the relevant bits from the data returned which looks something like
Again we parse this JSON data like the last time to extract each occurance of risetime which we add to a list called passtimes. The api documentation tells us that risetime is the time that the ISS will start passing by the location given in unix time format. A quick web search tells us how python can convert that into a more normal looking date and time by using the datetime library. If we print out our passtimes list we get the following output
[‘2014-05-10 12:05:11’, ‘2014-05-10 13:41:56’, ‘2014-05-10 15:19:22’, ‘2014-05-11 06:31:32’, ‘2014-05-11 08:04:31’]
If we want to tweet this information from our twitter account for the dojo @NaasCoderDojo then its a simple matter of putting this together with our weather twitter-bot from the last time
""" Python ISS for @NaasCoderDojo using tweepy and open-notify.org """ import tweepy import urllib2 import json import datetime #codes below are fake: enter details from your Twitter application CONSUMER_KEY = 'asdadjakljdakljdakljdlklm' CONSUMER_SECRET = 'dkjahdkhakdhakjdhkajhdkjahdkjahdkahdkhakdhajhdajbi' ACCESS_KEY = 'ajsdhakhdkajhdkjahdkjahdkjhakjdhakjdhkajhdkajhdakh' ACCESS_SECRET = 'djaghdhakhfkahfkjahfkjahfkhakfjhakjfhakfhkfah' def getiss(): """ call opennotify api""" response = urllib2.urlopen('http://api.open-notify.org/iss-pass.json?lat=53.22&lon=-6.67') mydata = response.read() return(mydata) auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) api = tweepy.API(auth) iss = getiss() #print iss pos = json.loads(iss) passtimes =  for i in pos['response']: when = i['risetime'] passtimes.append(datetime.datetime.fromtimestamp(int(when)).strftime('%Y-%m-%d %H:%M:%S')) tweet = "#coderdojo #python ISS is over Naas at " + ",".join(passtimes) print tweet api.update_status(tweet)
The second open-notify api call gives us the current location of the ISS and we can print use this by following our same technique of calling the api, printing out all the data returned and then picking the bits out of the JSON data that we want. The code for getting the current location would be
""" Get current ISS position from open-notify.org""" from datetime import datetime import time import urllib2 import json def getiss(): """ call opennotify api""" response = urllib2.urlopen('http://api.open-notify.org/iss-now.json') mydata = response.read() return(mydata) iss = getiss() pos = json.loads(iss) lat = pos['iss_position']['latitude'] lon = pos['iss_position']['longitude'] when = datetime.now() print('ISS Location For for %s Lat: %.2f Long: %.2f' % (when.strftime("%d %b %Y %H:%M:%S"),lat,lon))
This should give us something like
ISS Location For for 10 May 2014 11:58:23 Lat: 51.62 Long: -17.66
If you want to see what the ISS can see take a look at this link which is an actual case of Hello World! If you dont see anything then read the text which will explain why.
There will be second part to this ISS post which requires the installation of some python libraries so it would help if we can try to follow the instructions here and maybe here if you are on a Mac. The plan is to build an application which plots the current position of the ISS and displays it on a map. As usual we can have a look at any problems at the next dojo session.
Edit: depending on your python set-up you may also need to install pyparsing and python_dateutil packages. From a command window try