diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..d6f210b Binary files /dev/null and b/.DS_Store differ diff --git a/main.py b/main.py index bc096fb..675e5bd 100644 --- a/main.py +++ b/main.py @@ -1,28 +1,61 @@ -import os +""" +For this week, you're going to be working on a mashup! You'll use the code to scrape a random +fact from http://unkno.com (Links to an external site.)Links to an external site. that we +developed in class, and send it to a pig latin web application running on Heroku. The address +of the Pig Latinizer is: +https://hidden-journey-62459.herokuapp.com/ (Links to an external site.) + +The requirement is: + +You should deploy your assignment to Heroku. +Whenever someone visits your home page, it should scrape a new fact from unkno.com, send that +fact to the pig latin website, and print out the address for that piglatinized fact on the home +page. + +If you'd like to be fancy, then you can print the address as a clickable link. +""" +import os import requests -from flask import Flask, send_file, Response +from flask import Flask, render_template, send_file, Response from bs4 import BeautifulSoup app = Flask(__name__) def get_fact(): - + """ + Return the random fact from http://unkno.com as a string. + """ response = requests.get("http://unkno.com") - soup = BeautifulSoup(response.content, "html.parser") facts = soup.find_all("div", id="content") return facts[0].getText() +def get_link(fact): + """ + Feed the fact argument as a post to the pig latinizer, and return the resulting url. + + fact: the random fact from get_fact as a string. + """ + payload = {'input_text': fact} + response = requests.post('https://hidden-journey-62459.herokuapp.com/piglatinize/', + data=payload, allow_redirects=False) + + return response.headers['Location'] + + @app.route('/') def home(): - return "FILL ME!" + fact = get_fact() + link_ = get_link(fact) + + # return an html rendered response containing a hyperlink + return render_template('response.jinja2', link=link_) if __name__ == "__main__": port = int(os.environ.get("PORT", 6787)) app.run(host='0.0.0.0', port=port) - diff --git a/requirements.txt b/requirements.txt index 0ade18d..7c5da4a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,12 +1,12 @@ -beautifulsoup4==4.6.0 -certifi==2018.4.16 +beautifulsoup4==4.9.1 +certifi==2020.4.5.1 chardet==3.0.4 -click==6.7 -Flask==1.0.1 -idna==2.6 -itsdangerous==0.24 -Jinja2==2.10 -MarkupSafe==1.0 -requests==2.18.4 -urllib3==1.22 -Werkzeug==0.14.1 +click==7.1.1 +Flask==1.1.2 +idna==2.9 +itsdangerous==1.1.0 +Jinja2==2.11.1 +MarkupSafe==1.1.1 +requests==2.23.0 +urllib3==1.25.8 +Werkzeug==1.0.1 \ No newline at end of file diff --git a/templates/response.jinja2 b/templates/response.jinja2 new file mode 100644 index 0000000..f425dd0 --- /dev/null +++ b/templates/response.jinja2 @@ -0,0 +1,8 @@ + +
+