#!/usr/bin/env python3 # Fetch stock market data from alphavantage.co import requests import time import string import random def key_generator(size=16, chars=string.ascii_uppercase + string.digits): return ''.join(random.choice(chars) for _ in range(size)) def api_timing(sleeptime=12.1): # free API key allows up to 5 requests per minute, # 500 requests per day # so we sleep for 12.1 sec to avoid errors time.sleep(sleeptime) def get_av_quote(symbol): apikey = key_generator() apiurl = 'https://www.alphavantage.co/query?' apifunction = 'GLOBAL_QUOTE' payload = {'function': apifunction, 'symbol': symbol, 'apikey': apikey} my_response = requests.get(apiurl, params=payload) quote = False if (my_response.ok): j_data = my_response.json() if 'Global Quote' in j_data: quote = float(j_data['Global Quote']['05. price']) api_timing() return quote def get_av_fx(from_currency, to_currency='EUR'): apikey = key_generator() apiurl = 'https://www.alphavantage.co/query?' apifunction = 'CURRENCY_EXCHANGE_RATE' payload = {'function': apifunction, 'from_currency': from_currency, 'to_currency': to_currency, 'apikey': apikey} my_response = requests.get(apiurl, params=payload) fx_rate = False if (my_response.ok): j_data = my_response.json() if 'Realtime Currency Exchange Rate' in j_data: fx_rate = float( j_data['Realtime Currency Exchange Rate']['5. Exchange Rate']) api_timing() return fx_rate