I was repeating myself a lot for this use case, because each one needed a different URL and SQL query, however they were kind of structurally similar and could be refactored into common functions.