-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxbrlparser.py
76 lines (72 loc) · 2.76 KB
/
xbrlparser.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
65
66
67
68
69
70
71
72
73
74
75
76
"""
Copyright C.C.:
Emiliano Hernandez Laos
https://github.com/emhlaos/
28/02/2018
"""
from urllib.request import urlopen
import os
from io import BytesIO
from zipfile import ZipFile
#LOAD FUNCTION:
currentdirectory = os.getcwd()
xbrldirectory = currentdirectory+"/xbrl"
if not os.path.exists(xbrldirectory): os.makedirs(xbrldirectory)
db = open(currentdirectory+"/babycaw.txt","r").read()
matrix = {}
rows = db.split("\n")
matrix["R.TIME"] = {}
n = 0
for t in rows[0].split(",")[1:]:
matrix["R.TIME"][n] = t
n=n+1
print(rows[1]," $$ ",rows[1].split(","))
for row in rows[1:]:
columns = row.split(",")
ticker = columns[0]
matrix[ticker]={}
n=0
for cell in columns[1:]:
matrix[ticker][n] = cell
n=n+1
#DOWNLOAD INFO:
revenue_matrix = matrix
allread = []
stocks = list(matrix.keys())[1:]
n=len(list(matrix["R.TIME"].keys()))
print(n,"\n",stocks)
for stock in stocks:
allread.append(stock)
for m in range(n):
print("Reading about "+stock)
if ".zip" in matrix[stock][m]:
with urlopen(matrix[stock][m]) as pzip:
with ZipFile(BytesIO(pzip.read())) as zp:
for file in zp.namelist():
print(file)
print("Dowloading: "+ stock + "_" + matrix["R.TIME"][m] + ".json")
try:
pjson = open(xbrldirectory+"/" + stock + "_" + matrix["R.TIME"][m] + ".json", "wb")
pjson.write(zp.read(file))
pjson.close()
except Exception as args:
print(args,"you got {}%".format(len(allread)/n))
teencow = open(currentdirectory+"/teencaw.txt", "w")
for riadboe in allread:
teencow.write(riadboe,"\n")
allread=[]
elif ".json" in matrix[stock][m]:
jsonurl = matrix[stock][m]
jsonresp = urlopen(jsonurl)
with urlopen(matrix[stock][m]) as pjson:
try:
print("Downloading",stock + "_" + matrix["R.TIME"][m] + ".json")
tempjson = open(xbrldirectory+"/" + stock + "_" + matrix["R.TIME"][m] + ".json", "wb")
tempjson.write(pjson.read())
tempjson.close()
except Exception as args:
print(args, "you got {}%".format(len(allread) / n),"ending at a json file JSUUN")
teencow = open(currentdirectory+"/teencaw.txt", "w")
for riadboe in allread:
teencow.write(riadboe, "\n")
allread = []