A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

126 lignes
3.6 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. app.logger.setLevel(DEBUG)
  23. app.logger.addHandler(TimedRotatingFileHandler(
  24. "logs/app.log", when="midnight", backupCount=7))
  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. return render_template("error.mako", traceback=exc.text)
  33. except Exception:
  34. return render_template("error.mako", traceback=format_exc())
  35. return inner
  36. @app.before_first_request
  37. def setup_app():
  38. cache.bot = Bot(".earwigbot", 100)
  39. cache.langs, cache.projects = set(), set()
  40. cache.last_sites_update = 0
  41. cache.background_data = {}
  42. cache.last_background_updates = {}
  43. getLogger("earwigbot.wiki.cvworker").setLevel(INFO)
  44. # getLogger().handlers ....
  45. globalize()
  46. @app.before_request
  47. def prepare_request():
  48. g._db = None
  49. g.cookies = parse_cookies(
  50. request.script_root, request.environ.get("HTTP_COOKIE"))
  51. g.new_cookies = []
  52. @app.after_request
  53. def add_new_cookies(response):
  54. for cookie in g.new_cookies:
  55. response.headers.add("Set-Cookie", cookie)
  56. return response
  57. @app.after_request
  58. def write_access_log(response):
  59. msg = u"%s %s %s %s -> %s"
  60. app.logger.debug(msg, asctime(), request.method, request.path,
  61. request.values.to_dict(), response.status_code)
  62. return response
  63. @app.teardown_appcontext
  64. def close_databases(error):
  65. if g._db:
  66. g._db.close()
  67. @app.route("/")
  68. @catch_errors
  69. def index():
  70. query = do_check()
  71. update_sites()
  72. return render_template("index.mako", query=query, result=query.result)
  73. @app.route("/settings", methods=["GET", "POST"])
  74. @catch_errors
  75. def settings():
  76. status = process_settings() if request.method == "POST" else None
  77. update_sites()
  78. default = cache.bot.wiki.get_site()
  79. kwargs = {"status": status, "default_lang": default.lang,
  80. "default_project": default.project}
  81. return render_template("settings.mako", **kwargs)
  82. @app.route("/api")
  83. @catch_errors
  84. def api():
  85. return render_template("api.mako", help=True)
  86. @app.route("/api.json")
  87. @catch_errors
  88. def api_json():
  89. if not request.args:
  90. return render_template("api.mako", help=True)
  91. format = request.args.get("format", "json")
  92. if format in ["json", "jsonfm"]:
  93. try:
  94. result = handle_api_request()
  95. except Exception as exc:
  96. result = format_api_error("unhandled_exception", exc)
  97. else:
  98. errmsg = u"Unknown format: '{0}'".format(format)
  99. result = format_api_error("unknown_format", errmsg)
  100. if format == "jsonfm":
  101. return render_template("api.mako", help=False, result=result)
  102. resp = make_response(dumps(result))
  103. resp.mimetype = "application/json"
  104. return resp
  105. if __name__ == '__main__':
  106. WSGIServer(app).run()