-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
104 lines (89 loc) · 2.94 KB
/
Copy pathdatabase.py
File metadata and controls
104 lines (89 loc) · 2.94 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from sqlalchemy import create_engine, Column, Integer, String, Float, Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import os
Base = declarative_base()
class Article(Base):
__tablename__ = 'articles'
id = Column(Integer, primary_key=True)
url = Column(String(500), unique=True)
title = Column(String(500))
content = Column(Text)
source = Column(String(100))
sentiment = Column(Float)
subjectivity = Column(Float)
bias = Column(String(50))
bias_score = Column(Float)
def init_db():
engine = create_engine('sqlite:///news_dashboard.db')
Base.metadata.create_all(engine)
return engine
def save_article(article_data):
engine = init_db()
Session = sessionmaker(bind=engine)
session = Session()
article = Article(
url=article_data["url"],
title=article_data["title"],
content=article_data["content"],
source=article_data["source"],
sentiment=article_data["sentiment"],
subjectivity=article_data["subjectivity"],
bias=article_data["bias"],
bias_score=article_data["bias_score"]
)
try:
session.add(article)
session.commit()
except:
session.rollback()
raise
finally:
session.close()
def get_articles():
engine = init_db()
Session = sessionmaker(bind=engine)
session = Session()
articles = session.query(Article).all()
session.close()
return [{
"id": a.id,
"url": a.url,
"title": a.title,
"content": a.content,
"source": a.source,
"sentiment": a.sentiment,
"subjectivity": a.subjectivity,
"bias": a.bias,
"bias_score": a.bias_score
} for a in articles]
def get_comparable_articles():
engine = init_db()
Session = sessionmaker(bind=engine)
session = Session()
# Get articles with similar titles (potential same events)
articles = session.query(Article).all()
session.close()
# Simple grouping by first few words of title
title_groups = {}
for a in articles:
key = ' '.join(a.title.split()[:5]).lower()
if key not in title_groups:
title_groups[key] = []
title_groups[key].append({
"id": a.id,
"url": a.url,
"title": a.title,
"content": a.content,
"source": a.source,
"sentiment": a.sentiment,
"subjectivity": a.subjectivity,
"bias": a.bias,
"bias_score": a.bias_score
})
# Only return groups with at least 2 articles from different sources
comparable = []
for group in title_groups.values():
if len(group) >= 2 and len(set(a["source"] for a in group)) >= 2:
comparable.extend(group)
return comparable