|
| 1 | +# -*- coding: utf-8; -*- |
| 2 | +# |
| 3 | +# This file is part of Superdesk. |
| 4 | +# |
| 5 | +# Copyright 2025 Sourcefabric z.u. and contributors. |
| 6 | +# |
| 7 | +# For the full copyright and license information, please see the |
| 8 | +# AUTHORS and LICENSE files distributed with this source code, or |
| 9 | +# at https://www.sourcefabric.org/superdesk/license |
| 10 | + |
| 11 | +from datetime import datetime |
| 12 | +from superdesk.utc import utc |
| 13 | +from superdesk.etree import etree |
| 14 | +from superdesk.io.registry import register_feed_parser |
| 15 | +from superdesk.io.feed_parsers.nitf import NITFFeedParser |
| 16 | + |
| 17 | + |
| 18 | +class PAParser(NITFFeedParser): |
| 19 | + """ |
| 20 | + Feed Parser for PA (Press Association) XML files. |
| 21 | + """ |
| 22 | + |
| 23 | + NAME = "pa_parser" |
| 24 | + label = "PA Parser" |
| 25 | + |
| 26 | + def can_parse(self, xml): |
| 27 | + """ |
| 28 | + Check if the XML can be parsed by this parser. |
| 29 | + """ |
| 30 | + return xml.tag == "document" and xml.find("nitf") is not None |
| 31 | + |
| 32 | + def parse(self, xml, provider=None): |
| 33 | + """ |
| 34 | + Parse the XML and return a single dictionary representing the news item. |
| 35 | + """ |
| 36 | + item = {} |
| 37 | + nitf = xml.find("nitf") |
| 38 | + if nitf is not None: |
| 39 | + self.parse_head(nitf, item) |
| 40 | + self.parse_body(nitf, item) |
| 41 | + self.parse_resource(xml, item) |
| 42 | + return item |
| 43 | + |
| 44 | + def parse_head(self, nitf, item): |
| 45 | + """ |
| 46 | + Parse the head section of the NITF document. |
| 47 | + """ |
| 48 | + head = nitf.find("head") |
| 49 | + if head is not None: |
| 50 | + title = head.find("title") |
| 51 | + if title is not None and title.text: |
| 52 | + item["headline"] = title.text |
| 53 | + |
| 54 | + def parse_body(self, nitf, item): |
| 55 | + """ |
| 56 | + Parse the body section of the NITF document. |
| 57 | + """ |
| 58 | + body = nitf.find("body") |
| 59 | + if body is not None: |
| 60 | + self.parse_body_head(body, item) |
| 61 | + self.parse_body_content(body, item) |
| 62 | + |
| 63 | + def parse_body_head(self, body, item): |
| 64 | + """ |
| 65 | + Parse the body.head section of the NITF document. |
| 66 | + """ |
| 67 | + body_head = body.find("body.head") |
| 68 | + if body_head is not None: |
| 69 | + self.parse_hedline(body_head, item) |
| 70 | + self.parse_byline(body_head, item) |
| 71 | + |
| 72 | + def parse_hedline(self, body_head, item): |
| 73 | + """ |
| 74 | + Parse the hedline section of the NITF document. |
| 75 | + """ |
| 76 | + hedline = body_head.find("hedline") |
| 77 | + if hedline is not None: |
| 78 | + hl1 = hedline.find("hl1") |
| 79 | + if hl1 is not None and hl1.text: |
| 80 | + item["headline"] = hl1.text |
| 81 | + |
| 82 | + def parse_byline(self, body_head, item): |
| 83 | + """ |
| 84 | + Parse the byline section of the NITF document. |
| 85 | + """ |
| 86 | + byline = body_head.find("byline") |
| 87 | + if byline is not None: |
| 88 | + bytag = byline.find("bytag") |
| 89 | + if bytag is not None and bytag.text: |
| 90 | + item["byline"] = bytag.text |
| 91 | + |
| 92 | + def parse_body_content(self, body, item): |
| 93 | + """ |
| 94 | + Parse the body.content section of the NITF document and clean up HTML content. |
| 95 | + """ |
| 96 | + body_content = body.find("body.content") |
| 97 | + if body_content is not None: |
| 98 | + body_html = etree.tostring(body_content, encoding="unicode", method="html") |
| 99 | + parser = etree.HTMLParser() |
| 100 | + tree = etree.fromstring(body_html, parser) |
| 101 | + etree.strip_tags(tree, "section", "body", "span", "body.content") |
| 102 | + cleaned_html = "".join(etree.tostring(child, encoding="unicode", method="html") for child in tree) |
| 103 | + item["body_html"] = cleaned_html |
| 104 | + |
| 105 | + def parse_resource(self, xml, item): |
| 106 | + """ |
| 107 | + Parse the Resource section of the XML document. |
| 108 | + """ |
| 109 | + resource = xml.find(".//xn:Resource", namespaces={"xn": "http://www.xmlnews.org/namespaces/meta#"}) |
| 110 | + if resource is not None: |
| 111 | + for vendor_data in resource.findall( |
| 112 | + ".//xn:vendorData", namespaces={"xn": "http://www.xmlnews.org/namespaces/meta#"} |
| 113 | + ): |
| 114 | + if vendor_data.text and "PRESSUK_:Document ID=" in vendor_data.text: |
| 115 | + document_id = vendor_data.text.split("PRESSUK_:Document ID=")[-1].strip() |
| 116 | + if document_id: |
| 117 | + item["guid"] = document_id |
| 118 | + break |
| 119 | + self.parse_versioncreated(resource, item) |
| 120 | + self.parse_firstcreated(resource, item) |
| 121 | + self.parse_abstract(resource, item) |
| 122 | + self.parse_usageterms(resource, item) |
| 123 | + self.parse_word_count(resource, item) |
| 124 | + self.parse_keywords(resource, item) |
| 125 | + self.parse_embargo(resource, item) |
| 126 | + self.parse_priority(resource, item) |
| 127 | + |
| 128 | + def parse_versioncreated(self, resource, item): |
| 129 | + """ |
| 130 | + Parse the versioncreated timestamp from the Resource section. |
| 131 | + """ |
| 132 | + publication_time = resource.find( |
| 133 | + "xn:publicationTime", namespaces={"xn": "http://www.xmlnews.org/namespaces/meta#"} |
| 134 | + ) |
| 135 | + if publication_time is not None and publication_time.text: |
| 136 | + item["versioncreated"] = datetime.strptime(publication_time.text, "%Y-%m-%dT%H:%M:%S+00:00").replace( |
| 137 | + tzinfo=utc |
| 138 | + ) |
| 139 | + |
| 140 | + def parse_firstcreated(self, resource, item): |
| 141 | + """ |
| 142 | + Parse the firstcreated timestamp from the Resource section. |
| 143 | + """ |
| 144 | + received_time = resource.find("xn:receivedTime", namespaces={"xn": "http://www.xmlnews.org/namespaces/meta#"}) |
| 145 | + if received_time is not None and received_time.text: |
| 146 | + item["firstcreated"] = datetime.strptime(received_time.text, "%Y-%m-%dT%H:%M:%S+00:00").replace(tzinfo=utc) |
| 147 | + |
| 148 | + def parse_abstract(self, resource, item): |
| 149 | + """ |
| 150 | + Parse the abstract from the Resource section. |
| 151 | + """ |
| 152 | + description = resource.find("xn:description", namespaces={"xn": "http://www.xmlnews.org/namespaces/meta#"}) |
| 153 | + if description is not None and description.text: |
| 154 | + item["abstract"] = description.text |
| 155 | + |
| 156 | + def parse_usageterms(self, resource, item): |
| 157 | + """ |
| 158 | + Parse the usage terms (copyright) from the Resource section. |
| 159 | + """ |
| 160 | + copyright = resource.find("xn:copyright", namespaces={"xn": "http://www.xmlnews.org/namespaces/meta#"}) |
| 161 | + if copyright is not None and copyright.text: |
| 162 | + item["usageterms"] = copyright.text |
| 163 | + |
| 164 | + def parse_embargo(self, resource, item): |
| 165 | + """ |
| 166 | + Parse the embargo timestamp from the Resource section. |
| 167 | + """ |
| 168 | + for vendor_data in resource.findall("{http://www.xmlnews.org/namespaces/meta#}vendorData"): |
| 169 | + if vendor_data.text and "PRESSUK_:Expiration Date=" in vendor_data.text: |
| 170 | + embargo = vendor_data.text.split("PRESSUK_:Expiration Date=")[-1].strip() |
| 171 | + if embargo: |
| 172 | + try: |
| 173 | + embargo_dt = datetime.strptime(embargo, "%Y-%m-%dT%H:%M:%S%z") |
| 174 | + item["embargo"] = embargo_dt.isoformat() |
| 175 | + except ValueError: |
| 176 | + pass |
| 177 | + |
| 178 | + def parse_word_count(self, resource, item): |
| 179 | + for vendor_data in resource.findall("{http://www.xmlnews.org/namespaces/meta#}vendorData"): |
| 180 | + if vendor_data.text and "PRESSUK_:Word Count=" in vendor_data.text: |
| 181 | + word_count = vendor_data.text.split("PRESSUK_:Word Count=")[-1].strip() |
| 182 | + if word_count: |
| 183 | + try: |
| 184 | + item["word_count"] = int(word_count) |
| 185 | + except ValueError: |
| 186 | + pass |
| 187 | + |
| 188 | + def parse_priority(self, resource, item): |
| 189 | + for vendor_data in resource.findall("{http://www.xmlnews.org/namespaces/meta#}vendorData"): |
| 190 | + if vendor_data.text and "PRESSUK_:PA Priority=" in vendor_data.text: |
| 191 | + priority = vendor_data.text.split("PRESSUK_:PA Priority=")[-1].strip() |
| 192 | + if priority: |
| 193 | + try: |
| 194 | + item["priority"] = int(priority) |
| 195 | + except ValueError: |
| 196 | + pass |
| 197 | + |
| 198 | + def parse_keywords(self, resource, item): |
| 199 | + keywords = [] |
| 200 | + for vendor_data in resource.findall("{http://www.xmlnews.org/namespaces/meta#}vendorData"): |
| 201 | + if vendor_data.text and "PRESSUK_:Keyword=" in vendor_data.text: |
| 202 | + keyword = vendor_data.text.split("PRESSUK_:Keyword=")[-1].strip() |
| 203 | + if keyword: |
| 204 | + keywords.append(keyword) |
| 205 | + if keywords: |
| 206 | + item["keywords"] = keywords |
| 207 | + |
| 208 | + |
| 209 | +register_feed_parser(PAParser.NAME, PAParser()) |
0 commit comments