From 21efe665b09abc06e8e49f716b8f1b4a926c432f Mon Sep 17 00:00:00 2001 From: Douglas Fernandes Date: Mon, 5 Oct 2020 00:10:34 -0300 Subject: [PATCH 1/3] add: run with aws --- .gitignore | 3 + .vscode/settings.json | 3 + flaskProject/__init__.py | 0 flaskProject/main.py | 4 +- flaskProject/satelliteLocation.py | 179 +++++++++++++++--------------- install.sh | 3 + requirements.txt | 4 +- run.sh | 2 + 8 files changed, 105 insertions(+), 93 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 flaskProject/__init__.py create mode 100644 install.sh create mode 100644 run.sh diff --git a/.gitignore b/.gitignore index 6704566..30eea80 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,6 @@ dist # TernJS port file .tern-port + +__pycache__ +venv diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5b80df3 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "venv/bin/python" +} \ No newline at end of file diff --git a/flaskProject/__init__.py b/flaskProject/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/flaskProject/main.py b/flaskProject/main.py index fb0239b..d903b1b 100644 --- a/flaskProject/main.py +++ b/flaskProject/main.py @@ -1,5 +1,5 @@ from flask import Flask -import satelliteLocation as sl +from .satelliteLocation import SatelliteLocation import json from flask_cors import CORS, cross_origin @@ -9,6 +9,8 @@ satellite = {} satellite_list = [{}] +sl = SatelliteLocation() + app.config['CORS_HEADERS'] = 'Access-Control-Allow-Origin' @app.route('/') diff --git a/flaskProject/satelliteLocation.py b/flaskProject/satelliteLocation.py index 80fffc4..bd40401 100644 --- a/flaskProject/satelliteLocation.py +++ b/flaskProject/satelliteLocation.py @@ -30,97 +30,98 @@ TEMPLATE2 = "/above/{observer_lat:.5f}/{observer_lng:.5f}/{observer_alt:2f}/{search_radius:d}/0" -def parse_query(params, debug=False): - " Produce URL from parameter dictionary." - URL = "".join([BASE_URL, TEMPLATE.format(**params)]) - if debug: - print("Created query: {}".format(URL)) - return URL - -def parse_query_near(params, debug=False): - " Produce URL from parameter dictionary." - URL = "".join([BASE_URL, TEMPLATE2.format(**params)]) - if debug: - print("Created query: {}".format(URL)) - return URL - -def retrieve_data(QUERY_URL, debug=False): - "Retrieve data from given URL." - if debug: - print("Requesting data from: {}".format(QUERY_URL)) - r = requests.get(QUERY_URL, params={"apiKey" : API_KEY}) - if r.status_code == requests.codes.ok: +class SatelliteLocation: + def parse_query(params, debug=False): + " Produce URL from parameter dictionary." + URL = "".join([BASE_URL, TEMPLATE.format(**params)]) if debug: - print("Success.", frameinfo.lineno) - return r.json() - elif debug: - print("Failed! (status code {})".format(r.status_code)) - return "error :/" - return "{}" - -def retrieve_data_near(QUERY_URL, debug=False): - "Retrieve data from given URL." - if debug: - print("Requesting data from: {}".format(QUERY_URL)) - r = requests.get(QUERY_URL, params={"apiKey" : API_KEY}) - if r.status_code == requests.codes.ok: + print("Created query: {}".format(URL)) + return URL + + def parse_query_near(params, debug=False): + " Produce URL from parameter dictionary." + URL = "".join([BASE_URL, TEMPLATE2.format(**params)]) + if debug: + print("Created query: {}".format(URL)) + return URL + + def retrieve_data(QUERY_URL, debug=False): + "Retrieve data from given URL." if debug: - print("Success.", frameinfo.lineno) - return r.json() - elif debug: - print("Failed! (status code {})".format(r.status_code)) - return "error :/" - return "{}" - -def read_ids(filename, debug=False): - "Read a list of satellite ID numbers from given filename." - IDs = [] - if debug: - print("Parsing file: {}".format(filename)) - with open(filename, "r") as f: - for line in f: - ID = int(line.strip()) + print("Requesting data from: {}".format(QUERY_URL)) + r = requests.get(QUERY_URL, params={"apiKey" : API_KEY}) + if r.status_code == requests.codes.ok: if debug: - print(ID) - IDs.append(ID) - return IDs - -def get_single(ID, base_params, debug=False): - "Retrieve data of a single satellite based on ID." - query_params = base_params.copy() #pegou os dados de TOML - query_params["id"] = ID - if debug: - print("Query parameters:") - pprint(query_params) - URL = parse_query(query_params, debug) - data = retrieve_data(URL, debug) - return data - -def get_list_sat(debug, base_params): - query_params = base_params.copy() #pegar dados do toml - URL = parse_query_near(query_params, debug) - data = retrieve_data_near(URL, debug) - return data - -def get_all(filename, debug=False): - if path.exists("config.toml"): - base_params = toml.loads(open("config.toml").read()) - else: - print("Error: config.toml not found!") - exit() - "Retrieve data of all satellites in a list of IDs." - IDs = read_ids(filename, debug) - all_data = [get_single(ID, base_params ,debug) for ID in IDs] - return all_data - -def get_near(debug): - all_data = [] - for i in list_country: - print("in list...") - if path.exists(i): - base_params = toml.loads(open(i).read()) + print("Success.", frameinfo.lineno) + return r.json() + elif debug: + print("Failed! (status code {})".format(r.status_code)) + return "error :/" + return "{}" + + def retrieve_data_near(QUERY_URL, debug=False): + "Retrieve data from given URL." + if debug: + print("Requesting data from: {}".format(QUERY_URL)) + r = requests.get(QUERY_URL, params={"apiKey" : API_KEY}) + if r.status_code == requests.codes.ok: + if debug: + print("Success.", frameinfo.lineno) + return r.json() + elif debug: + print("Failed! (status code {})".format(r.status_code)) + return "error :/" + return "{}" + + def read_ids(filename, debug=False): + "Read a list of satellite ID numbers from given filename." + IDs = [] + if debug: + print("Parsing file: {}".format(filename)) + with open(filename, "r") as f: + for line in f: + ID = int(line.strip()) + if debug: + print(ID) + IDs.append(ID) + return IDs + + def get_single(ID, base_params, debug=False): + "Retrieve data of a single satellite based on ID." + query_params = base_params.copy() #pegou os dados de TOML + query_params["id"] = ID + if debug: + print("Query parameters:") + pprint(query_params) + URL = parse_query(query_params, debug) + data = retrieve_data(URL, debug) + return data + + def get_list_sat(debug, base_params): + query_params = base_params.copy() #pegar dados do toml + URL = parse_query_near(query_params, debug) + data = retrieve_data_near(URL, debug) + return data + + def get_all(filename, debug=False): + if path.exists("config.toml"): + base_params = toml.loads(open("config.toml").read()) else: - print("Error: " + i + " not found!") + print("Error: config.toml not found!") exit() - all_data.append(get_list_sat(debug, base_params)) - return all_data + "Retrieve data of all satellites in a list of IDs." + IDs = read_ids(filename, debug) + all_data = [get_single(ID, base_params ,debug) for ID in IDs] + return all_data + + def get_near(debug): + all_data = [] + for i in list_country: + print("in list...") + if path.exists(i): + base_params = toml.loads(open(i).read()) + else: + print("Error: " + i + " not found!") + exit() + all_data.append(get_list_sat(debug, base_params)) + return all_data diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..cce2297 --- /dev/null +++ b/install.sh @@ -0,0 +1,3 @@ +local_dir="$(dirname $0)" +virtualenv -p python3.6 $local_dir/venv +$local_dir/venv/bin/pip install -r $local_dir/requirements.txt diff --git a/requirements.txt b/requirements.txt index ba14824..28a2f58 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,4 @@ flask -flask-cors +flask_cors requests toml -pprint -inspect diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..8447128 --- /dev/null +++ b/run.sh @@ -0,0 +1,2 @@ +local_dir="$(dirname $0)" +PYTHON_PATH=$local_dir/ FLASK_APP=$local_dir/flaskProject/main.py flask run From 5fa6d1fb242411edb69f0c9258826e1f69c9faa8 Mon Sep 17 00:00:00 2001 From: Douglas Fernandes Date: Mon, 5 Oct 2020 00:25:52 -0300 Subject: [PATCH 2/3] add: run script --- flaskProject/main.py | 12 ++++-------- run.sh | 3 ++- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/flaskProject/main.py b/flaskProject/main.py index d903b1b..2ef0bdf 100644 --- a/flaskProject/main.py +++ b/flaskProject/main.py @@ -1,5 +1,5 @@ from flask import Flask -from .satelliteLocation import SatelliteLocation +from satelliteLocation import SatelliteLocation import json from flask_cors import CORS, cross_origin @@ -32,11 +32,7 @@ def prepare_near(): del i["info"] for j in i['above']: del j["intDesignator"] + return json.dumps(data) - - #para acessar: - #for i in data: - # for j in i['above']: - # print(j['satid']) - - return json.dumps(data) \ No newline at end of file +if __name__ == '__main__': + app.run() diff --git a/run.sh b/run.sh index 8447128..907f953 100644 --- a/run.sh +++ b/run.sh @@ -1,2 +1,3 @@ local_dir="$(dirname $0)" -PYTHON_PATH=$local_dir/ FLASK_APP=$local_dir/flaskProject/main.py flask run +run=$local_dir/flaskProject/main.py +PYTHON_PATH=$local_dir/ FLASK_APP=$run $local_dir/venv/bin/python $run From cc61879a3ec28f5112c48fe396595fb82933e600 Mon Sep 17 00:00:00 2001 From: Douglas Fernandes Date: Mon, 5 Oct 2020 00:34:42 -0300 Subject: [PATCH 3/3] add: aws acceptable host --- flaskProject/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flaskProject/main.py b/flaskProject/main.py index 2ef0bdf..e2d2dff 100644 --- a/flaskProject/main.py +++ b/flaskProject/main.py @@ -35,4 +35,4 @@ def prepare_near(): return json.dumps(data) if __name__ == '__main__': - app.run() + app.run(host= '0.0.0.0')