Created
October 18, 2022 06:36
-
-
Save patrikbraborec/e71f9bcbc6265f4c21153cd9149349e0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import http.client | |
| import json | |
| import os | |
| token = os.environ["RAPID_API_TOKEN"] | |
| stock_symbol = os.environ["STOCK_SYMBOL"] | |
| conn = http.client.HTTPSConnection("meteostat.p.rapidapi.com") | |
| headers = { | |
| 'X-RapidAPI-Key': token, | |
| 'X-RapidAPI-Host': "alpha-vantage.p.rapidapi.com" | |
| } | |
| conn.request( | |
| "GET", | |
| f"/query?function=TIME_SERIES_DAILY&symbol={stock_symbol}&outputsize=compact&datatype=json", | |
| headers=headers | |
| ) | |
| def get_data(): | |
| stock_data = {} | |
| try: | |
| res = conn.getresponse() | |
| data = res.read().decode("utf-8") | |
| data_json = json.loads(data) | |
| conn.close() | |
| stock_data["symbol"] = data_json["Meta Data"]["2. Symbol"] | |
| stock_data["data"] = data_json["Time Series (Daily)"] | |
| except: | |
| print("extract operation has failed") | |
| return stock_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment