plugin XEP-0277: better resilience to broken items:

- if `author` element can't be found, `publisher` attribute, then `IQ`'s `from` attributes
  are used as fallback to find author jid
- fix categories (tags) parsing if `author` element is not found
- remove items which have failed parsing from `mbGet` results (instead of using `None`).
This commit is contained in:
Goffi 2021-03-27 14:38:27 +01:00
commit 69112bcd79
2 changed files with 18 additions and 8 deletions

View file

@ -1181,7 +1181,10 @@ class XEP_0060(object):
log.warning(f"Error while parsing item: {failure_.value}") log.warning(f"Error while parsing item: {failure_.value}")
d = defer.gatherResults([item_cb(item).addErrback(eb) for item in items]) d = defer.gatherResults([item_cb(item).addErrback(eb) for item in items])
d.addCallback(lambda parsed_items: (parsed_items, metadata)) d.addCallback(lambda parsed_items: (
[i for i in parsed_items if i is not None],
metadata
))
return d return d
def serDList(self, results, failure_result=None): def serDList(self, results, failure_result=None):

View file

@ -415,12 +415,19 @@ class XEP_0277(object):
) )
# author # author
publisher = item_elt.getAttribute("publisher")
try: try:
author_elt = next(entry_elt.elements(NS_ATOM, "author")) author_elt = next(entry_elt.elements(NS_ATOM, "author"))
except StopIteration: except StopIteration:
log.debug("Can't find author element in item {}".format(id_)) log.debug("Can't find author element in item {}".format(id_))
if publisher:
microblog_data["author_jid"] = publisher
microblog_data["author_jid_verified"] = True
else:
iq_elt = xml_tools.findAncestor(item_elt, "iq", C.NS_CLIENT)
microblog_data["author_jid"] = iq_elt["from"]
microblog_data["author_jid_verified"] = False
else: else:
publisher = item_elt.getAttribute("publisher")
# name # name
try: try:
name_elt = next(author_elt.elements(NS_ATOM, "name")) name_elt = next(author_elt.elements(NS_ATOM, "name"))
@ -470,12 +477,12 @@ class XEP_0277(object):
else: else:
microblog_data["author_email"] = str(email_elt) microblog_data["author_email"] = str(email_elt)
# categories # categories
categories = [ categories = [
category_elt.getAttribute("term", "") category_elt.getAttribute("term", "")
for category_elt in entry_elt.elements(NS_ATOM, "category") for category_elt in entry_elt.elements(NS_ATOM, "category")
] ]
microblog_data["tags"] = categories microblog_data["tags"] = categories
## the trigger ## ## the trigger ##
# if other plugins have things to add or change # if other plugins have things to add or change