A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

128 linhas
3.7 KiB

  1. #! /usr/bin/env 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 copyvios.api import format_api_error, handle_api_request
  14. from copyvios.checker import do_check
  15. from copyvios.cookies import parse_cookies
  16. from copyvios.misc import cache
  17. from copyvios.settings import process_settings
  18. from copyvios.sites import update_sites
  19. app = Flask(__name__)
  20. MakoTemplates(app)
  21. hand = TimedRotatingFileHandler("logs/app.log", when="midnight", backupCount=7)
  22. hand.setLevel(DEBUG)
  23. app.logger.addHandler(hand)
  24. app.logger.info(u"Flask server started " + asctime())
  25. def catch_errors(func):
  26. @wraps(func)
  27. def inner(*args, **kwargs):
  28. try:
  29. return func(*args, **kwargs)
  30. except TemplateError as exc:
  31. app.logger.error(u"Caught exception:\n{0}".format(exc.text))
  32. return render_template("error.mako", traceback=exc.text)
  33. except Exception:
  34. app.logger.exception(u"Caught exception:")
  35. return render_template("error.mako", traceback=format_exc())
  36. return inner
  37. @app.before_first_request
  38. def setup_app():
  39. cache.bot = Bot(".earwigbot", 100)
  40. cache.langs, cache.projects = [], []
  41. cache.last_sites_update = 0
  42. cache.background_data = {}
  43. cache.last_background_updates = {}
  44. getLogger("earwigbot.wiki.cvworker").setLevel(INFO)
  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. update_sites()
  71. query = do_check()
  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. update_sites()
  94. try:
  95. result = handle_api_request()
  96. except Exception as exc:
  97. result = format_api_error("unhandled_exception", exc)
  98. else:
  99. errmsg = u"Unknown format: '{0}'".format(format)
  100. result = format_api_error("unknown_format", errmsg)
  101. if format == "jsonfm":
  102. return render_template("api.mako", help=False, result=result)
  103. resp = make_response(dumps(result))
  104. resp.mimetype = "application/json"
  105. resp.headers["Access-Control-Allow-Origin"] = "*"
  106. return resp
  107. if __name__ == '__main__':
  108. app.run()