Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
45 changes: 39 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -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)

22 changes: 11 additions & 11 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions templates/response.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Link to Pig Latinizer Result</title>
</head>
<body>
<a href={{ link }}>{{ link }}</a>
</body>
</html>