-
Notifications
You must be signed in to change notification settings - Fork 3
/
list_of_companies.py
64 lines (53 loc) · 1.71 KB
/
list_of_companies.py
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
56
57
58
59
60
61
62
63
64
import requests
import json
import logging
from urllib.request import urlopen
import os
import sys
from dotenv import load_dotenv
load_dotenv()
from os import environ
te = open("newCompList.txt", "a") # File where you need to keep the logs
class Unbuffered:
def __init__(self, stream):
self.stream = stream
def write(self, data):
self.stream.write(data)
self.stream.flush()
te.write(data) # Write the data of stdout here to a text file as well
def flush(self):
pass
sys.stdout = Unbuffered(sys.stdout)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
symbols = []
try:
my_value_a = os.getenv("MY_VAR_A")
url = "https://financialmodelingprep.com/api/v3/stock/list?apikey=" + my_value_a
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
} #
print(url)
request = urlopen(url)
response = request.read()
data = json.loads(response) # print(data)
# li = [item.get('symbol') for item in data]
symbols.extend([item.get("symbol") for item in data])
request.close()
except IndexError:
print("Data not available (Index)" + "\n")
print(
"*****************************************************************************"
+ "\n"
)
except AttributeError as err:
print("Data not available (Attribute)" + "\n")
print(
"*****************************************************************************"
+ "\n"
)
logger.warning("The data was not available: {}".format(err) + "\n")
pass
print("*****My Symbols array*********" + "\n")
print(symbols)
print(len(symbols))