Skip to content

Commit

Permalink
Weight episodes and listening time to define top podcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
leandroalonso committed Oct 31, 2023
1 parent 0c286c8 commit 844bd08
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,16 @@ class EndOfYearDataManager {
dbQueue.inDatabase { db in
do {
let query = """
SELECT DISTINCT \(DataManager.episodeTableName).uuid,
SELECT (totalPlayedTime * 0.4) * (played_episodes * 0.6) as weighted, * FROM
(SELECT DISTINCT \(DataManager.episodeTableName).uuid as episodeUuid,
SUM(playedUpTo) as totalPlayedTime,
COUNT(\(DataManager.episodeTableName).id) as played_episodes,
\(DataManager.podcastTableName).*
FROM \(DataManager.episodeTableName), \(DataManager.podcastTableName)
WHERE `\(DataManager.podcastTableName)`.uuid = `\(DataManager.episodeTableName)`.podcastUuid and
\(listenedEpisodesThisYear)
GROUP BY podcastUuid
ORDER BY totalPlayedTime DESC
GROUP BY podcastUuid)
ORDER BY weighted DESC
LIMIT \(limit)
"""
let resultSet = try db.executeQuery(query, values: nil)
Expand All @@ -243,12 +244,14 @@ class EndOfYearDataManager {
}

// If there's a tie on total played time, check number of played episodes
return allPodcasts.sorted(by: {
if $0.totalPlayedTime == $1.totalPlayedTime {
return $0.numberOfPlayedEpisodes > $1.numberOfPlayedEpisodes
}
return $0.totalPlayedTime > $1.totalPlayedTime
})
// return allPodcasts.sorted(by: {
// if $0.totalPlayedTime == $1.totalPlayedTime {
// return $0.numberOfPlayedEpisodes > $1.numberOfPlayedEpisodes
// }
// return $0.totalPlayedTime > $1.totalPlayedTime
// })

return allPodcasts
}

/// Return the longest listened episode
Expand Down

0 comments on commit 844bd08

Please sign in to comment.