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.
 
 
 
 
 

86 regels
2.5 KiB

  1. #! /data/project/copyvios/env/bin/python
  2. # -*- coding: utf-8 -*-
  3. from functools import wraps
  4. from logging import DEBUG
  5. from logging.handlers import TimedRotatingFileHandler
  6. from time import asctime
  7. from traceback import format_exc
  8. from earwigbot.bot import Bot
  9. from flask import Flask, g, request
  10. from flask.ext.mako import MakoTemplates, render_template, TemplateError
  11. from flup.server.fcgi import WSGIServer
  12. from copyvios.checker import do_check
  13. from copyvios.cookies import parse_cookies
  14. from copyvios.settings import process_settings
  15. from copyvios.sites import get_sites
  16. app = Flask(__name__)
  17. MakoTemplates(app)
  18. app.logger.setLevel(DEBUG)
  19. app.logger.addHandler(TimedRotatingFileHandler(
  20. "logs/app.log", when="midnight", backupCount=7))
  21. app.logger.info(u"Flask server started " + asctime())
  22. bot = Bot(".earwigbot", 100)
  23. def catch_errors(func):
  24. @wraps(func)
  25. def inner(*args, **kwargs):
  26. try:
  27. return func(*args, **kwargs)
  28. except TemplateError as exc:
  29. return render_template("error.mako", traceback=exc.text)
  30. except Exception:
  31. return render_template("error.mako", traceback=format_exc())
  32. return inner
  33. @app.before_request
  34. def prepare_request():
  35. g.bot = bot
  36. g.globals_db = g.cache_db = None
  37. g.cookies = parse_cookies(request.script_root,
  38. request.environ.get("HTTP_COOKIE"))
  39. g.new_cookies = []
  40. @app.after_request
  41. def add_new_cookies(response):
  42. for cookie in g.new_cookies:
  43. response.headers.add("Set-Cookie", cookie)
  44. return response
  45. @app.after_request
  46. def write_access_log(response):
  47. msg = u"%s %s -> %s"
  48. app.logger.debug(msg, asctime(), request.path, response.status_code)
  49. return response
  50. @app.teardown_appcontext
  51. def close_databases(error):
  52. if g.globals_db:
  53. g.globals_db.close()
  54. if g.cache_db:
  55. g.cache_db.close()
  56. @app.route("/")
  57. @catch_errors
  58. def index():
  59. query = do_check()
  60. return render_template("index.mako", query=query, result=query.result)
  61. @app.route("/settings", methods=["GET", "POST"])
  62. @catch_errors
  63. def settings():
  64. status = process_settings() if request.method == "POST" else None
  65. langs, projects = get_sites()
  66. default = bot.wiki.get_site()
  67. kwargs = {"status": status, "langs": langs, "projects": projects,
  68. "default_lang": default.lang, "default_project": default.project}
  69. return render_template("settings.mako", **kwargs)
  70. if __name__ == '__main__':
  71. WSGIServer(app).run()