Browse Source

afcAFCStatus and Rights IRC commands now use wikitools

tags/v0.1^2
Ben Kurtovic 13 years ago
parent
commit
28cbbd7221
2 changed files with 15 additions and 42 deletions
  1. +7
    -22
      irc/commands/afc_status.py
  2. +8
    -20
      irc/commands/rights.py

+ 7
- 22
irc/commands/afc_status.py View File

@@ -3,12 +3,11 @@
"""Report the status of AFC submissions, either as an automatic message on join
or a request via !status."""

import json
import re
import urllib

from core import config
from irc.classes import BaseCommand
from wiki import tools

class AFCStatus(BaseCommand):
def get_hooks(self):
@@ -29,6 +28,8 @@ class AFCStatus(BaseCommand):
return False

def process(self, data):
self.site = tools.get_site()

if data.line[1] == "JOIN":
notice = self.get_join_notice()
self.connection.notice(data.nick, notice)
@@ -85,19 +86,15 @@ class AFCStatus(BaseCommand):

def count_submissions(self):
"""Returns the number of open AFC submissions (count of CAT:PEND)."""
params = {'action': 'query', 'list': 'categorymembers', 'cmlimit':'500', 'format': 'json'}
params['cmtitle'] = "Category:Pending_AfC_submissions"
data = urllib.urlencode(params)
raw = urllib.urlopen("http://en.wikipedia.org/w/api.php", data).read()
res = json.loads(raw)
subs = len(res['query']['categorymembers'])
cat = self.site.get_category("Pending AfC submissions")
subs = cat.get_members(limit=500)
subs -= 2 # remove [[Wikipedia:Articles for creation/Redirects]] and [[Wikipedia:Files for upload]], which aren't real submissions
return subs

def count_redirects(self):
"""Returns the number of open redirect submissions. Calculated as the
total number of submissions minus the closed ones."""
content = self.get_page("Wikipedia:Articles_for_creation/Redirects")
content = self.site.get_page("Wikipedia:Articles for creation/Redirects").get()
total = len(re.findall("^\s*==(.*?)==\s*$", content, re.MULTILINE))
closed = content.lower().count("{{afc-c|b}}")
redirs = total - closed
@@ -106,24 +103,12 @@ class AFCStatus(BaseCommand):
def count_files(self):
"""Returns the number of open WP:FFU (Files For Upload) requests.
Calculated as the total number of requests minus the closed ones."""
content = self.get_page("Wikipedia:Files_for_upload")
content = self.site.get_page("Wikipedia:Files for upload").get()
total = len(re.findall("^\s*==(.*?)==\s*$", content, re.MULTILINE))
closed = content.lower().count("{{ifu-c|b}}")
files = total - closed
return files

def get_page(self, pagename):
"""Simple method to return the content of the page 'pagename'. Will be
a part of wiki/tools/ when I finish that."""
params = {'action': 'query', 'prop': 'revisions', 'rvprop':'content', 'rvlimit':'1', 'format': 'json'}
params['titles'] = pagename
data = urllib.urlencode(params)
raw = urllib.urlopen("http://en.wikipedia.org/w/api.php", data).read()
res = json.loads(raw)
pageid = res['query']['pages'].keys()[0]
content = res['query']['pages'][pageid]['revisions'][0]['*']
return content

def get_aggregate(self, num):
"""Returns a human-readable AFC status based on the number of pending
AFC submissions, open redirect requests, and open FFU requests. This


+ 8
- 20
irc/commands/rights.py View File

@@ -4,10 +4,8 @@
Retrieve a list of user rights for a given username via the API.
"""

import json
import urllib

from irc.classes import BaseCommand
from wiki import tools

class Rights(BaseCommand):
def get_hooks(self):
@@ -27,24 +25,14 @@ class Rights(BaseCommand):
return

username = ' '.join(data.args)
rights = self.get_rights(username)
site = tools.get_site()
user = site.get_user(username)
rights = user.get_rights()
if rights:
try:
rights.remove("*") # remove the implicit '*' group given to everyone
except ValueError:
pass
self.connection.reply(data, "the rights for \x0302{0}\x0301 are {1}.".format(username, ', '.join(rights)))
else:
self.connection.reply(data, "the user \x0302{0}\x0301 has no rights, or does not exist.".format(username))

def get_rights(self, username):
params = {'action': 'query', 'format': 'json', 'list': 'users', 'usprop': 'groups'}
params['ususers'] = username
data = urllib.urlencode(params)
raw = urllib.urlopen("http://en.wikipedia.org/w/api.php", data).read()
res = json.loads(raw)
try:
rights = res['query']['users'][0]['groups']
except KeyError: # 'groups' not found, meaning the user does not exist
return None
try:
rights.remove("*") # remove the implicit '*' group given to everyone
except ValueError: # I don't expect this to happen, but if it does, be prepared
pass
return rights

Loading…
Cancel
Save