jenkins-bot has submitted this change and it was merged.
Change subject: MVP: implement Echo support ......................................................................
MVP: implement Echo support
with a new Notification class in echo.py
and new APISite methods: - notifications() - notifications_mark_read()
bug: 66275 Change-Id: I25d72ce5f3f3418d8f92e9e41186aaf07fef04ea --- A pywikibot/echo.py M pywikibot/site.py 2 files changed, 84 insertions(+), 0 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/echo.py b/pywikibot/echo.py new file mode 100644 index 0000000..a7ed138 --- /dev/null +++ b/pywikibot/echo.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +"""Classes and functions for working with the Echo extension.""" + +import pywikibot + + +class Notification(object): + + """A notification issued by the Echo extension.""" + + def __init__(self, site): + """Construct an empty Notification object.""" + self.site = site + + @classmethod + def fromJSON(cls, site, data): + """ + Construct a Notification object from JSON data returned by the API. + + @rtype: Notification + """ + notif = cls(site) + + notif.id = data['id'] # TODO: use numeric id ? + notif.type = data['type'] + notif.category = data['category'] + notif.timestamp = pywikibot.Timestamp.fromtimestampformat(data['timestamp']['mw']) + + # TODO: use 'namespace-key' + 'text' ? + notif.page = pywikibot.Page(site, data['title']['full']) + + if 'agent' in data and 'name' in data['agent']: + notif.agent = pywikibot.User(site, data['agent']['name']) + else: + notif.agent = None + + if 'read' in data: + notif.read = pywikibot.Timestamp.fromtimestampformat(data['read']) + else: + notif.read = False + + notif.content = data.get('*', None) + + return notif + + def mark_as_read(self): + """Mark the notification as read.""" + return self.site.notifications_mark_read(list=self.id) diff --git a/pywikibot/site.py b/pywikibot/site.py index 8e42f27..e5da073 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -42,6 +42,7 @@ SpamfilterError, UserBlocked, ) +from pywikibot.echo import Notification
if sys.version_info[0] > 2: basestring = (str,) @@ -1480,6 +1481,41 @@ self.login(sysop) return 'hasmsg' in self._userinfo
+ def notifications(self, **kwargs): + """Yield Notification objects from the Echo extension.""" + if self.has_extension('Echo'): + params = dict(site=self, action='query', + meta='notifications', + notprop='list', notformat='text') + + for key in kwargs: + params['not' + key] = kwargs[key] + + data = api.Request(**params).submit() + for notif in data['query']['notifications']['list'].values(): + yield Notification.fromJSON(self, notif) + + def notifications_mark_read(self, **kwargs): + """Mark one, some or all notifications, + selected via keyword arguments, as read. + + @return: whether the action was successful + @rtype: bool + """ + if self.has_extension('Echo'): + # TODO: ensure that the 'echomarkread' action + # is supported by the site + req = api.Request(site=self, + action='echomarkread', + token=self.token(pywikibot.Page(self, u'Main Page'), + 'edit'), # Use a dummy page + **kwargs) + data = req.submit() + try: + return data['query']['echomarkread']['result'] == 'success' + except KeyError: + return False + def mediawiki_messages(self, keys): """Fetch the text of a set of MediaWiki messages.
pywikibot-commits@lists.wikimedia.org