forked from hbap/python2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquote.py
More file actions
31 lines (24 loc) · 634 Bytes
/
quote.py
File metadata and controls
31 lines (24 loc) · 634 Bytes
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
"""
Gets a stock quote.
Demonstrates an HTTP API.
"""
import csv
import os
import requests
import sys
# Check for symbol
if len(sys.argv) != 2:
sys.exit("Usage: python3 quote.py SYMBOL")
symbol = sys.argv[1]
# Get API_KEY
API_KEY = os.getenv("API_KEY")
if not API_KEY:
sys.exit("Missing API_KEY")
# Get data as a CSV file
url = f"https://www.alphavantage.co/query?apikey={API_KEY}&datatype=csv&function=TIME_SERIES_INTRADAY&interval=1min&symbol={symbol}"
response = requests.get(url)
# Parse CSV
reader = csv.DictReader(response.text.splitlines())
# Print most recent price
row = next(reader)
print(f"${row['close']}")