A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
6.7 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2015 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. from threading import Event
  23. from time import time
  24. from earwigbot.wiki.copyvios.markov import EMPTY, EMPTY_INTERSECTION
  25. __all__ = ["CopyvioSource", "CopyvioCheckResult"]
  26. class CopyvioSource(object):
  27. """
  28. **EarwigBot: Wiki Toolset: Copyvio Source**
  29. A class that represents a single possible source of a copyright violation,
  30. i.e., a URL.
  31. *Attributes:*
  32. - :py:attr:`url`: the URL of the source
  33. - :py:attr:`confidence`: the confidence of a violation, between 0 and 1
  34. - :py:attr:`chains`: a 2-tuple of the source chain and the delta chain
  35. - :py:attr:`skipped`: whether this URL was skipped during the check
  36. - :py:attr:`excluded`: whether this URL was in the exclusions list
  37. """
  38. def __init__(self, workspace, url, headers=None, timeout=5,
  39. parser_args=None):
  40. self.workspace = workspace
  41. self.url = url
  42. self.headers = headers
  43. self.timeout = timeout
  44. self.parser_args = parser_args
  45. self.confidence = 0.0
  46. self.chains = (EMPTY, EMPTY_INTERSECTION)
  47. self.skipped = False
  48. self.excluded = False
  49. self._event1 = Event()
  50. self._event2 = Event()
  51. self._event2.set()
  52. def __repr__(self):
  53. """Return the canonical string representation of the source."""
  54. res = ("CopyvioSource(url={0!r}, confidence={1!r}, skipped={2!r}, "
  55. "excluded={3!r})")
  56. return res.format(
  57. self.url, self.confidence, self.skipped, self.excluded)
  58. def __str__(self):
  59. """Return a nice string representation of the source."""
  60. if self.excluded:
  61. return "<CopyvioSource ({0}, excluded)>".format(self.url)
  62. if self.skipped:
  63. return "<CopyvioSource ({0}, skipped)>".format(self.url)
  64. res = "<CopyvioSource ({0} with {1} conf)>"
  65. return res.format(self.url, self.confidence)
  66. def start_work(self):
  67. """Mark this source as being worked on right now."""
  68. self._event2.clear()
  69. self._event1.set()
  70. def update(self, confidence, source_chain, delta_chain):
  71. """Fill out the confidence and chain information inside this source."""
  72. self.confidence = confidence
  73. self.chains = (source_chain, delta_chain)
  74. def finish_work(self):
  75. """Mark this source as finished."""
  76. self._event2.set()
  77. def skip(self):
  78. """Deactivate this source without filling in the relevant data."""
  79. if self._event1.is_set():
  80. return
  81. self.skipped = True
  82. self._event1.set()
  83. def join(self, until):
  84. """Block until this violation result is filled out."""
  85. for event in [self._event1, self._event2]:
  86. if until:
  87. timeout = until - time()
  88. if timeout <= 0:
  89. return
  90. event.wait(timeout)
  91. else:
  92. event.wait()
  93. class CopyvioCheckResult(object):
  94. """
  95. **EarwigBot: Wiki Toolset: Copyvio Check Result**
  96. A class holding information about the results of a copyvio check.
  97. *Attributes:*
  98. - :py:attr:`violation`: ``True`` if this is a violation, else ``False``
  99. - :py:attr:`sources`: a list of CopyvioSources, sorted by confidence
  100. - :py:attr:`best`: the best matching CopyvioSource, or ``None``
  101. - :py:attr:`confidence`: the best matching source's confidence, or 0
  102. - :py:attr:`url`: the best matching source's URL, or ``None``
  103. - :py:attr:`queries`: the number of queries used to reach a result
  104. - :py:attr:`time`: the amount of time the check took to complete
  105. - :py:attr:`article_chain`: the MarkovChain of the article text
  106. - :py:attr:`possible_miss`: whether some URLs might have been missed
  107. """
  108. def __init__(self, violation, sources, queries, check_time, article_chain,
  109. possible_miss):
  110. self.violation = violation
  111. self.sources = sources
  112. self.queries = queries
  113. self.time = check_time
  114. self.article_chain = article_chain
  115. self.possible_miss = possible_miss
  116. def __repr__(self):
  117. """Return the canonical string representation of the result."""
  118. res = "CopyvioCheckResult(violation={0!r}, sources={1!r}, queries={2!r}, time={3!r})"
  119. return res.format(self.violation, self.sources, self.queries,
  120. self.time)
  121. def __str__(self):
  122. """Return a nice string representation of the result."""
  123. res = "<CopyvioCheckResult ({0} with best {1})>"
  124. return res.format(self.violation, self.best)
  125. @property
  126. def best(self):
  127. """The best known source, or None if no sources exist."""
  128. return self.sources[0] if self.sources else None
  129. @property
  130. def confidence(self):
  131. """The confidence of the best source, or 0 if no sources exist."""
  132. return self.best.confidence if self.best else 0.0
  133. @property
  134. def url(self):
  135. """The URL of the best source, or None if no sources exist."""
  136. return self.best.url if self.best else None
  137. def get_log_message(self, title):
  138. """Build a relevant log message for this copyvio check result."""
  139. if not self.sources:
  140. log = u"No violation for [[{0}]] (no sources; {1} queries; {2} seconds)"
  141. return log.format(title, self.queries, self.time)
  142. log = u"{0} for [[{1}]] (best: {2} ({3} confidence); {4} sources; {5} queries; {6} seconds)"
  143. is_vio = "Violation detected" if self.violation else "No violation"
  144. return log.format(is_vio, title, self.url, self.confidence,
  145. len(self.sources), self.queries, self.time)