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.
 
 
 
 
 

77 rivejä
1.9 KiB

  1. # -*- coding: utf-8 -*-
  2. from os.path import expanduser
  3. from flask import g, request
  4. import oursql
  5. from sqlalchemy.pool import manage
  6. oursql = manage(oursql)
  7. __all__ = ["Query", "cache", "get_db", "get_notice", "httpsfix", "urlstrip"]
  8. class Query(object):
  9. def __init__(self, method="GET"):
  10. self.query = {}
  11. data = request.form if method == "POST" else request.args
  12. for key in data:
  13. self.query[key] = data.getlist(key)[-1]
  14. def __getattr__(self, key):
  15. return self.query.get(key)
  16. def __setattr__(self, key, value):
  17. if key == "query":
  18. super(Query, self).__setattr__(key, value)
  19. else:
  20. self.query[key] = value
  21. class _AppCache(object):
  22. def __init__(self):
  23. super(_AppCache, self).__setattr__("_data", {})
  24. def __getattr__(self, key):
  25. return self._data[key]
  26. def __setattr__(self, key, value):
  27. self._data[key] = value
  28. cache = _AppCache()
  29. def get_db():
  30. if not g._db:
  31. args = cache.bot.config.wiki["_copyviosSQL"]
  32. args["read_default_file"] = expanduser("~/.my.cnf")
  33. args["autoping"] = True
  34. args["autoreconnect"] = True
  35. g._db = oursql.connect(**args)
  36. return g._db
  37. def get_notice():
  38. try:
  39. with open(expanduser("~/copyvios_notice.html")) as fp:
  40. lines = fp.read().decode("utf8").strip().splitlines()
  41. if lines[0] == "<!-- active -->":
  42. return "\n".join(lines[1:])
  43. return None
  44. except IOError:
  45. return None
  46. def httpsfix(context, url):
  47. if url.startswith("http://"):
  48. url = url[len("http:"):]
  49. return url
  50. def urlstrip(context, url):
  51. if url.startswith("http://"):
  52. url = url[7:]
  53. if url.startswith("https://"):
  54. url = url[8:]
  55. if url.startswith("www."):
  56. url = url[4:]
  57. if url.endswith("/"):
  58. url = url[:-1]
  59. return url