Skip to content

Commit 1d21cbc

Browse files
Day 14 - Web App with Flask, FastAPI, ngrok, and Invictify
1 parent be86c43 commit 1d21cbc

16 files changed

+640
-0
lines changed

tutorial-reference/Day 14/Pipfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
8+
[packages]
9+
flask = "*"
10+
gunicorn = "*"
11+
uvicorn = "*"
12+
fastapi = "*"
13+
requests = "*"
14+
pandas = "*"
15+
requests-html = "*"
16+
17+
[requires]
18+
python_version = "3.8"

tutorial-reference/Day 14/Pipfile.lock

Lines changed: 438 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Rank,Release Group,Worldwide,Domestic,%,Foreign,%
2+
1,Bad Boys for Life,"$419,074,646","$204,417,855",48.8%,"$214,656,791",51.2%
3+
2,Sonic the Hedgehog,"$306,766,470","$146,066,470",47.6%,"$160,700,000",52.4%
4+
3,Dolittle,"$223,343,452","$77,047,065",34.5%,"$146,296,387",65.5%
5+
4,Birds of Prey: And the Fantabulous Emancipation of One Harley Quinn,"$201,858,461","$84,158,461",41.7%,"$117,700,000",58.3%
6+
5,The Invisible Man,"$122,914,050","$64,914,050",52.8%,"$58,000,000",47.2%
7+
6,The Gentlemen,"$114,996,853","$36,296,853",31.6%,"$78,700,000",68.4%
8+
7,The Call of the Wild,"$107,604,626","$62,342,368",57.9%,"$45,262,258",42.1%
9+
8,The Grudge,"$49,511,319","$21,221,803",42.9%,"$28,289,516",57.1%
10+
9,Fantasy Island,"$47,315,959","$26,441,782",55.9%,"$20,874,177",44.1%
11+
10,Underwater,"$40,882,928","$17,291,078",42.3%,"$23,591,850",57.7%
12+
11,Like a Boss,"$29,753,143","$22,169,514",74.5%,"$7,583,629",25.5%
13+
12,Bloodshot,"$28,428,855","$10,021,787",35.3%,"$18,407,068",64.7%
14+
13,Emma.,"$25,155,355","$10,055,355",40%,"$15,100,000",60%
15+
14,Gretel & Hansel,"$21,282,967","$15,347,654",72.1%,"$5,935,313",27.9%
16+
15,Brahms: The Boy II,"$20,311,536","$12,611,536",62.1%,"$7,700,000",37.9%
17+
16,The Turning,"$18,474,062","$15,472,775",83.8%,"$3,001,287",16.2%
18+
17,The Way Back,"$14,690,514","$13,590,514",92.5%,"$1,100,000",7.5%
19+
18,La Belle Époque,"$14,298,084",-,-,"$14,298,084",100%
20+
19,I Still Believe,"$10,484,984","$9,868,521",94.1%,"$616,463",5.9%
21+
20,The Hunt,"$6,512,500","$5,812,500",89.3%,"$700,000",10.7%
22+
21,Las Pildoras De Mi Novio,"$6,292,780","$2,971,116",47.2%,"$3,321,664",52.8%
23+
22,The Rhythm Section,"$5,989,583","$5,437,971",90.8%,"$551,612",9.2%
24+
23,My Spy,"$4,418,501",-,-,"$4,418,501",100%
25+
24,The Last Full Measure,"$3,006,059","$2,949,212",98.1%,"$56,847",1.9%
26+
25,The Lodge,"$2,679,438","$1,666,564",62.2%,"$1,012,874",37.8%
27+
26,Trolls World Tour,"$1,887,828",-,-,"$1,887,828",100%
28+
27,Blind Eyes Opened,"$234,505","$234,505",100%,-,-
29+
28,Burden,"$139,270","$139,270",100%,-,-
30+
29,"14 Days, 12 Nights","$122,806","$122,530",99.8%,$276,0.2%
31+
30,First Cow,"$101,068","$101,068",100%,-,-
32+
31,Run,"$4,052",-,-,"$4,052",100%
33+
32,Mulan,$874,-,-,$874,100%
34+
33,A Quiet Place Part II,$159,-,-,$159,100%

tutorial-reference/Day 14/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>Hello World</h1>
2+
<p>http://localhost:8000</p>
3+
<p>http://127.0.0.1:8000</p>

tutorial-reference/Day 14/logger.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
import datetime
3+
4+
BASE_DIR = os.path.dirname(__file__)
5+
log_dir = os.path.join(BASE_DIR, "logs")
6+
os.makedirs(log_dir, exist_ok=True)
7+
8+
def trigger_log_save():
9+
filename = f"{datetime.datetime.now()}.txt"
10+
filepath = os.path.join(log_dir, filename)
11+
with open(filepath, 'w+') as f:
12+
f.write("")

tutorial-reference/Day 14/logs/2020-04-06 11:22:00.968551.txt

Whitespace-only changes.

tutorial-reference/Day 14/logs/2020-04-06 12:22:00.912814.txt

Whitespace-only changes.

tutorial-reference/Day 14/logs/2020-04-06 13:22:00.853037.txt

Whitespace-only changes.

tutorial-reference/Day 14/run1.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gunicorn server1:app --bind 127.0.0.1:8888

tutorial-reference/Day 14/run1.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gunicorn server1:app --bind 127.0.0.1:8888

tutorial-reference/Day 14/run2.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uvicorn server2:app --port 8888

tutorial-reference/Day 14/run2.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uvicorn server2:app --port 8888

tutorial-reference/Day 14/scrape.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os
2+
import sys
3+
import datetime
4+
import requests
5+
import pandas as pd
6+
from requests_html import HTML
7+
8+
BASE_DIR = os.path.dirname(__file__)
9+
10+
11+
12+
def url_to_txt(url, filename="world.html", save=False):
13+
r = requests.get(url)
14+
if r.status_code == 200:
15+
html_text = r.text
16+
if save:
17+
with open(f"world-{year}.html", 'w') as f:
18+
f.write(html_text)
19+
return html_text
20+
return None
21+
22+
23+
24+
def parse_and_extract(url, name='2020'):
25+
html_text = url_to_txt(url)
26+
if html_text == None:
27+
return False
28+
r_html = HTML(html=html_text)
29+
table_class = ".imdb-scroll-table"
30+
# table_class = "#table"
31+
r_table = r_html.find(table_class)
32+
33+
# print(r_table)
34+
table_data = []
35+
# table_data_dicts = []
36+
header_names = []
37+
if len(r_table) == 0:
38+
return False
39+
parsed_table = r_table[0]
40+
rows = parsed_table.find("tr")
41+
header_row = rows[0]
42+
header_cols = header_row.find('th')
43+
header_names = [x.text for x in header_cols]
44+
for row in rows[1:]:
45+
# print(row.text)
46+
cols = row.find("td")
47+
row_data = []
48+
row_dict_data = {}
49+
for i, col in enumerate(cols):
50+
# print(i, col.text, '\n\n')
51+
header_name = header_names[i]
52+
# row_dict_data[header_name] = col.text
53+
row_data.append(col.text)
54+
# table_data_dicts.append(row_dict_data)
55+
table_data.append(row_data)
56+
df = pd.DataFrame(table_data, columns=header_names)
57+
# df = pd.DataFrame(table_data_dicts)
58+
path = os.path.join(BASE_DIR, 'data')
59+
os.makedirs(path, exist_ok=True)
60+
filepath = os.path.join('data', f'{name}.csv')
61+
df.to_csv(filepath, index=False)
62+
return True
63+
64+
def run(start_year=None, years_ago=0):
65+
if start_year == None:
66+
now = datetime.datetime.now()
67+
start_year = now.year
68+
assert isinstance(start_year, int)
69+
assert isinstance(years_ago, int)
70+
assert len(f"{start_year}") == 4
71+
for i in range(0, years_ago+1):
72+
url = f"https://www.boxofficemojo.com/year/world/{start_year}/"
73+
finished = parse_and_extract(url, name=start_year)
74+
if finished:
75+
print(f"Finished {start_year}")
76+
else:
77+
print(f"{start_year} not finished")
78+
start_year -= 1

tutorial-reference/Day 14/server1.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from flask import Flask
2+
3+
from logger import trigger_log_save
4+
from scrape import run as scrape_runner
5+
6+
app = Flask(__name__)
7+
8+
# http://localhost:8000/
9+
@app.route("/", methods=['GET'])
10+
def hello_world():
11+
# run other code here.
12+
return "Hello, world. this is Flask"
13+
14+
# http://localhost:8000/abc
15+
@app.route("/abc", methods=['GET'])
16+
def abc_view():
17+
# run other code here.
18+
return "Hello, world. this is abc"
19+
20+
@app.route("/box-office-mojo-scraper", methods=['POST'])
21+
def box_office_scraper_view():
22+
# run other code here.
23+
trigger_log_save()
24+
scrape_runner()
25+
return {"data": [1,2,3]}

tutorial-reference/Day 14/server2.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
import datetime
3+
from fastapi import FastAPI
4+
from logger import trigger_log_save
5+
from scrape import run as scrape_runner
6+
app = FastAPI()
7+
8+
@app.get("/")
9+
def hello_world():
10+
return {"hello": "world"}
11+
12+
@app.get("/abc")
13+
def abc_view():
14+
return {"data": [1,2,3]}
15+
16+
17+
@app.post("/box-office-mojo-scraper")
18+
def scrape_runner_view():
19+
trigger_log_save()
20+
scrape_runner()
21+
return {"data": [1,2,3]}

tutorial-reference/Day 14/trigger.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import requests
2+
3+
ngrok_url = 'https://a5681caa.ngrok.io'
4+
endpoint = f'{ngrok_url}/box-office-mojo-scraper'
5+
6+
r = requests.post(endpoint, json={})
7+
print(r.json()['data'])

0 commit comments

Comments
 (0)