From 009e9eb646dcb1be994f20dd475723693d18472d Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Mon, 21 Sep 2020 11:19:45 -0400 Subject: [PATCH 1/5] sp_py230 lesson5, setup and start use request lib --- goMainRun.bat | 2 ++ main.py | 38 +++++++++++++++++++++++++++++++++++++- readmeSetupSteps.txt | 4 ++++ requirements.txt | 2 +- 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 goMainRun.bat create mode 100644 readmeSetupSteps.txt diff --git a/goMainRun.bat b/goMainRun.bat new file mode 100644 index 0000000..4635404 --- /dev/null +++ b/goMainRun.bat @@ -0,0 +1,2 @@ + +start python -u main.py diff --git a/main.py b/main.py index bc096fb..94977c3 100644 --- a/main.py +++ b/main.py @@ -6,20 +6,56 @@ app = Flask(__name__) +### +###//hidden-journey-62459.herokuapp.com/piglatinize/ +### r = requests.post('https://httpbin.org/post', data = {'key':'value'}) +### +#payload = {'key1': 'value1', 'key2': 'value2'} +#>>> r = requests.post("https://httpbin.org/post", data=payload) +#>>> print(r.text) def get_fact(): + payload = {'input_text': 'test'} + + #resource_url = "https://hidden-journey-62459.herokuapp.com/" + #r = requests.post(resource_url, allow_redirects=False, data=payload) + + resource_url = "https://hidden-journey-62459.herokuapp.com/piglatinize/" + r = requests.post(resource_url, allow_redirects=True, data=payload) + + print(f"***MMM response text = -{r.text}-") + + #soup = BeautifulSoup(response.content, "html.parser") + #facts = soup.find_all("div", id="content") + + #print(f"***MMM facts = -{facts[0].getText()}-") + + return r.text + + +def get_fact_00(): + response = requests.get("http://unkno.com") soup = BeautifulSoup(response.content, "html.parser") facts = soup.find_all("div", id="content") + print(f"***MMM facts = -{facts[0].getText()}-") + return facts[0].getText() @app.route('/') def home(): - return "FILL ME!" + + print("***MMM aaaaaaaaaaaa") + result_fact = get_fact() + + #print(f"***MMM called result fact = -{facts[0].getText()}-") + + #return "FILL ME!" + return result_fact if __name__ == "__main__": diff --git a/readmeSetupSteps.txt b/readmeSetupSteps.txt new file mode 100644 index 0000000..423eee1 --- /dev/null +++ b/readmeSetupSteps.txt @@ -0,0 +1,4 @@ + +mkvirtualenv ustjay-ethay-actsfayig +pip install -r requirements.txt +python -u main.py diff --git a/requirements.txt b/requirements.txt index 0ade18d..c4fd48f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,7 +6,7 @@ Flask==1.0.1 idna==2.6 itsdangerous==0.24 Jinja2==2.10 -MarkupSafe==1.0 +MarkupSafe==1.1.1 requests==2.18.4 urllib3==1.22 Werkzeug==0.14.1 From 9343b18186929cc293f26d9556734f33e408c0a1 Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Mon, 21 Sep 2020 17:18:01 -0400 Subject: [PATCH 2/5] sp_py230 lesson5, handle resp redirect --- main.py | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 94977c3..d3a04be 100644 --- a/main.py +++ b/main.py @@ -14,18 +14,37 @@ #>>> r = requests.post("https://httpbin.org/post", data=payload) #>>> print(r.text) -def get_fact(): +def latinize(in_fact): - payload = {'input_text': 'test'} + print(f"***MMM arg fact to latinize = -{in_fact}-") + #***MMM + #fact_to_latinize = in_fact + fact_to_latinize = 'test' + + payload = {'input_text': fact_to_latinize} #resource_url = "https://hidden-journey-62459.herokuapp.com/" - #r = requests.post(resource_url, allow_redirects=False, data=payload) + #r = requests.post(resource_url, data=payload, allow_redirects=False) resource_url = "https://hidden-journey-62459.herokuapp.com/piglatinize/" - r = requests.post(resource_url, allow_redirects=True, data=payload) + r = requests.post(resource_url, data=payload, allow_redirects=False) print(f"***MMM response text = -{r.text}-") + for key, value in r.headers.items(): + print(f"***MMM 111111111111 key = -{key}- value = -{value}-") + + try: + #latinized_result_url = r.headers['content-type'] + latinized_result_url = r.headers['Location'] + print(f"***MMM latinized_result_url = -{latinized_result_url}-") + except Exception as err: + print(f'***MMM 555555555555555555555Other error occurred: {err}') + + print(f"***MMM 333333333333333333333") + + + #soup = BeautifulSoup(response.content, "html.parser") #facts = soup.find_all("div", id="content") @@ -34,28 +53,38 @@ def get_fact(): return r.text -def get_fact_00(): +def get_random_fact(): response = requests.get("http://unkno.com") soup = BeautifulSoup(response.content, "html.parser") facts = soup.find_all("div", id="content") - print(f"***MMM facts = -{facts[0].getText()}-") + random_fact = facts[0].getText() + + print(f"***MMM a random fact = -{random_fact}-") + + return random_fact + +def do_latinize_a_random_fact(): + + random_fact = get_random_fact() + latinized_fact = latinize(random_fact) - return facts[0].getText() + return latinized_fact + #return "hello mike" @app.route('/') def home(): print("***MMM aaaaaaaaaaaa") - result_fact = get_fact() + body = do_latinize_a_random_fact() #print(f"***MMM called result fact = -{facts[0].getText()}-") #return "FILL ME!" - return result_fact + return body if __name__ == "__main__": From 8029b50f193a1968af88aff6128bc2bb63579936 Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Tue, 22 Sep 2020 10:02:40 -0400 Subject: [PATCH 3/5] sp_py230 lesson5, return result Location from hdr --- main.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index d3a04be..23d7c8f 100644 --- a/main.py +++ b/main.py @@ -31,13 +31,14 @@ def latinize(in_fact): print(f"***MMM response text = -{r.text}-") + latinized_result_url = "" for key, value in r.headers.items(): print(f"***MMM 111111111111 key = -{key}- value = -{value}-") try: #latinized_result_url = r.headers['content-type'] latinized_result_url = r.headers['Location'] - print(f"***MMM latinized_result_url = -{latinized_result_url}-") + print(f"***MMM 44444444444444444444444 latinized_result_url = -{latinized_result_url}-") except Exception as err: print(f'***MMM 555555555555555555555Other error occurred: {err}') @@ -50,7 +51,14 @@ def latinize(in_fact): #print(f"***MMM facts = -{facts[0].getText()}-") - return r.text + + #return r.text + + result = r.text + if latinized_result_url != "": + result = latinized_result_url + + return result def get_random_fact(): From 988afc8baf173c9af247a2e85982c3ebea38b3ab Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Tue, 22 Sep 2020 10:22:26 -0400 Subject: [PATCH 4/5] Innitial commit. --- main.py | 65 ++++++++++++++++++++------------------------------------- 1 file changed, 23 insertions(+), 42 deletions(-) diff --git a/main.py b/main.py index 23d7c8f..1bebd6f 100644 --- a/main.py +++ b/main.py @@ -6,54 +6,37 @@ app = Flask(__name__) -### -###//hidden-journey-62459.herokuapp.com/piglatinize/ -### r = requests.post('https://httpbin.org/post', data = {'key':'value'}) -### -#payload = {'key1': 'value1', 'key2': 'value2'} -#>>> r = requests.post("https://httpbin.org/post", data=payload) -#>>> print(r.text) def latinize(in_fact): - print(f"***MMM arg fact to latinize = -{in_fact}-") + ### latinize a supplied fact - #***MMM - #fact_to_latinize = in_fact - fact_to_latinize = 'test' + print(f"diag arg fact to latinize = -{in_fact}-") + fact_to_latinize = in_fact + + # prepare the payload from form data, and initialize option allow_redirects off payload = {'input_text': fact_to_latinize} - #resource_url = "https://hidden-journey-62459.herokuapp.com/" - #r = requests.post(resource_url, data=payload, allow_redirects=False) - resource_url = "https://hidden-journey-62459.herokuapp.com/piglatinize/" r = requests.post(resource_url, data=payload, allow_redirects=False) - print(f"***MMM response text = -{r.text}-") - + # r contains the response from the latinizer api + latinized_result_url = "" for key, value in r.headers.items(): - print(f"***MMM 111111111111 key = -{key}- value = -{value}-") - + #print(f"diag key = -{key}- value = -{value}-") + pass + try: - #latinized_result_url = r.headers['content-type'] + # extract the location link from the response header latinized_result_url = r.headers['Location'] - print(f"***MMM 44444444444444444444444 latinized_result_url = -{latinized_result_url}-") + #print(f"diag latinized_result_url = -{latinized_result_url}-") except Exception as err: - print(f'***MMM 555555555555555555555Other error occurred: {err}') + latinized_result_url = "" + #print(f'diag latinizer error occurred: {err}') + pass - print(f"***MMM 333333333333333333333") - - - - #soup = BeautifulSoup(response.content, "html.parser") - #facts = soup.find_all("div", id="content") - - #print(f"***MMM facts = -{facts[0].getText()}-") - - - #return r.text - + # prepare the result (url to latinized result) result = r.text if latinized_result_url != "": result = latinized_result_url @@ -63,6 +46,8 @@ def latinize(in_fact): def get_random_fact(): + ### get a random fact from unkno api + response = requests.get("http://unkno.com") soup = BeautifulSoup(response.content, "html.parser") @@ -70,28 +55,24 @@ def get_random_fact(): random_fact = facts[0].getText() - print(f"***MMM a random fact = -{random_fact}-") + #print(f"diag a random fact = -{random_fact}-") return random_fact + def do_latinize_a_random_fact(): + ### latinize a random fact control + random_fact = get_random_fact() latinized_fact = latinize(random_fact) return latinized_fact - #return "hello mike" - + @app.route('/') def home(): - - print("***MMM aaaaaaaaaaaa") body = do_latinize_a_random_fact() - - #print(f"***MMM called result fact = -{facts[0].getText()}-") - - #return "FILL ME!" return body From 57f523450d6d605a39f55ec9a4bbcacadf6e49af Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Tue, 22 Sep 2020 10:30:12 -0400 Subject: [PATCH 5/5] sp_py230 lesson5, final refactor deploy to heroku --- readme_heroku_deployed_url.txt | 2 + results_heroku_deploy.txt | 80 ++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 readme_heroku_deployed_url.txt create mode 100644 results_heroku_deploy.txt diff --git a/readme_heroku_deployed_url.txt b/readme_heroku_deployed_url.txt new file mode 100644 index 0000000..0541abe --- /dev/null +++ b/readme_heroku_deployed_url.txt @@ -0,0 +1,2 @@ + +https://evening-retreat-32147.herokuapp.com/ diff --git a/results_heroku_deploy.txt b/results_heroku_deploy.txt new file mode 100644 index 0000000..491fa2c --- /dev/null +++ b/results_heroku_deploy.txt @@ -0,0 +1,80 @@ + +(ustjay-ethay-actsfayig) C:\A_uwPython\SP_Online_PY230\lesson5\assignment\ustjay-ethay-actsfayig\ustjay-ethay-actsfayig>heroku create +Creating app... done, ? evening-retreat-32147 +https://evening-retreat-32147.herokuapp.com/ | https://git.heroku.com/evening-retreat-32147.git + +(ustjay-ethay-actsfayig) C:\A_uwPython\SP_Online_PY230\lesson5\assignment\ustjay-ethay-actsfayig\ustjay-ethay-actsfayig>git remote -v +heroku https://git.heroku.com/evening-retreat-32147.git (fetch) +heroku https://git.heroku.com/evening-retreat-32147.git (push) +origin https://github.com/mmanc125uw/ustjay-ethay-actsfayig.git (fetch) +origin https://github.com/mmanc125uw/ustjay-ethay-actsfayig.git (push) + +(ustjay-ethay-actsfayig) C:\A_uwPython\SP_Online_PY230\lesson5\assignment\ustjay-ethay-actsfayig\ustjay-ethay-actsfayig>git add . + +(ustjay-ethay-actsfayig) C:\A_uwPython\SP_Online_PY230\lesson5\assignment\ustjay-ethay-actsfayig\ustjay-ethay-actsfayig>git commit -a -m "Innitial commit. +[master 988afc8] Innitial commit. + 1 file changed, 23 insertions(+), 42 deletions(-) + +(ustjay-ethay-actsfayig) C:\A_uwPython\SP_Online_PY230\lesson5\assignment\ustjay-ethay-actsfayig\ustjay-ethay-actsfayig>git push heroku master +Counting objects: 20, done. +Delta compression using up to 4 threads. +Compressing objects: 100% (18/18), done. +Writing objects: 100% (20/20), 3.16 KiB | 1.05 MiB/s, done. +Total 20 (delta 8), reused 0 (delta 0) +remote: Compressing source files... done. +remote: Building source: +remote: +remote: -----> Python app detected +remote: -----> Installing python-3.6.12 +remote: -----> Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2 +remote: -----> Installing SQLite3 +remote: -----> Installing requirements with pip +remote: Collecting beautifulsoup4==4.6.0 +remote: Downloading beautifulsoup4-4.6.0-py3-none-any.whl (86 kB) +remote: Collecting certifi==2018.4.16 +remote: Downloading certifi-2018.4.16-py2.py3-none-any.whl (150 kB) +remote: Collecting chardet==3.0.4 +remote: Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB) +remote: Collecting click==6.7 +remote: Downloading click-6.7-py2.py3-none-any.whl (71 kB) +remote: Collecting Flask==1.0.1 +remote: Downloading Flask-1.0.1-py2.py3-none-any.whl (91 kB) +remote: Collecting idna==2.6 +remote: Downloading idna-2.6-py2.py3-none-any.whl (56 kB) +remote: Collecting itsdangerous==0.24 +remote: Downloading itsdangerous-0.24.tar.gz (46 kB) +remote: Collecting Jinja2==2.10 +remote: Downloading Jinja2-2.10-py2.py3-none-any.whl (126 kB) +remote: Collecting MarkupSafe==1.1.1 +remote: Downloading MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl (27 kB) +remote: Collecting requests==2.18.4 +remote: Downloading requests-2.18.4-py2.py3-none-any.whl (88 kB) +remote: Collecting urllib3==1.22 +remote: Downloading urllib3-1.22-py2.py3-none-any.whl (132 kB) +remote: Collecting Werkzeug==0.14.1 +remote: Downloading Werkzeug-0.14.1-py2.py3-none-any.whl (322 kB) +remote: Building wheels for collected packages: itsdangerous +remote: Building wheel for itsdangerous (setup.py): started +remote: Building wheel for itsdangerous (setup.py): finished with status 'done' +remote: Created wheel for itsdangerous: filename=itsdangerous-0.24-py3-none-any.whl size=10621 sha256=df326338f0a2e9d1212884a31c2c403f685940b3c2a +50ec52d9f6c8333 +remote: Stored in directory: /tmp/pip-ephem-wheel-cache-vu_kve52/wheels/87/c5/5a/ec57bae8cc6dc7933ea260ae3170fd858bec5673131c289a95 +remote: Successfully built itsdangerous +remote: Installing collected packages: beautifulsoup4, certifi, chardet, click, Werkzeug, MarkupSafe, Jinja2, itsdangerous, Flask, idna, urllib3, r +s +remote: Successfully installed Flask-1.0.1 Jinja2-2.10 MarkupSafe-1.1.1 Werkzeug-0.14.1 beautifulsoup4-4.6.0 certifi-2018.4.16 chardet-3.0.4 click- +na-2.6 itsdangerous-0.24 requests-2.18.4 urllib3-1.22 +remote: -----> Discovering process types +remote: Procfile declares types -> web +remote: +remote: -----> Compressing... +remote: Done: 46.1M +remote: -----> Launching... +remote: Released v3 +remote: https://evening-retreat-32147.herokuapp.com/ deployed to Heroku +remote: +remote: Verifying deploy... done. +To https://git.heroku.com/evening-retreat-32147.git + * [new branch] master -> master + +(ustjay-ethay-actsfayig) C:\A_uwPython\SP_Online_PY230\lesson5\assignment\ustjay-ethay-actsfayig\ustjay-ethay-actsfayig>heroku open