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.
 
 
 
 
 

87 lines
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 %s -> %s"
  48. app.logger.debug(msg, asctime(), request.method, request.path,
  49. request.values.to_dict(), response.status_code)
  50. return response
  51. @app.teardown_appcontext
  52. def close_databases(error):
  53. if g.globals_db:
  54. g.globals_db.close()
  55. if g.cache_db:
  56. g.cache_db.close()
  57. @app.route("/")
  58. @catch_errors
  59. def index():
  60. query = do_check()
  61. return render_template("index.mako", query=query, result=query.result)
  62. @app.route("/settings", methods=["GET", "POST"])
  63. @catch_errors
  64. def settings():
  65. status = process_settings() if request.method == "POST" else None
  66. langs, projects = get_sites()
  67. default = bot.wiki.get_site()
  68. kwargs = {"status": status, "langs": langs, "projects": projects,
  69. "default_lang": default.lang, "default_project": default.project}
  70. return render_template("settings.mako", **kwargs)
  71. if __name__ == '__main__':
  72. WSGIServer(app).run()