A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

230 рядки
10 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 Ben Kurtovic <ben.kurtovic@verizon.net>
  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. from gzip import GzipFile
  23. from socket import timeout
  24. from StringIO import StringIO
  25. from time import sleep, time
  26. from urllib2 import build_opener, URLError
  27. import oauth2 as oauth
  28. from earwigbot import exceptions
  29. from earwigbot.wiki.copyvios.markov import MarkovChain, MarkovChainIntersection
  30. from earwigbot.wiki.copyvios.parsers import ArticleTextParser, HTMLTextParser
  31. from earwigbot.wiki.copyvios.result import CopyvioCheckResult
  32. from earwigbot.wiki.copyvios.search import YahooBOSSSearchEngine
  33. __all__ = ["CopyvioMixIn"]
  34. class CopyvioMixIn(object):
  35. """
  36. **EarwigBot: Wiki Toolset: Copyright Violation MixIn**
  37. This is a mixin that provides two public methods, :py:meth:`copyvio_check`
  38. and :py:meth:`copyvio_compare`. The former checks the page for copyright
  39. violations using a search engine API, and the latter compares the page
  40. against a given URL. Credentials for the search engine API are stored in
  41. the :py:class:`~earwigbot.wiki.site.Site`'s config.
  42. """
  43. def __init__(self, site):
  44. self._search_config = site._search_config
  45. self._exclusions_db = self._search_config.get("exclusions_db")
  46. self._opener = build_opener()
  47. self._opener.addheaders = site._opener.addheaders
  48. def _open_url_ignoring_errors(self, url):
  49. """Open a URL using self._opener and return its content, or None.
  50. Will decompress the content if the headers contain "gzip" as its
  51. content encoding, and will return None if URLError is raised while
  52. opening the URL. IOErrors while gunzipping a compressed response are
  53. ignored, and the original content is returned.
  54. """
  55. try:
  56. response = self._opener.open(url.encode("utf8"), timeout=5)
  57. except (URLError, timeout):
  58. return None
  59. result = response.read()
  60. if response.headers.get("Content-Encoding") == "gzip":
  61. stream = StringIO(result)
  62. gzipper = GzipFile(fileobj=stream)
  63. try:
  64. result = gzipper.read()
  65. except IOError:
  66. pass
  67. return result
  68. def _select_search_engine(self):
  69. """Return a function that can be called to do web searches.
  70. The function takes one argument, a search query, and returns a list of
  71. URLs, ranked by importance. The underlying logic depends on the
  72. *engine* argument within our config; for example, if *engine* is
  73. "Yahoo! BOSS", we'll use YahooBOSSSearchEngine for querying.
  74. Raises UnknownSearchEngineError if the 'engine' listed in our config is
  75. unknown to us, and UnsupportedSearchEngineError if we are missing a
  76. required package or module, like oauth2 for "Yahoo! BOSS".
  77. """
  78. engine = self._search_config["engine"]
  79. credentials = self._search_config["credentials"]
  80. if engine == "Yahoo! BOSS":
  81. if not oauth:
  82. e = "The package 'oauth2' could not be imported"
  83. raise exceptions.UnsupportedSearchEngineError(e)
  84. return YahooBOSSSearchEngine(credentials)
  85. raise exceptions.UnknownSearchEngineError(engine)
  86. def _copyvio_compare_content(self, article, url):
  87. """Return a number comparing an article and a URL.
  88. The *article* is a Markov chain, whereas the *url* is just a string
  89. that we'll try to open and read ourselves.
  90. """
  91. html = self._open_url_ignoring_errors(url)
  92. if not html:
  93. return 0
  94. source = MarkovChain(HTMLTextParser(html).strip())
  95. delta = MarkovChainIntersection(article, source)
  96. return float(delta.size()) / article.size(), (source, delta)
  97. def copyvio_check(self, min_confidence=0.5, max_queries=-1,
  98. interquery_sleep=1):
  99. """Check the page for copyright violations.
  100. Returns a
  101. :py:class:`~earwigbot.wiki.copyvios.result.CopyvioCheckResult` object
  102. with information on the results of the check.
  103. *max_queries* is self-explanatory; we will never make more than this
  104. number of queries in a given check. If it's lower than 0, we will not
  105. limit the number of queries.
  106. *interquery_sleep* is the minimum amount of time we will sleep between
  107. search engine queries, in seconds.
  108. Raises :py:exc:`~earwigbot.exceptions.CopyvioCheckError` or subclasses
  109. (:py:exc:`~earwigbot.exceptions.UnknownSearchEngineError`,
  110. :py:exc:`~earwigbot.exceptions.SearchQueryError`, ...) on errors.
  111. """
  112. searcher = self._select_search_engine()
  113. if self._exclusions_db:
  114. self._exclusions_db.sync(self.site.name)
  115. handled_urls = []
  116. best_confidence = 0
  117. best_match = None
  118. num_queries = 0
  119. empty = MarkovChain("")
  120. best_chains = (empty, MarkovChainIntersection(empty, empty))
  121. parser = ArticleTextParser(self.get())
  122. clean = parser.strip()
  123. chunks = parser.chunk(self._search_config["nltk_dir"], max_queries)
  124. article_chain = MarkovChain(clean)
  125. last_query = time()
  126. if article_chain.size() < 20: # Auto-fail very small articles
  127. return CopyvioCheckResult(False, best_confidence, best_match,
  128. num_queries, article_chain, best_chains)
  129. while (chunks and best_confidence < min_confidence and
  130. (max_queries < 0 or num_queries < max_queries)):
  131. chunk = chunks.pop(0)
  132. log = u"[[{0}]] -> querying {1} for {2!r}"
  133. self._logger.debug(log.format(self.title, searcher.name, chunk))
  134. urls = searcher.search(chunk)
  135. urls = [url for url in urls if url not in handled_urls]
  136. for url in urls:
  137. handled_urls.append(url)
  138. if self._exclusions_db:
  139. if self._exclusions_db.check(self.site.name, url):
  140. continue
  141. conf, chains = self._copyvio_compare_content(article_chain, url)
  142. if conf > best_confidence:
  143. best_confidence = conf
  144. best_match = url
  145. best_chains = chains
  146. num_queries += 1
  147. diff = time() - last_query
  148. if diff < interquery_sleep:
  149. sleep(interquery_sleep - diff)
  150. last_query = time()
  151. if best_confidence >= min_confidence:
  152. is_violation = True
  153. log = u"Violation detected for [[{0}]] (confidence: {1}; URL: {2}; using {3} queries)"
  154. self._logger.debug(log.format(self.title, best_confidence,
  155. best_match, num_queries))
  156. else:
  157. is_violation = False
  158. log = u"No violation for [[{0}]] (confidence: {1}; using {2} queries)"
  159. self._logger.debug(log.format(self.title, best_confidence,
  160. num_queries))
  161. return CopyvioCheckResult(is_violation, best_confidence, best_match,
  162. num_queries, article_chain, best_chains)
  163. def copyvio_compare(self, url, min_confidence=0.5):
  164. """Check the page like :py:meth:`copyvio_check` against a specific URL.
  165. This is essentially a reduced version of the above - a copyivo
  166. comparison is made using Markov chains and the result is returned in a
  167. :py:class:`~earwigbot.wiki.copyvios.result.CopyvioCheckResult` object -
  168. but without using a search engine, since the suspected "violated" URL
  169. is supplied from the start.
  170. Its primary use is to generate a result when the URL is retrieved from
  171. a cache, like the one used in EarwigBot's Toolserver site. After a
  172. search is done, the resulting URL is stored in a cache for 24 hours so
  173. future checks against that page will not require another set of
  174. time-and-money-consuming search engine queries. However, the comparison
  175. itself (which includes the article's and the source's content) cannot
  176. be stored for data retention reasons, so a fresh comparison is made
  177. using this function.
  178. Since no searching is done, neither
  179. :py:exc:`~earwigbot.exceptions.UnknownSearchEngineError` nor
  180. :py:exc:`~earwigbot.exceptions.SearchQueryError` will be raised.
  181. """
  182. content = self.get()
  183. clean = ArticleTextParser(content).strip()
  184. article_chain = MarkovChain(clean)
  185. confidence, chains = self._copyvio_compare_content(article_chain, url)
  186. if confidence >= min_confidence:
  187. is_violation = True
  188. log = u"Violation detected for [[{0}]] (confidence: {1}; URL: {2})"
  189. self._logger.debug(log.format(self.title, confidence, url))
  190. else:
  191. is_violation = False
  192. log = u"No violation for [[{0}]] (confidence: {1}; URL: {2})"
  193. self._logger.debug(log.format(self.title, confidence, url))
  194. return CopyvioCheckResult(is_violation, confidence, url, 0,
  195. article_chain, chains)