A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

104 rindas
3.7 KiB

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