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.

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