I think your best bet is the data explorer plugin and Run Data Explorer queries with the Discourse API