Created
October 25, 2018 08:54
-
-
Save NMZivkovic/a766a4513ea6bce972eb5ac5f9db441c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Usage: get_data.py --year=<year> | |
""" | |
import requests | |
import os | |
from docopt import docopt | |
# docopt helps parsing the command line argument in | |
# a simple manner (http://docopt.org/) | |
args = docopt(doc=__doc__, argv=None, | |
help=True, version=None, | |
options_first=False) | |
year = args['--year'] | |
# Create directory if not present | |
year_directory_name = 'data/{year}'.format(year=year) | |
if not os.path.exists(year_directory_name): | |
os.makedirs(year_directory_name) | |
# Fetching file list for the corresponding year | |
year_data_files = requests.get( | |
'http://data.pystock.com/{year}/index.txt'.format(year=year) | |
).text.strip().split('\n') | |
for data_file_name in year_data_files: | |
file_location = '{year_directory_name}/{data_file_name}'.format( | |
year_directory_name=year_directory_name, | |
data_file_name=data_file_name) | |
with open(file_location, 'wb+') as data_file: | |
print('>>> Downloading \t {file_location}'.format(file_location=file_location)) | |
data_file_content = requests.get( | |
'http://data.pystock.com/{year}/{data_file_name}'.format(year=year, data_file_name=data_file_name) | |
).content | |
print('<<< Download Completed \t {file_location}'.format(file_location=file_location)) | |
data_file.write(data_file_content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment