-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrapeAPI.py
207 lines (163 loc) · 5.36 KB
/
scrapeAPI.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# script for querying the vantage point api for the top 50 stocks in every sector
import json
import time
import requests
import config
# import files from frontend
# import <database models.py >
# ------------
# to query
# ------------
bad_devices = []
# returns where a device is updated by checking against current version
def queryScraperAPI(url, device_vers):
"""
url would have to have https://
"""
api_url = 'http://api.scrapestack.com/scrape?access_key='+config.api_key+'&url='+ str(url)
response = requests.get(api_url)
if response: #status_code == 200:
#print("status code: " + str(response.status_code))
# parse json response here
json_file = response.json()
if str(device_vers) == str(json_file["version"]) :
return True
else:
return False
else: # status_code == 404:
print(url + ' not Found. status code: ' + str(response.status_code))
return 0
# api for recalls and known vulnerabilities
# openFDA
# returns true if not recalled
def queryRecallAPI(device_name):
# format name
device_name = device_name.strip().replace(" ", "+")
api_url = 'https://api.fda.gov/device/recall.json?search=device_name:'+device_name
response = requests.get(api_url)
if response: #status_code == 200:
#print("status code: " + str(response.status_code))
# parse json response here
json_file = response.json()
return json_file["results"][0]["device_name"]
return True
# parses given device info from input file
def parseInput():
# take in input file given
# extract url and version
# pass url,version into queryScrapeAPI
# if file is a json
# open file for read
# pop each line
# for all devices :
is_updated_version = queryScraperAPI("https://raw.githubusercontent.com/CycloneDX/sbom-examples/master/dropwizard-1.3.15/bom.json", 1)
is_not_recalled = queryRecallAPI("asasasaunicorn")
is_good = is_updated_version and is_not_recalled
print(is_good)
# if (is_good == False) :
# add to bad_devices
# create and return a file that contains all bad devices
def createOutputFile():
# ------------
# main
# ------------
def main() :
"""
some descriptive info here
"""
parseInput()
# createOutputFile() - send to frontend
main()
# -------
# ------------
# adds an object into an established database
# ------------
'''
def create_object(st):
# iterate through each stock json object
stock_sym = st["Symbol"]
# check for existing item
q = db.session.query(stocks).filter(stocks.sym == stock_sym).all()
if len(q) == 0:
stock_name = st['Name']
country = st["Country"]
stock_index = st["Exchange"]
stock_sect = st["Sector"]
stock_indu = st["Industry"]
# new stock object
newStock = stocks( sym = stock_sym, name = stock_name, country = country, sector = stock_sect, industry = stock_indu, index = stock_index)
db.session.add(newStock)
print("Stock: " + stock_sym + " added")
# make a catalog of all of the sectors and the corresponding industries
q = db.session.query(sectors).filter(sectors.name == stock_sect).all()
if len(q) == 0:
# new sector object
newSector = sectors( name = stock_sect)
db.session.add(newSector)
print("Sector: " + stock_sect + " added")
# checking industry
q = db.session.query(industries).filter(industries.name == stock_indu).all()
if len(q) == 0:
# new sector object
newIndustry = industries( name = stock_indu, sector_name = stock_sect)
db.session.add(newIndustry)
print("Industry: " + stock_indu + " added")
# commit the session to my DB.
db.session.commit()
'''
'''
def populatestocksDB(stock_list):
# loop through each stock
# query
# save relevant info to json/dict
# wait 15 seconds
for stock in stock_list:
print("querying "+ stock)
stock_json = queryAlphaVantage(stock)
if stock_json != 0 :
print("creating instance of "+ stock)
create_object(stock_json)
print("waiting 15 seconds")
time.sleep(15)
print("Done")
'''
#print("starting query")
#populatestocksDB(stock_small)
# loop through each stock
# query
# save relevant info to json/dict
# wait 15 seconds
'''
for sto in stock:
print("querying "+ sto)
stock_json = queryAlphaVantage(sto)
if stock_json != 0 :
if len(stock_json) > 0 :
print("creating instance of "+ sto)
create_object(stock_json)
else:
print("no instance of "+ sto + " from query")
print("waiting 15 seconds")
time.sleep(15)
print("Done")
'''
'''
# json dump
# input: stock_id / stock symbol
# returns: [status code 200] json
@app.route('/stocks/company_overview_json/<str: stock_id>/')
def companyOverviewJson(stock_id):
# check if it is zero for failed response
return json.dumps( queryCompanyOverview(stock_id) )
'''
'''
# might not need
# process the json?
# input: stock_id / stock symbol
# returns: [status code 200] json
@app.route('/stocks/company_overview/<str: stock_id>/')
def companyOverview(stock_id):
stock_json = queryCompanyOverview(stock_id)
# check if it is zero for failed response
#return render_template()
'''