Refactor: Extract common broadcast history logic into shared function
This commit is contained in:
parent
a07bb67a33
commit
916e5f8610
62
storage.go
62
storage.go
@ -217,8 +217,9 @@ func loadArticles() map[string]Article {
|
|||||||
return articles
|
return articles
|
||||||
}
|
}
|
||||||
|
|
||||||
// getBroadcastHistory gets the most recent broadcast articles
|
// getBroadcastArticles is a common function for retrieving broadcast articles
|
||||||
func getBroadcastHistory(limit int) ([]Article, error) {
|
// with consistent filtering criteria
|
||||||
|
func getBroadcastArticles(limit int) ([]Article, error) {
|
||||||
rows, err := db.Query(`
|
rows, err := db.Query(`
|
||||||
SELECT link, title, description, published, originalDate, source, firstseen, seen, summary, importance, id, broadcastTime
|
SELECT link, title, description, published, originalDate, source, firstseen, seen, summary, importance, id, broadcastTime
|
||||||
FROM articles
|
FROM articles
|
||||||
@ -271,6 +272,11 @@ func getBroadcastHistory(limit int) ([]Article, error) {
|
|||||||
return articles, nil
|
return articles, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getBroadcastHistory gets the most recent broadcast articles
|
||||||
|
func getBroadcastHistory(limit int) ([]Article, error) {
|
||||||
|
return getBroadcastArticles(limit)
|
||||||
|
}
|
||||||
|
|
||||||
// getNextUpArticles gets the top 25 articles eligible for broadcast sorted by importance
|
// getNextUpArticles gets the top 25 articles eligible for broadcast sorted by importance
|
||||||
func getNextUpArticles() ([]Article, error) {
|
func getNextUpArticles() ([]Article, error) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
@ -328,62 +334,14 @@ func getNextUpArticles() ([]Article, error) {
|
|||||||
|
|
||||||
// getRecentBroadcasts retrieves the n most recently broadcast articles
|
// getRecentBroadcasts retrieves the n most recently broadcast articles
|
||||||
func getRecentBroadcasts(n int) []Article {
|
func getRecentBroadcasts(n int) []Article {
|
||||||
rows, err := db.Query(`
|
articles, err := getBroadcastArticles(n)
|
||||||
SELECT link, title, description, published, originalDate, source, firstseen, seen, summary, importance, id, broadcastTime
|
|
||||||
FROM articles
|
|
||||||
WHERE broadcastTime IS NOT NULL
|
|
||||||
AND broadcastTime > 1
|
|
||||||
AND broadcastTime != 0
|
|
||||||
AND datetime(broadcastTime) != '1970-01-01 00:00:00'
|
|
||||||
AND datetime(broadcastTime) != '0001-01-01 00:00:00'
|
|
||||||
AND strftime('%Y', broadcastTime) > '2000' -- Ensure year is at least 2000
|
|
||||||
ORDER BY broadcastTime DESC
|
|
||||||
LIMIT ?
|
|
||||||
`, n)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logInfo("db", "Error retrieving recent broadcasts", map[string]interface{}{
|
logInfo("db", "Error retrieving recent broadcasts", map[string]interface{}{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
})
|
})
|
||||||
return []Article{}
|
return []Article{}
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
return articles
|
||||||
|
|
||||||
var broadcasts []Article
|
|
||||||
for rows.Next() {
|
|
||||||
var a Article
|
|
||||||
var seen sql.NullTime
|
|
||||||
var broadcastTime sql.NullTime
|
|
||||||
var originalDate sql.NullTime
|
|
||||||
|
|
||||||
err := rows.Scan(
|
|
||||||
&a.Link, &a.Title, &a.Description, &a.Published, &originalDate, &a.Source,
|
|
||||||
&a.FirstSeen, &seen, &a.Summary, &a.Importance, &a.ID, &broadcastTime,
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
logInfo("db", "Error scanning broadcast article", map[string]interface{}{
|
|
||||||
"error": err.Error(),
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if seen.Valid {
|
|
||||||
a.Seen = seen.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
if broadcastTime.Valid {
|
|
||||||
a.BroadcastTime = broadcastTime.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
if originalDate.Valid {
|
|
||||||
a.OriginalDate = originalDate.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
broadcasts = append(broadcasts, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
return broadcasts
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupLogging() {
|
func setupLogging() {
|
||||||
|
Loading…
Reference in New Issue
Block a user