def categorydown(listacategory): wikipedia.config.put_throttle = 0 wikipedia.put_throttle.setDelay() count=1 for catname in listacategory: count+=1 if count==200: break gencat = pagegenerators.SubCategoriesPageGenerator(catname, recurse=False) for subcat in gencat: try: wikipedia.output(subcat) except: wikipedia.output(str(subcat)) if subcat in listacategory: continue else: listacategory.append(subcat) return listacategory
class CategoryMoveRobot: """Robot to move pages from one category to another.""" def __init__(self, oldCatTitle, newCatTitle, batchMode=False, editSummary='', inPlace=False, moveCatPage=True, deleteEmptySourceCat=True, titleRegex=None, useSummaryForDeletion=True): site = pywikibot.getSite() self.editSummary = editSummary self.oldCat = catlib.Category(site, oldCatTitle) self.newCatTitle = newCatTitle self.inPlace = inPlace self.moveCatPage = moveCatPage self.batchMode = batchMode self.deleteEmptySourceCat = deleteEmptySourceCat self.titleRegex = titleRegex self.useSummaryForDeletion = useSummaryForDeletion def run(self): site = pywikibot.getSite() newCat = catlib.Category(site, self.newCatTitle) # set edit summary message if not self.editSummary: self.editSummary = i18n.twtranslate(site, 'category-changing') \ % {'oldcat':self.oldCat.title(), 'newcat':newCat.title()} if self.useSummaryForDeletion and self.editSummary: reason = self.editSummary else: reason = i18n.twtranslate(site, deletion_reason_move) \ % {'newcat': self.newCatTitle, 'title': self.newCatTitle} # Copy the category contents to the new category page copied = False oldMovedTalk = None if self.oldCat.exists() and self.moveCatPage: copied = self.oldCat.copyAndKeep( self.newCatTitle, pywikibot.translate(site, cfd_templates)) # Also move the talk page if copied: oldTalk = self.oldCat.toggleTalkPage() if oldTalk.exists(): newTalkTitle = newCat.toggleTalkPage().title() try: talkMoved = oldTalk.move(newTalkTitle, reason) except (pywikibot.NoPage, pywikibot.PageNotSaved), e: #in order : #Source talk does not exist, or #Target talk already exists pywikibot.output(e.message) else: if talkMoved: oldMovedTalk = oldTalk # Move articles gen = pagegenerators.CategorizedPageGenerator(self.oldCat, recurse=False) preloadingGen = pagegenerators.PreloadingGenerator(gen) for article in preloadingGen: if not self.titleRegex or re.search(self.titleRegex, article.title()): catlib.change_category(article, self.oldCat, newCat, comment=self.editSummary, inPlace=self.inPlace) # Move subcategories gen = pagegenerators.SubCategoriesPageGenerator(self.oldCat, recurse=False) preloadingGen = pagegenerators.PreloadingGenerator(gen) for subcategory in preloadingGen: if not self.titleRegex or re.search(self.titleRegex, subcategory.title()): catlib.change_category(subcategory, self.oldCat, newCat, comment=self.editSummary, inPlace=self.inPlace) # Delete the old category and its moved talk page if copied and self.deleteEmptySourceCat == True: if self.oldCat.isEmptyCategory(): confirm = not self.batchMode self.oldCat.delete(reason, confirm, mark=True) if oldMovedTalk is not None: oldMovedTalk.delete(reason, confirm, mark=True) else: pywikibot.output('Couldn\'t delete %s - not empty.' % self.oldCat.title())
class CategoryMoveRobot(object): """Bot to move pages from one category to another.""" def __init__(self, oldCatTitle, newCatTitle, batchMode=False, editSummary='', inPlace=False, moveCatPage=True, deleteEmptySourceCat=True, titleRegex=None, useSummaryForDeletion=True, withHistory=False): site = pywikibot.getSite() self.editSummary = editSummary self.oldCat = catlib.Category(site, oldCatTitle) self.newCatTitle = newCatTitle self.inPlace = inPlace self.moveCatPage = moveCatPage self.batchMode = batchMode self.deleteEmptySourceCat = deleteEmptySourceCat self.titleRegex = titleRegex self.useSummaryForDeletion = useSummaryForDeletion self.withHistory = withHistory def run(self): """The main bot function that does all the work.""" site = pywikibot.getSite() newCat = catlib.Category(site, self.newCatTitle) # set edit summary message if self.useSummaryForDeletion and self.editSummary: reason = self.editSummary else: reason = i18n.twtranslate(site, 'category-was-moved', { 'newcat': self.newCatTitle, 'title': self.newCatTitle }) if not self.editSummary: self.editSummary = i18n.twtranslate(site, 'category-changing', { 'oldcat': self.oldCat.title(), 'newcat': newCat.title() }) # Copy the category contents to the new category page copied = False oldMovedTalk = None if (site.isAllowed('move-categorypages') and self.oldCat.exists() and self.moveCatPage): self.oldCat.move(newCat.title(), reason=self.editSummary, movetalkpage=True, leaveRedirect=not self.deleteEmptySourceCat) copied = True elif self.oldCat.exists() and self.moveCatPage: copied = self.oldCat.copyAndKeep( newCat.title(), pywikibot.translate(site, cfd_templates)) # Also move the talk page if copied: oldTalk = self.oldCat.toggleTalkPage() if oldTalk.exists(): newTalkTitle = newCat.toggleTalkPage().title() try: talkMoved = oldTalk.move(newTalkTitle, reason) except (pywikibot.NoPage, pywikibot.PageNotSaved), e: #in order : #Source talk does not exist, or #Target talk already exists pywikibot.output(e.message) else: if talkMoved: oldMovedTalk = oldTalk if self.withHistory: # Whether or not there was an old talk page, we write # the page history to the new talk page history = self.oldCat.getVersionHistoryTable() # Set the section title for the old cat's history on the new # cat's talk page. sectionTitle = i18n.twtranslate( site, 'category-section-title', {'oldcat': self.oldCat.title()}) #Should be OK, we are within if self.oldCat.exists() historySection = u'\n== %s ==\n%s' % (sectionTitle, history) try: text = newCat.toggleTalkPage().get() + historySection except pywikibot.NoPage: text = historySection try: newCat.toggleTalkPage().put( text, i18n.twtranslate(site, 'category-version-history') % {'oldcat': self.oldCat.title()}) except: pywikibot.output( 'History of the category has not been saved to new ' 'talk page') #TODO: some nicer exception handling (not too important) # first move the page, than tagg the vh # Move articles gen = pagegenerators.CategorizedPageGenerator(self.oldCat, recurse=False) preloadingGen = pagegenerators.PreloadingGenerator(gen) for article in preloadingGen: if not self.titleRegex or re.search(self.titleRegex, article.title()): catlib.change_category(article, self.oldCat, newCat, comment=self.editSummary, inPlace=self.inPlace) # Move subcategories gen = pagegenerators.SubCategoriesPageGenerator(self.oldCat, recurse=False) preloadingGen = pagegenerators.PreloadingGenerator(gen) for subcategory in preloadingGen: if not self.titleRegex or re.search(self.titleRegex, subcategory.title()): catlib.change_category(subcategory, self.oldCat, newCat, comment=self.editSummary, inPlace=self.inPlace) # Delete the old category and its moved talk page if copied and self.deleteEmptySourceCat: if self.oldCat.isEmptyCategory(): confirm = not self.batchMode self.oldCat.delete(reason, confirm, mark=True) if oldMovedTalk is not None: oldMovedTalk.delete(reason, confirm, mark=True) else: pywikibot.output('Couldn\'t delete %s - not empty.' % self.oldCat.title())