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.
 
 
 
 
 

90 lines
2.6 KiB

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