A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

113 lines
3.4 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.settings import process_settings
  18. from copyvios.sites import get_sites
  19. app = Flask(__name__)
  20. MakoTemplates(app)
  21. app.logger.setLevel(DEBUG)
  22. app.logger.addHandler(TimedRotatingFileHandler(
  23. "logs/app.log", when="midnight", backupCount=7))
  24. app.logger.info(u"Flask server started " + asctime())
  25. bot = Bot(".earwigbot", 100)
  26. getLogger("earwigbot.wiki.cvworker").setLevel(INFO)
  27. globalize()
  28. def catch_errors(func):
  29. @wraps(func)
  30. def inner(*args, **kwargs):
  31. try:
  32. return func(*args, **kwargs)
  33. except TemplateError as exc:
  34. return render_template("error.mako", traceback=exc.text)
  35. except Exception:
  36. return render_template("error.mako", traceback=format_exc())
  37. return inner
  38. @app.before_request
  39. def prepare_request():
  40. g.bot = bot
  41. g.globals_db = g.cache_db = None
  42. g.cookies = parse_cookies(request.script_root,
  43. request.environ.get("HTTP_COOKIE"))
  44. g.new_cookies = []
  45. @app.after_request
  46. def add_new_cookies(response):
  47. for cookie in g.new_cookies:
  48. response.headers.add("Set-Cookie", cookie)
  49. return response
  50. @app.after_request
  51. def write_access_log(response):
  52. msg = u"%s %s %s %s -> %s"
  53. app.logger.debug(msg, asctime(), request.method, request.path,
  54. request.values.to_dict(), response.status_code)
  55. return response
  56. @app.teardown_appcontext
  57. def close_databases(error):
  58. if g.globals_db:
  59. g.globals_db.close()
  60. if g.cache_db:
  61. g.cache_db.close()
  62. @app.route("/")
  63. @catch_errors
  64. def index():
  65. query = do_check()
  66. return render_template("index.mako", query=query, result=query.result)
  67. @app.route("/settings", methods=["GET", "POST"])
  68. @catch_errors
  69. def settings():
  70. status = process_settings() if request.method == "POST" else None
  71. langs, projects = get_sites()
  72. default = bot.wiki.get_site()
  73. kwargs = {"status": status, "langs": langs, "projects": projects,
  74. "default_lang": default.lang, "default_project": default.project}
  75. return render_template("settings.mako", **kwargs)
  76. @app.route("/api.json")
  77. def api():
  78. if not request.args:
  79. return render_template("api.mako", help=True)
  80. format = request.args.get("format", "json")
  81. if format in ["json", "jsonfm"]:
  82. try:
  83. result = handle_api_request()
  84. except Exception as exc:
  85. result = format_api_error("unhandled_exception", exc)
  86. else:
  87. errmsg = u"Unknown format: '{0}'".format(format)
  88. result = format_api_error("unknown_format", errmsg)
  89. if format == "jsonfm":
  90. return render_template("api.mako", help=False, result=result)
  91. resp = make_response(dumps(result))
  92. resp.mimetype = "application/json"
  93. return resp
  94. if __name__ == '__main__':
  95. WSGIServer(app).run()