comparison sat/plugins/plugin_xep_0277.py @ 3492:fa796612adad

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`).
author Goffi <goffi@goffi.org>
date Sat, 27 Mar 2021 14:38:27 +0100
parents be6d91572633
children b54bdd4ec507
comparison
equal deleted inserted replaced
3491:2bd75fc2555d 3492:fa796612adad
413 rel=rel, title=title, href=href 413 rel=rel, title=title, href=href
414 ) 414 )
415 ) 415 )
416 416
417 # author 417 # author
418 publisher = item_elt.getAttribute("publisher")
418 try: 419 try:
419 author_elt = next(entry_elt.elements(NS_ATOM, "author")) 420 author_elt = next(entry_elt.elements(NS_ATOM, "author"))
420 except StopIteration: 421 except StopIteration:
421 log.debug("Can't find author element in item {}".format(id_)) 422 log.debug("Can't find author element in item {}".format(id_))
423 if publisher:
424 microblog_data["author_jid"] = publisher
425 microblog_data["author_jid_verified"] = True
426 else:
427 iq_elt = xml_tools.findAncestor(item_elt, "iq", C.NS_CLIENT)
428 microblog_data["author_jid"] = iq_elt["from"]
429 microblog_data["author_jid_verified"] = False
422 else: 430 else:
423 publisher = item_elt.getAttribute("publisher")
424 # name 431 # name
425 try: 432 try:
426 name_elt = next(author_elt.elements(NS_ATOM, "name")) 433 name_elt = next(author_elt.elements(NS_ATOM, "name"))
427 except StopIteration: 434 except StopIteration:
428 log.warning( 435 log.warning(
468 except StopIteration: 475 except StopIteration:
469 pass 476 pass
470 else: 477 else:
471 microblog_data["author_email"] = str(email_elt) 478 microblog_data["author_email"] = str(email_elt)
472 479
473 # categories 480 # categories
474 categories = [ 481 categories = [
475 category_elt.getAttribute("term", "") 482 category_elt.getAttribute("term", "")
476 for category_elt in entry_elt.elements(NS_ATOM, "category") 483 for category_elt in entry_elt.elements(NS_ATOM, "category")
477 ] 484 ]
478 microblog_data["tags"] = categories 485 microblog_data["tags"] = categories
479 486
480 ## the trigger ## 487 ## the trigger ##
481 # if other plugins have things to add or change 488 # if other plugins have things to add or change
482 yield self.host.trigger.point( 489 yield self.host.trigger.point(
483 "XEP-0277_item2data", item_elt, entry_elt, microblog_data 490 "XEP-0277_item2data", item_elt, entry_elt, microblog_data