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.
 
 
 
 
 

126 rader
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. globalize()
  45. @app.before_request
  46. def prepare_request():
  47. g._db = None
  48. g.cookies = parse_cookies(
  49. request.script_root, request.environ.get("HTTP_COOKIE"))
  50. g.new_cookies = []
  51. @app.after_request
  52. def add_new_cookies(response):
  53. for cookie in g.new_cookies:
  54. response.headers.add("Set-Cookie", cookie)
  55. return response
  56. @app.after_request
  57. def write_access_log(response):
  58. msg = u"%s %s %s %s -> %s"
  59. app.logger.debug(msg, asctime(), request.method, request.path,
  60. request.values.to_dict(), response.status_code)
  61. return response
  62. @app.teardown_appcontext
  63. def close_databases(error):
  64. if g._db:
  65. g._db.close()
  66. @app.route("/")
  67. @catch_errors
  68. def index():
  69. update_sites()
  70. query = do_check()
  71. return render_template("index.mako", query=query, result=query.result)
  72. @app.route("/settings", methods=["GET", "POST"])
  73. @catch_errors
  74. def settings():
  75. status = process_settings() if request.method == "POST" else None
  76. update_sites()
  77. default = cache.bot.wiki.get_site()
  78. kwargs = {"status": status, "default_lang": default.lang,
  79. "default_project": default.project}
  80. return render_template("settings.mako", **kwargs)
  81. @app.route("/api")
  82. @catch_errors
  83. def api():
  84. return render_template("api.mako", help=True)
  85. @app.route("/api.json")
  86. @catch_errors
  87. def api_json():
  88. if not request.args:
  89. return render_template("api.mako", help=True)
  90. format = request.args.get("format", "json")
  91. if format in ["json", "jsonfm"]:
  92. update_sites()
  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()