A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

129 wiersze
3.8 KiB

  1. #! /data/project/copyvios/env/bin/python
  2. # -*- coding: utf-8 -*-
  3. from functools import wraps
  4. from json import dumps
  5. from logging import DEBUG, INFO, getLogger
  6. from logging.handlers import TimedRotatingFileHandler
  7. from time import asctime
  8. from traceback import format_exc
  9. from earwigbot.bot import Bot
  10. from earwigbot.wiki.copyvios import globalize
  11. from flask import Flask, g, make_response, request
  12. from flask.ext.mako import MakoTemplates, render_template, TemplateError
  13. from flup.server.fcgi import WSGIServer
  14. from copyvios.api import format_api_error, handle_api_request
  15. from copyvios.checker import do_check
  16. from copyvios.cookies import parse_cookies
  17. from copyvios.misc import cache
  18. from copyvios.settings import process_settings
  19. from copyvios.sites import update_sites
  20. app = Flask(__name__)
  21. MakoTemplates(app)
  22. hand = TimedRotatingFileHandler("logs/app.log", when="midnight", backupCount=7)
  23. hand.setLevel(DEBUG)
  24. app.logger.addHandler(hand)
  25. app.logger.info(u"Flask server started " + asctime())
  26. def catch_errors(func):
  27. @wraps(func)
  28. def inner(*args, **kwargs):
  29. try:
  30. return func(*args, **kwargs)
  31. except TemplateError as exc:
  32. app.logger.error(u"Caught exception:\n{0}".format(exc.text))
  33. return render_template("error.mako", traceback=exc.text)
  34. except Exception:
  35. app.logger.exception(u"Caught exception:")
  36. return render_template("error.mako", traceback=format_exc())
  37. return inner
  38. @app.before_first_request
  39. def setup_app():
  40. cache.bot = Bot(".earwigbot", 100)
  41. cache.langs, cache.projects = [], []
  42. cache.last_sites_update = 0
  43. cache.background_data = {}
  44. cache.last_background_updates = {}
  45. getLogger("earwigbot.wiki.cvworker").setLevel(INFO)
  46. globalize()
  47. @app.before_request
  48. def prepare_request():
  49. g._db = None
  50. g.cookies = parse_cookies(
  51. request.script_root, request.environ.get("HTTP_COOKIE"))
  52. g.new_cookies = []
  53. @app.after_request
  54. def add_new_cookies(response):
  55. for cookie in g.new_cookies:
  56. response.headers.add("Set-Cookie", cookie)
  57. return response
  58. @app.after_request
  59. def write_access_log(response):
  60. msg = u"%s %s %s %s -> %s"
  61. app.logger.debug(msg, asctime(), request.method, request.path,
  62. request.values.to_dict(), response.status_code)
  63. return response
  64. @app.teardown_appcontext
  65. def close_databases(error):
  66. if g._db:
  67. g._db.close()
  68. @app.route("/")
  69. @catch_errors
  70. def index():
  71. update_sites()
  72. query = do_check()
  73. return render_template("index.mako", query=query, result=query.result)
  74. @app.route("/settings", methods=["GET", "POST"])
  75. @catch_errors
  76. def settings():
  77. status = process_settings() if request.method == "POST" else None
  78. update_sites()
  79. default = cache.bot.wiki.get_site()
  80. kwargs = {"status": status, "default_lang": default.lang,
  81. "default_project": default.project}
  82. return render_template("settings.mako", **kwargs)
  83. @app.route("/api")
  84. @catch_errors
  85. def api():
  86. return render_template("api.mako", help=True)
  87. @app.route("/api.json")
  88. @catch_errors
  89. def api_json():
  90. if not request.args:
  91. return render_template("api.mako", help=True)
  92. format = request.args.get("format", "json")
  93. if format in ["json", "jsonfm"]:
  94. update_sites()
  95. try:
  96. result = handle_api_request()
  97. except Exception as exc:
  98. result = format_api_error("unhandled_exception", exc)
  99. else:
  100. errmsg = u"Unknown format: '{0}'".format(format)
  101. result = format_api_error("unknown_format", errmsg)
  102. if format == "jsonfm":
  103. return render_template("api.mako", help=False, result=result)
  104. resp = make_response(dumps(result))
  105. resp.mimetype = "application/json"
  106. resp.headers["Access-Control-Allow-Origin"] = "*"
  107. return resp
  108. if __name__ == '__main__':
  109. WSGIServer(app).run()