def fixRepetedVowelTitle(page): """ If found impossible vowel arrangement in title, correct by moving that page. """ opagetitle = page.title() pagetitle = opagetitle for i in checkVowel: pagetitle = re2.sub(i + u"+", i, pagetitle) if pagetitle != opagetitle: pywikibot.output("ย้ายบทความชื่อมีสระซ้อน") reason = u"โรบอต: เปลี่ยนชื่อบทความมีสระซ้อน" try: page.move(pagetitle, reason=reason) except: preload.error() else: page = pywikibot.Page(pywikibot.getSite(), page.title()) page.delete(reason=reason, prompt=False, mark=True)
def edititem(datapage, summary=None, minorEdit=True, token=None, newToken=False, sysop=False, captcha=None, botflag=True, maxTries=-1, value={}, iscreate=False, isset=False): """ Just edit an item. """ retry_attempt = 0 retry_delay = 1 dblagged = False newPage = True params = { 'summary': datapage._encodeArg(summary, 'summary'), 'format': 'jsonfm', 'action': 'wbeditentity', 'data': json.dumps(value), } print json.dumps(value) if __debug__: raw_input("ok?... ") if token: params['token'] = token else: params['token'] = datapage.site().getToken(sysop = sysop) if config.maxlag: params['maxlag'] = str(config.maxlag) if botflag: params['bot'] = 1 if captcha: params['captchaid'] = captcha['id'] params['captchaword'] = captcha['answer'] if iscreate: params['createonly'] = 1 else: datapage.get() params['id'] = datapage.title() params['nocreate'] = 1 if isset: params['clear'] = 1 while True: if (maxTries == 0): raise pywikibot.MaxTriesExceededError() maxTries -= 1 # Check whether we are not too quickly after the previous # putPage, and wait a bit until the interval is acceptable if not dblagged: pywikibot.put_throttle() pywikibot.output(u'Editing page %s via API' % datapage._originTitle) try: response, data = query.GetData(params, datapage.site(), sysop=sysop, back_response=True) if isinstance(data, basestring): raise KeyError except httplib.BadStatusLine, line: raise pywikibot.PageNotSaved('Bad status line: %s' % line.line) except pywikibot.ServerError: pywikibot.output(u''.join(traceback.format_exception(*sys.exc_info()))) retry_attempt += 1 pywikibot.output(u'Got a server error when putting %s; will retry in %i minute%s.' % (datapage, retry_delay, retry_delay != 1 and "s" or "")) time.sleep(30) continue
pywikibot.output(u''.join(traceback.format_exception(*sys.exc_info()))) retry_attempt += 1 pywikibot.output(u'Got a server error when putting %s; will retry in %i minute%s.' % (datapage, retry_delay, retry_delay != 1 and "s" or "")) time.sleep(30) continue except ValueError: # API result cannot decode output(u"Server error encountered; will retry in %i minute%s." % (retry_delay, retry_delay != 1 and "s" or "")) time.sleep(30) continue # If it has gotten this far then we should reset dblagged dblagged = False # Check blocks datapage.site().checkBlocks(sysop = sysop) # A second text area means that an edit conflict has occured. if response.code == 500: pywikibot.output(u"Server error encountered; will retry in %i minute%s." % (retry_delay, retry_delay != 1 and "s" or "")) time.sleep(30) continue if 'error' in data: errorCode = data['error']['code'] pywikibot.output(u'Got an unknown error when putting data: %s' %errorCode) else: if unicode(data['success']) == u"1": print 302, response.msg, data['success'] return 302, response.msg, data['success'] print response.code, response.msg, data return response.code, response.msg, data
def change_category(article, oldCat, newCat, comment=None, sortKey=None, inPlace=False): """ Given an article which is in category oldCat, moves it to category newCat. Moves subcategories of oldCat as well. oldCat and newCat should be Category objects. If newCat is None, the category will be removed. """ cats = article.categories(get_redirect=True) site = article.site() changesMade = False if not article.canBeEdited(): pywikibot.output("Can't edit %s, skipping it..." % article.title(asLink=True)) return False if inPlace or article.namespace() == 10: oldtext = article.get(get_redirect=True) newtext = pywikibot.replaceCategoryInPlace(oldtext, oldCat, newCat) if newtext == oldtext: pywikibot.output(u"No changes in made in page %s." % article.title(asLink=True)) return False try: article.put(newtext, comment) return True except pywikibot.EditConflict: pywikibot.output(u"Skipping %s because of edit conflict" % article.title(asLink=True)) except pywikibot.LockedPage: pywikibot.output(u"Skipping locked page %s" % article.title(asLink=True)) except pywikibot.SpamfilterError, error: pywikibot.output( u"Changing page %s blocked by spam filter (URL=%s)" % (article.title(asLink=True), error.url) ) except pywikibot.NoUsername: pywikibot.output(u"Page %s not saved; sysop privileges required." % article.title(asLink=True))
return False try: article.put(newtext, comment) return True except pywikibot.EditConflict: pywikibot.output(u"Skipping %s because of edit conflict" % article.title(asLink=True)) except pywikibot.LockedPage: pywikibot.output(u"Skipping locked page %s" % article.title(asLink=True)) except pywikibot.SpamfilterError, error: pywikibot.output( u"Changing page %s blocked by spam filter (URL=%s)" % (article.title(asLink=True), error.url) ) except pywikibot.NoUsername: pywikibot.output(u"Page %s not saved; sysop privileges required." % article.title(asLink=True)) except pywikibot.PageNotSaved, error: pywikibot.output(u"Saving page %s failed: %s" % (article.title(asLink=True), error.message)) return False # This loop will replace all occurrences of the category to be changed, # and remove duplicates. newCatList = [] newCatSet = set() for i in range(len(cats)): cat = cats[i] if cat == oldCat: changesMade = True if not sortKey: sortKey = cat.sortKey if newCat: if newCat.title() not in newCatSet: newCategory = catlib.Category(site, newCat.title(), sortKey=sortKey)
def service(serviceTitle, operation, verifyFunc, datwiki, site, summary, debug=False): """ Get: Title of service"s page Key to read from config page, Function to verify user Config page, site Summary function. Function: Clear service"s page Return: Header of table List of rows Suspicious entry(/row) """ page = pywikibot.Page(site, serviceTitle) datwiki = pywikibot.Page(site, datwiki) lastrev = int(libinfo.getdat(key = operation, wikipage = datwiki)) oldcontent = page.get() header, table = libwikitable.wiki2table(oldcontent) disable = [False] * len(table) hist = page.getVersionHistory() # There is no need to get all revisions, just 500 is fine (by default). histlist = [] for version in hist: histlist.append((version, page.getOldVersion(version[0]))) if version[0] == lastrev: break hist = histlist hist.reverse() pywikibot.output(u"Processing %d revision(s)" % len(hist)) for i in xrange(len(hist) - 1): oldv = hist[i][1] newv = hist[i + 1][1] usernew = hist[i + 1][0][2] dummy, cold = libwikitable.wiki2table(oldv) dummy, cnew = libwikitable.wiki2table(newv) oldvc = set([preload.enunicode(x) for x in cold]) newvc = set([preload.enunicode(x) for x in cnew]) difference = [eval(x) for x in (newvc - oldvc)] if not verifyFunc(usernew): for entry in difference: for cnt, fentry in enumerate(table): if entry == fentry: disable[cnt] = True break newcontent = re2.sub(ur"(?ms)^(\!.*?$\n).*?(^\|\})", ur"\1\2", oldcontent) if oldcontent != newcontent: if not debug: page = pywikibot.Page(site, page.title()) page.put(newcontent, summary()) print page.getVersionHistory()[0][0] libinfo.putdat(key=operation, value=page.getVersionHistory()[0][0], wikipage=datwiki) return header, table, disable