A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

app.fcgi 3.5 KiB

10年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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")
  77. @catch_errors
  78. def api():
  79. return render_template("api.mako", help=True)
  80. @app.route("/api.json")
  81. @catch_errors
  82. def api_json():
  83. if not request.args:
  84. return render_template("api.mako", help=True)
  85. format = request.args.get("format", "json")
  86. if format in ["json", "jsonfm"]:
  87. try:
  88. result = handle_api_request()
  89. except Exception as exc:
  90. result = format_api_error("unhandled_exception", exc)
  91. else:
  92. errmsg = u"Unknown format: '{0}'".format(format)
  93. result = format_api_error("unknown_format", errmsg)
  94. if format == "jsonfm":
  95. return render_template("api.mako", help=False, result=result)
  96. resp = make_response(dumps(result))
  97. resp.mimetype = "application/json"
  98. return resp
  99. if __name__ == '__main__':
  100. WSGIServer(app).run()