-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
57 lines (47 loc) · 2.02 KB
/
database.py
File metadata and controls
57 lines (47 loc) · 2.02 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
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase
class Base(DeclarativeBase):
pass
db = SQLAlchemy(model_class=Base)
def init_app(app):
"""Initialize the database with the app context"""
# Configure the SQLAlchemy part of the app instance
app.config["SQLALCHEMY_DATABASE_URI"] = app.config.get("SQLALCHEMY_DATABASE_URI")
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {
"pool_recycle": 300,
"pool_pre_ping": True,
}
# Initialize SQLAlchemy with the app
db.init_app(app)
with app.app_context():
# Import models here to avoid circular imports
import models # noqa: F401
# Create all tables
db.create_all()
def sync_article_counts():
"""Sync article counts and scores for all news sources"""
from models import Article, NewsSourceMetrics
try:
default_scores = {
'CoinDesk': {'trust': 82.5, 'accuracy': 86.0},
'Cointelegraph': {'trust': 74.0, 'accuracy': 77.0},
'The Block': {'trust': 88.0, 'accuracy': 91.0},
'Messari': {'trust': 85.0, 'accuracy': 88.0},
'Decrypt': {'trust': 77.0, 'accuracy': 81.0},
'BeInCrypto': {'trust': 68.0, 'accuracy': 72.0},
'CryptoSlate': {'trust': 71.0, 'accuracy': 75.0},
'Bitcoin.com': {'trust': 65.0, 'accuracy': 69.0},
'NewsBTC': {'trust': 63.0, 'accuracy': 67.0}
}
sources = NewsSourceMetrics.query.all()
for source in sources:
count = Article.query.filter_by(source_name=source.source_name).count()
source.article_count = count
scores = default_scores.get(source.source_name, {'trust': 60.0, 'accuracy': 65.0})
source.trust_score = scores['trust']
source.accuracy_score = scores['accuracy']
db.session.commit()
except Exception as e:
import logging
logging.error(f"Error syncing article counts and scores: {str(e)}")
db.session.rollback()