# Timed requests and parsing JSON in Python

I wrote this tiny script to pull the JSON feed from the CitiBike website:

import requests
import time

def executeCiti():
r = requests.get("http://citibikenyc.com/stations/json")
print r.json()

time.sleep(62)

while True:
executeCiti()

exit()


Then I just run the script in terminal and output it to a .txt file like so: python citi_bike.py > output.txt

The end goal of what I would like by the end of this exercise is well formatted JSON data (with only select few of the pairs from each request), separated by each request. I want it to be manageable so I can create visualizations from it.

1. Is this an okay way to start what I'm trying to accomplish?

2. Is there a better way to begin, so that the data came out like I want it as my end goal? In regards to this question, I feel like I have already started taking a very roundabout way to getting to a cleaned up data set, even though this is only the first step.

While this is a good start, there are several things that should be noted.

r = requests.get("http://citibikenyc.com/stations/json")

This is code snippet is network-based, and so errors may occur. You will want to handle these errors in some way, with a try-except block like so:

try:
r = requests.get("http://citibikenyc.com/stations/json")
except ConnectionError:
pass # handle the error
except TimeoutError:
pass # handle the error


and so on, as per the requests documentation.

Additionally, do not sleep and do in the same function. These are two responsibilities and so should be separated as per the Single Responsibility Principle.

I would suggest adding the sleep(62) to the while True: block.

Also, there is no point to this:

exit()

as it will never be executed. This leads me to my next point, you should probably do the file writing in Python instead of the command line, so you can open the file, append some data, and then close it to make sure it is safe between network errors and power outages.