A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rights.py 1.7 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. """
  3. Retrieve a list of user rights for a given username via the API.
  4. """
  5. import json
  6. import urllib
  7. from irc.classes import BaseCommand
  8. class Rights(BaseCommand):
  9. def get_hooks(self):
  10. return ["msg"]
  11. def get_help(self, command):
  12. return "Retrieve a list of rights for a given username."
  13. def check(self, data):
  14. if data.is_command and data.command in ["rights", "groups", "permissions", "privileges"]:
  15. return True
  16. return False
  17. def process(self, data):
  18. if not data.args:
  19. self.connection.reply(data, "what user do you want me to look up?")
  20. return
  21. username = ' '.join(data.args)
  22. rights = self.get_rights(username)
  23. if rights:
  24. self.connection.reply(data, "the rights for \x0302{0}\x0301 are {1}.".format(username, ', '.join(rights)))
  25. else:
  26. self.connection.reply(data, "the user \x0302{0}\x0301 has no rights, or does not exist.".format(username))
  27. def get_rights(self, username):
  28. params = {'action': 'query', 'format': 'json', 'list': 'users', 'usprop': 'groups'}
  29. params['ususers'] = username
  30. data = urllib.urlencode(params)
  31. raw = urllib.urlopen("http://en.wikipedia.org/w/api.php", data).read()
  32. res = json.loads(raw)
  33. try:
  34. rights = res['query']['users'][0]['groups']
  35. except KeyError: # 'groups' not found, meaning the user does not exist
  36. return None
  37. try:
  38. rights.remove("*") # remove the implicit '*' group given to everyone
  39. except ValueError: # I don't expect this to happen, but if it does, be prepared
  40. pass
  41. return rights