8
\$\begingroup\$

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.

Reinderien
70.9k5 gold badges76 silver badges256 bronze badges
asked Jun 25, 2013 at 19:38
\$\endgroup\$

1 Answer 1

8
\$\begingroup\$

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.

answered Aug 21, 2014 at 21:41
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.