A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

133 rader
4.8 KiB

  1. # -*- coding: utf-8 -*-
  2. from collections import OrderedDict
  3. from .highlighter import highlight_delta
  4. from .checker import do_check, T_POSSIBLE, T_SUSPECT
  5. from .misc import Query, cache
  6. from .sites import update_sites
  7. __all__ = ["format_api_error", "handle_api_request"]
  8. _CHECK_ERRORS = {
  9. "no search method": "Either 'use_engine' or 'use_links' must be true",
  10. "no URL": "The parameter 'url' is required for URL comparisons",
  11. "bad URI": "The given URI scheme is unsupported",
  12. "no data": "No text could be found in the given URL (note that only HTML "
  13. "and plain text pages are supported, and content generated by "
  14. "JavaScript or found inside iframes is ignored)",
  15. "timeout": "The given URL timed out before any data could be retrieved",
  16. "search error": "An error occurred while using the search engine; try "
  17. "reloading or setting 'use_engine' to 0",
  18. }
  19. def _serialize_page(page):
  20. return OrderedDict((("title", page.title), ("url", page.url)))
  21. def _serialize_source(source, show_skip=True):
  22. if not source:
  23. return OrderedDict((
  24. ("url", None), ("confidence", 0.0), ("violation", "none")))
  25. conf = source.confidence
  26. data = OrderedDict((
  27. ("url", source.url),
  28. ("confidence", conf),
  29. ("violation", "suspected" if conf >= T_SUSPECT else
  30. "possible" if conf >= T_POSSIBLE else "none")
  31. ))
  32. if show_skip:
  33. data["skipped"] = source.skipped
  34. data["excluded"] = source.excluded
  35. return data
  36. def _serialize_detail(result):
  37. source_chain, delta = result.best.chains
  38. article = highlight_delta(None, result.article_chain, delta)
  39. source = highlight_delta(None, source_chain, delta)
  40. return OrderedDict((("article", article), ("source", source)))
  41. def format_api_error(code, info):
  42. if isinstance(info, BaseException):
  43. info = type(info).__name__ + ": " + str(info)
  44. elif isinstance(info, unicode):
  45. info = info.encode("utf8")
  46. error_inner = OrderedDict((("code", code), ("info", info)))
  47. return OrderedDict((("status", "error"), ("error", error_inner)))
  48. def _hook_default(query):
  49. info = u"Unknown action: '{0}'".format(query.action.lower())
  50. return format_api_error("unknown_action", info)
  51. def _hook_check(query):
  52. do_check(query)
  53. if not query.submitted:
  54. info = ("The query parameters 'project', 'lang', and either 'title' "
  55. "or 'oldid' are required for checks")
  56. return format_api_error("missing_params", info)
  57. if query.error:
  58. info = _CHECK_ERRORS.get(query.error, "An unknown error occurred")
  59. return format_api_error(query.error.replace(" ", "_"), info)
  60. elif not query.site:
  61. info = (u"The given site (project={0}, lang={1}) either doesn't exist,"
  62. u" is closed, or is private").format(query.project, query.lang)
  63. return format_api_error("bad_site", info)
  64. elif not query.result:
  65. if query.oldid:
  66. info = u"The given revision ID doesn't seem to exist: {0}"
  67. return format_api_error("bad_oldid", info.format(query.oldid))
  68. else:
  69. info = u"The given page doesn't seem to exist: {0}"
  70. return format_api_error("bad_title", info.format(query.page.title))
  71. result = query.result
  72. data = OrderedDict((
  73. ("status", "ok"),
  74. ("meta", OrderedDict((
  75. ("time", result.time),
  76. ("queries", result.queries),
  77. ("cached", result.cached),
  78. ("redirected", bool(query.redirected_from))
  79. ))),
  80. ("page", _serialize_page(query.page))
  81. ))
  82. if result.cached:
  83. data["meta"]["cache_time"] = result.cache_time
  84. if query.redirected_from:
  85. data["original_page"] = _serialize_page(query.redirected_from)
  86. data["best"] = _serialize_source(result.best, show_skip=False)
  87. data["sources"] = [_serialize_source(source) for source in result.sources]
  88. if query.detail in ("1", "true"):
  89. data["detail"] = _serialize_detail(result)
  90. return data
  91. def _hook_sites(query):
  92. update_sites()
  93. return OrderedDict((
  94. ("status", "ok"), ("langs", cache.langs), ("projects", cache.projects)
  95. ))
  96. _HOOKS = {
  97. "compare": _hook_check,
  98. "search": _hook_check,
  99. "sites": _hook_sites,
  100. }
  101. def handle_api_request():
  102. query = Query()
  103. if query.version:
  104. try:
  105. query.version = int(query.version)
  106. except ValueError:
  107. info = "The version string is invalid: {0}".format(query.version)
  108. return format_api_error("invalid_version", info)
  109. else:
  110. query.version = 1
  111. if query.version == 1:
  112. action = query.action.lower() if query.action else ""
  113. return _HOOKS.get(action, _hook_default)(query)
  114. info = "The API version is unsupported: {0}".format(query.version)
  115. return format_api_error("unsupported_version", info)