Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

102 líneas
4.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2017 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import time
  23. from earwigbot.tasks import Task
  24. class BannerUntag(Task):
  25. """A task to undo mistaken tagging edits made by wikiproject_tagger."""
  26. name = "banner_untag"
  27. number = 14
  28. def run(self, **kwargs):
  29. self.site = self.bot.wiki.get_site()
  30. self.summary = kwargs["summary"]
  31. self.throttle = int(kwargs.get("throttle", 0))
  32. rev_file = kwargs["rev-file"]
  33. done_file = kwargs["done-file"]
  34. error_file = kwargs["error-file"]
  35. with open(rev_file) as fp:
  36. data = fp.read().splitlines()
  37. with open(done_file) as donefp:
  38. done = [int(line) for line in donefp.read().splitlines()]
  39. with open(error_file, "a") as errfp:
  40. with open(done_file, "a") as donefp:
  41. self._process_data(data, done, errfp, donefp)
  42. def _process_data(self, data, done, errfile, donefile):
  43. chunksize = 50
  44. for chunkidx in range((len(data) + chunksize - 1) / chunksize):
  45. chunk = data[chunkidx*chunksize:(chunkidx+1)*chunksize]
  46. chunk = [[int(x) for x in line.split("\t")] for line in chunk]
  47. if self.shutoff_enabled():
  48. return
  49. self._process_chunk(chunk, done, errfile, donefile)
  50. def _process_chunk(self, chunk, done, errfile, donefile):
  51. pageids_to_revids = dict(chunk)
  52. res = self.site.api_query(
  53. action="query", prop="revisions", rvprop="ids",
  54. pageids="|".join(str(item[0]) for item in chunk), formatversion=2)
  55. stage2 = []
  56. for pagedata in res["query"]["pages"]:
  57. pageid = pagedata["pageid"]
  58. title = pagedata["title"]
  59. revid = pagedata["revisions"][0]["revid"]
  60. parentid = pagedata["revisions"][0]["parentid"]
  61. if pageids_to_revids[pageid] == revid:
  62. stage2.append(str(parentid))
  63. else:
  64. self.logger.info(u"Skipping [[%s]], not latest edit" % title)
  65. done.append(pageid)
  66. donefile.write("%d\n" % pageid)
  67. errfile.write(u"%s\n" % title)
  68. if not stage2:
  69. return
  70. res2 = self.site.api_query(
  71. action="query", prop="revisions", rvprop="content",
  72. revids="|".join(stage2), formatversion=2)
  73. for pagedata in res2["query"]["pages"]:
  74. if pagedata["revisions"][0]["contentmodel"] != "wikitext":
  75. continue
  76. pageid = pagedata["pageid"]
  77. title = pagedata["title"]
  78. content = pagedata["revisions"][0]["content"]
  79. self.logger.debug(u"Reverting one edit on [[%s]]" % title)
  80. page = self.site.get_page(title)
  81. page.edit(content, self.summary, minor=True, bot=True)
  82. done.append(pageid)
  83. donefile.write("%d\n" % pageid)
  84. if self.throttle:
  85. time.sleep(self.throttle)