-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetcher.py
More file actions
55 lines (45 loc) · 1.82 KB
/
fetcher.py
File metadata and controls
55 lines (45 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
import requests
import json
import time
import datetime
from iex import Stock
# For some reason the Stock(ticker).quote() function outputs to the screen
def update_info(ticker, f):
"""
Gets quote data for specific ticker and writes to output file in a csv format
"""
stock = Stock(ticker).quote()
args = [time.strftime('%H:%M', time.localtime(stock['latestUpdate']//1000)), stock['symbol'], stock['latestPrice'],
stock['latestVolume'], stock['close'], stock['open'], stock['low'], stock['high']]
f.write(",".join(map(str, args)))
f.write('\n')
def call_update(filename='info.csv'):
"""
Opens output file and calls update_info() for each ticker than it gets from the tickers.txt
Outputs the data to the use specified output file in csv format
"""
tickers_obj = open(sys.argv[2])
content = tickers_obj.readlines()
content = [x.strip() for x in content]
output_file = open(filename, "a+")
# Writes first line of column names
args = ["Time", "Ticker", "latestPrice",
"latestVolume", "Close", "Open", "low", "high"]
if open(filename, 'r').readline().strip() != ",".join(map(str, args)):
output_file.write(",".join(map(str, args)))
output_file.write('\n')
for x in content:
update_info(x, output_file)
if __name__ == "__main__":
try:
limit = int(sys.argv[1])
ticker_file = sys.argv[2]
info_file = sys.argv[3]
# While loop makes the program run for user specified amount of time
start_time = time.time()
while(time.time() - start_time) < limit:
call_update(info_file)
time.sleep(60)
except IndexError as e:
print('Format:\n $ python fetcher.py <time limit> <input file> <output file>')