From c7685f2dda70bb260919a15ac79e89ab04cdfee1 Mon Sep 17 00:00:00 2001 From: Alan Date: Mon, 16 Mar 2020 07:46:11 +0300 Subject: [PATCH 1/2] News from female sites Wonderzine, Cosmopolitan, Vogue... All what u need --- .gitignore | 5 +++- assignment_2/client/index.html | 26 ++-------------- assignment_2/server/app_db.py | 55 +++++++++++++++++++++++----------- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index cc715cc..23facde 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ __pycache__/ -.ipynb_checkpoints/ \ No newline at end of file +.ipynb_checkpoints/ +assignment_2/.idea/ + +assignment_2/server/my_database.sqlite 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..acdb341 100644 --- a/assignment_2/server/app_db.py +++ b/assignment_2/server/app_db.py @@ -1,8 +1,10 @@ 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) @@ -22,10 +24,40 @@ def init_db(): db = get_db() cursor = db.cursor() 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)""" ) + + femaleNews = [] + + wonderzine = requests.get('https://www.wonderzine.com/') + soup_wonderzine = BeautifulSoup(wonderzine.content, 'html.parser') + wonderzine_news = soup_wonderzine.find_all('div', {"class": "title"}) + for elem in wonderzine_news: + femaleNews.append((elem.a.text, 'Wonderzine')) + + + cosmopolitan = requests.get('https://www.cosmo.ru/news/') + soup_cosmopolitan = BeautifulSoup(cosmopolitan.content, 'html.parser') + cosmopolitan_news = soup_cosmopolitan.find_all('a', {"class": "news-section-link"}) + for elem in cosmopolitan_news: + femaleNews.append((elem.h3.text, 'Cosmopolitan')) + + + vogue = requests.get('https://www.vogue.ru/lifestyle') + soup_vogue = BeautifulSoup(vogue.content, 'html.parser') + vogue_news = soup_vogue.find_all('h3', {"data-test-id": "Hed"}) + for elem in vogue_news: + femaleNews.append((elem.text, 'Vogue')) + + femaleNews.sort(key=lambda x: x[0]) + + for elem in femaleNews: + print(elem[0]) + cursor.execute("""INSERT INTO FemaleNews (title,source) VALUES(?,?)""", (elem[0], elem[1])) + db.commit() @@ -33,25 +65,12 @@ def init_db(): 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) From 61513cdd45a6ce87701d01a0bb2c1385f6c57c1a Mon Sep 17 00:00:00 2001 From: Alan Date: Mon, 16 Nov 2020 06:12:29 +0300 Subject: [PATCH 2/2] Codestyle fixes --- assignment_2/server/app_db.py | 46 +++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/assignment_2/server/app_db.py b/assignment_2/server/app_db.py index acdb341..152ad54 100644 --- a/assignment_2/server/app_db.py +++ b/assignment_2/server/app_db.py @@ -10,6 +10,7 @@ CORS(app) DATABASE = 'my_database.sqlite' +female_news = [] def get_db(): @@ -23,6 +24,7 @@ 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 FemaleNews (id integer primary key, @@ -30,37 +32,37 @@ def init_db(): source text not null)""" ) - femaleNews = [] + db.commit() + - wonderzine = requests.get('https://www.wonderzine.com/') - soup_wonderzine = BeautifulSoup(wonderzine.content, 'html.parser') - wonderzine_news = soup_wonderzine.find_all('div', {"class": "title"}) - for elem in wonderzine_news: - femaleNews.append((elem.a.text, 'Wonderzine')) +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] - cosmopolitan = requests.get('https://www.cosmo.ru/news/') - soup_cosmopolitan = BeautifulSoup(cosmopolitan.content, 'html.parser') - cosmopolitan_news = soup_cosmopolitan.find_all('a', {"class": "news-section-link"}) - for elem in cosmopolitan_news: - femaleNews.append((elem.h3.text, 'Cosmopolitan')) + 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]) - vogue = requests.get('https://www.vogue.ru/lifestyle') - soup_vogue = BeautifulSoup(vogue.content, 'html.parser') - vogue_news = soup_vogue.find_all('h3', {"data-test-id": "Hed"}) - for elem in vogue_news: - femaleNews.append((elem.text, 'Vogue')) - femaleNews.sort(key=lambda x: x[0]) +def fill_bd(): + with app.app_context(): + db = get_db() + cursor = db.cursor() - for elem in femaleNews: - print(elem[0]) - cursor.execute("""INSERT INTO FemaleNews (title,source) VALUES(?,?)""", (elem[0], elem[1])) + cursor.executemany('INSERT INTO FemaleNews (title,source) VALUES(?,?)', female_news) db.commit() - @app.route('/get_all') def get_all(): db_cursor = get_db().cursor() @@ -80,4 +82,6 @@ def close_connection(exception): if __name__ == '__main__': init_db() + add_news(female_news) + fill_bd() app.run()
IdNameSurnameAgeTitleSource