-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
81 lines (56 loc) · 2.04 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from flask import Flask
from flask import request
from flask_sqlalchemy import SQLAlchemy
from util.email_sender import send_email
from util.news import crawl_news, summarize
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///news.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class News(db.Model):
__tablename__ = 'news'
id = db.Column('id', db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.Text, nullable=False)
link = db.Column(db.Text, nullable=False, unique=True)
description = db.Column(db.Text, nullable=False)
contents = db.Column(db.Text, nullable=False)
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())
updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(),
onupdate=db.func.current_timestamp())
def __init__(self, title, link, description, contents):
self.title = title
self.link = link
self.contents = contents
self.description = description
@app.route('/', methods=['POST'])
def crawl_news_save():
query = request.form['query']
news = crawl_news(query, start=1, offset=2)
news_records = []
for n in news:
news_records.append(News(
title=n['title'],
contents=n['contents'],
link=n['link'],
description=n['description']
))
db.session.add_all(news_records)
db.session.commit()
return 'ok'
@app.route('/subscribe', methods=['GET'])
def subscribe():
q = request.args.get('query')
news = crawl_news(q, start=1, offset=2)
top5_latest_news = news[:5]
for n in top5_latest_news:
contents = n['contents']
summarized_contents = summarize(contents)
send_email(
subject=n['title'],
from_email='[email protected]',
to_email='[email protected]',
basic_text=summarized_contents
)
if __name__ == '__main__':
db.create_all()
app.run(debug=True)