diff --git a/.gitignore b/.gitignore index f70cbc8..0fda79b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ __pycache__/ .ipynb_checkpoints/ +assignment_2/.idea/ .idea/ +assignment_2/server/my_database.sqlite \ No newline at end of file diff --git a/assignment_2/client/index.html b/assignment_2/client/index.html index 9090ebc..60f0e1d 100644 --- a/assignment_2/client/index.html +++ b/assignment_2/client/index.html @@ -8,35 +8,13 @@ -
-
-
- - -
-
- - -
-
- - -
- -
-
-
- - - + + diff --git a/assignment_2/server/app_db.py b/assignment_2/server/app_db.py index 607d409..152ad54 100644 --- a/assignment_2/server/app_db.py +++ b/assignment_2/server/app_db.py @@ -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(): @@ -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) @@ -61,4 +82,6 @@ def close_connection(exception): if __name__ == '__main__': init_db() + add_news(female_news) + fill_bd() app.run()
IdNameSurnameAgeTitleSource