A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

72 строки
2.1 KiB

  1. # -*- coding: utf-8 -*-
  2. from os.path import expanduser
  3. from urlparse import parse_qs
  4. from earwigbot.bot import Bot
  5. from flask import request
  6. import oursql
  7. _bot = None
  8. _connections = {}
  9. class Query(object):
  10. def __init__(self, method="GET"):
  11. self.query = {}
  12. if method == "GET":
  13. parsed = parse_qs(request.environ["QUERY_STRING"])
  14. elif method == "POST":
  15. size = int(request.environ.get("CONTENT_LENGTH", 0))
  16. parsed = parse_qs(request.environ["wsgi.input"].read(size))
  17. else:
  18. parsed = {}
  19. for key, value in parsed.iteritems():
  20. try:
  21. self.query[key] = value[-1].decode("utf8")
  22. except UnicodeDecodeError:
  23. pass
  24. def __getattr__(self, key):
  25. return self.query.get(key)
  26. def __setattr__(self, key, value):
  27. if key == "query":
  28. super(Query, self).__setattr__(key, value)
  29. else:
  30. self.query[key] = value
  31. def get_bot():
  32. global _bot
  33. if not _bot:
  34. _bot = Bot(".earwigbot", 100) # Don't print any logs to the console
  35. return _bot
  36. def open_sql_connection(bot, dbname):
  37. if dbname in _connections:
  38. return _connections[dbname]
  39. conn_args = bot.config.wiki["_copyviosSQL"][dbname]
  40. if "read_default_file" not in conn_args and "user" not in conn_args and "passwd" not in conn_args:
  41. conn_args["read_default_file"] = expanduser("~/.my.cnf")
  42. elif "read_default_file" in conn_args:
  43. default_file = expanduser(conn_args["read_default_file"])
  44. conn_args["read_default_file"] = default_file
  45. if "autoping" not in conn_args:
  46. conn_args["autoping"] = True
  47. if "autoreconnect" not in conn_args:
  48. conn_args["autoreconnect"] = True
  49. conn = oursql.connect(**conn_args)
  50. _connections[dbname] = conn
  51. return conn
  52. def urlstrip(context, url):
  53. if url.startswith("http://"):
  54. url = url[7:]
  55. if url.startswith("https://"):
  56. url = url[8:]
  57. if url.startswith("www."):
  58. url = url[4:]
  59. if url.endswith("/"):
  60. url = url[:-1]
  61. return url