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.
 
 
 
 
 

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