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

Task02_Gamaonov #5

Open
wants to merge 3 commits 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__pycache__/
.ipynb_checkpoints/
assignment_2/.idea/
.idea/
assignment_2/server/my_database.sqlite
26 changes: 2 additions & 24 deletions assignment_2/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,13 @@
</head>
<body>

<div class="container mt-5">
<form id="form">
<div class="form-group">
<label for="inputName">Name</label>
<input type="text" class="form-control" id="inputName" name="name"
placeholder="Enter name">
</div>
<div class="form-group">
<label for="inputSurname">Surname</label>
<input type="text" class="form-control" id="inputSurname" name="surname"
placeholder="Enter surname">
</div>
<div class="form-group">
<label for="inputAge">Age</label>
<input type="number" class="form-control" id="inputAge" name="age"
placeholder="Enter age">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>

<div class="container-fluid mt-5">
<table class="table table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Surname</th>
<th scope="col">Age</th>
<th scope="col">Title</th>
<th scope="col">Source</th>
</tr>
</thead>
<tbody id="table_body">
Expand Down
59 changes: 41 additions & 18 deletions assignment_2/server/app_db.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import json
import sqlite3
import requests

from flask import Flask, g, request
from flask import Flask, g
from flask_cors import CORS
from bs4 import BeautifulSoup

app = Flask(__name__)
CORS(app)

DATABASE = 'my_database.sqlite'
female_news = []


def get_db():
Expand All @@ -21,37 +24,55 @@ def init_db():
with app.app_context():
db = get_db()
cursor = db.cursor()
cursor.executescript("""DROP TABLE IF EXISTS FemaleNews""")
cursor.executescript(
"""CREATE TABLE IF NOT EXISTS Users
(id integer primary key, name text not null,
surname text not null, age integer)"""
"""CREATE TABLE IF NOT EXISTS FemaleNews
(id integer primary key,
title text not null,
source text not null)"""
)

db.commit()


def add_news(female_news):
wonderzine = requests.get('https://www.wonderzine.com/')
soup_wonderzine = BeautifulSoup(wonderzine.content, 'html.parser')
wonderzine_news = soup_wonderzine.find_all('div', {"class": "title"})
female_news += [(elem.a.text, 'Wonderzine') for elem in wonderzine_news]

cosmopolitan = requests.get('https://www.cosmo.ru/news/')
soup_cosmopolitan = BeautifulSoup(cosmopolitan.content, 'html.parser')
cosmopolitan_news = soup_cosmopolitan.find_all('div', {"class": "article-tile__title-wrapper"})
female_news += [(elem.h3.text, 'Cosmopolitan') for elem in cosmopolitan_news]

vogue = requests.get('https://www.vogue.ru/lifestyle')
soup_vogue = BeautifulSoup(vogue.content, 'html.parser')
vogue_news = soup_vogue.find_all('a', {"data-test-id": "Hed"})
female_news += [(elem.text, 'Vogue') for elem in vogue_news]

female_news.sort(key=lambda x: x[0])


def fill_bd():
with app.app_context():
db = get_db()
cursor = db.cursor()

cursor.executemany('INSERT INTO FemaleNews (title,source) VALUES(?,?)', female_news)

db.commit()

@app.route('/get_all')
def get_all():
db_cursor = get_db().cursor()
db_cursor.row_factory = sqlite3.Row
db_cursor.execute("SELECT * From Users")
db_cursor.execute("SELECT * From FemaleNews")
result = db_cursor.fetchall()
json_result = json.dumps([dict(row) for row in result])
return json_result


@app.route('/new_user', methods=['POST'])
def create_new_user():
user_json = request.get_json()
for key in ['name', 'surname', 'age']:
assert key in user_json, f'{key} not found in the request'
query = f"INSERT INTO Users (name, surname, age) VALUES ('{user_json['name']}', '{user_json['surname']}', {user_json['age']});"
db_conn = get_db()
db_conn.execute(query)
db_conn.commit()
db_conn.close()
return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}


@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
Expand All @@ -61,4 +82,6 @@ def close_connection(exception):

if __name__ == '__main__':
init_db()
add_news(female_news)
fill_bd()
app.run()