Roy, thank you! This is great!
My version is https://quarry.wmcloud.org/query/68559
Next step is to try to use it with Pywikibot as Xqt said.