Exemplo n.º 1
0
 def test_map_priority(self):
     self.assertEqual('f', map_priority(1))
     self.assertEqual('u', map_priority(2))
     self.assertEqual('b', map_priority(3))
     self.assertEqual('r', map_priority(4))
     self.assertEqual('r', map_priority(5))
     self.assertEqual('r', map_priority(6))
     self.assertEqual('r', map_priority(None))
     self.assertEqual('r', map_priority(7))
     self.assertEqual('r', map_priority(''))
Exemplo n.º 2
0
 def test_map_priority(self):
     self.assertEqual('f', map_priority(1))
     self.assertEqual('u', map_priority(2))
     self.assertEqual('b', map_priority(3))
     self.assertEqual('r', map_priority(4))
     self.assertEqual('r', map_priority(5))
     self.assertEqual('r', map_priority(6))
     self.assertEqual('r', map_priority(None))
     self.assertEqual('r', map_priority(7))
     self.assertEqual('r', map_priority(''))
 def test_map_priority(self):
     self.assertEqual("f", map_priority(1))
     self.assertEqual("u", map_priority(2))
     self.assertEqual("b", map_priority(3))
     self.assertEqual("r", map_priority(4))
     self.assertEqual("r", map_priority(5))
     self.assertEqual("r", map_priority(6))
     self.assertEqual("r", map_priority(None))
     self.assertEqual("r", map_priority(7))
     self.assertEqual("r", map_priority(""))
Exemplo n.º 4
0
    def format(self, article, subscriber):
        """
        Constructs a dictionary that represents the parameters passed to the SMS InsertAlerts stored procedure
        :return: returns the sequence number of the subscriber and the constructed parameter dictionary
        """
        try:
            pub_seq_num = superdesk.get_resource_service(
                'subscribers').generate_sequence_number(subscriber)

            odbc_item = {
                'Sequence': pub_seq_num,
                'Category': article.get('anpa_category', [{}])[0].get('qcode'),
                'Headline': article.get('headline', '').replace('\'', '\'\''),
                'Priority': map_priority(article.get('priority'))
            }

            if article.get(EMBARGO):
                embargo = '{}{}'.format('Embargo Content. Timestamp: ',
                                        article.get(EMBARGO).isoformat())
                article['body_html'] = embargo + article['body_html']

            if article[ITEM_TYPE] == CONTENT_TYPE.PREFORMATTED:
                odbc_item['StoryText'] = article.get('body_html', '').replace(
                    '\'', '\'\'')  # @article_text
            elif article[ITEM_TYPE] == CONTENT_TYPE.TEXT:
                soup = BeautifulSoup(article.get('body_html', ''))
                odbc_item['StoryText'] = soup.text.replace('\'', '\'\'')

            odbc_item['ident'] = '0'

            return [(pub_seq_num, odbc_item)]
        except Exception as ex:
            raise FormatterError.AAPSMSFormatterError(ex, subscriber)
Exemplo n.º 5
0
    def format(self, article, subscriber):
        """
        Constructs a dictionary that represents the parameters passed to the SMS InsertAlerts stored procedure
        :return: returns the sequence number of the subscriber and the constructed parameter dictionary
        """
        try:
            pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)

            odbc_item = {'Sequence': pub_seq_num, 'Category': article.get('anpa_category', [{}])[0].get('qcode'),
                         'Headline': article.get('headline', '').replace('\'', '\'\''),
                         'Priority': map_priority(article.get('priority'))}

            if article.get(EMBARGO):
                embargo = '{}{}'.format('Embargo Content. Timestamp: ', article.get(EMBARGO).isoformat())
                article['body_html'] = embargo + article['body_html']

            if article[ITEM_TYPE] == CONTENT_TYPE.PREFORMATTED:
                odbc_item['StoryText'] = article.get('body_html', '').replace('\'', '\'\'')  # @article_text
            elif article[ITEM_TYPE] == CONTENT_TYPE.TEXT:
                soup = BeautifulSoup(article.get('body_html', ''))
                odbc_item['StoryText'] = soup.text.replace('\'', '\'\'')

            odbc_item['ident'] = '0'

            return [(pub_seq_num, odbc_item)]
        except Exception as ex:
            raise FormatterError.AAPSMSFormatterError(ex, subscriber)
Exemplo n.º 6
0
    def format(self, article, subscriber):
        """
        Constructs a dictionary that represents the parameters passed to the SMS InsertAlerts stored procedure
        :return: returns the sequence number of the subscriber and the constructed parameter dictionary
        """
        try:
            pub_seq_num = superdesk.get_resource_service(
                'subscribers').generate_sequence_number(subscriber)
            sms_message = article.get('sms_message',
                                      article.get('headline',
                                                  '')).replace('\'', '\'\'')

            odbc_item = {
                'Sequence': pub_seq_num,
                'Category': article.get('anpa_category', [{}])[0].get('qcode'),
                'Headline': sms_message,
                'Priority': map_priority(article.get('priority'))
            }

            body = self.append_body_footer(article)
            if article.get(EMBARGO):
                embargo = '{}{}'.format(
                    'Embargo Content. Timestamp: ',
                    get_utc_schedule(article, EMBARGO).isoformat())
                body = embargo + body

            if article[ITEM_TYPE] == CONTENT_TYPE.TEXT:
                body = BeautifulSoup(body, "html.parser").text

            odbc_item['StoryText'] = body.replace('\'',
                                                  '\'\'')  # @article_text
            odbc_item['ident'] = '0'

            return [(pub_seq_num, json.dumps(odbc_item))]
        except Exception as ex:
            raise FormatterError.AAPSMSFormatterError(ex, subscriber)
Exemplo n.º 7
0
    def format(self, article, subscriber):
        try:
            docs = []
            for category in article.get('anpa_category'):
                pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
                anpa = []

                # selector codes are only injected for those subscribers that are defined
                # in the mapper
                selectors = dict()
                SelectorcodeMapper().map(article, category.get('qcode').upper(),
                                         subscriber=subscriber,
                                         formatted_item=selectors)
                if 'selector_codes' in selectors and selectors['selector_codes']:
                    anpa.append(b'\x05')
                    anpa.append(selectors['selector_codes'].encode('ascii'))
                    anpa.append(b'\x0D\x0A')

                # start of message header (syn syn soh)
                anpa.append(b'\x16\x16\x01')
                anpa.append(article.get('service_level', 'a').lower().encode('ascii'))

                # story number
                anpa.append(str(pub_seq_num).zfill(4).encode('ascii'))

                # field seperator
                anpa.append(b'\x0A')  # -LF
                anpa.append(map_priority(article.get('priority')).encode('ascii'))
                anpa.append(b'\x20')

                anpa.append(category['qcode'].encode('ascii'))

                anpa.append(b'\x13')
                # format identifier
                if article[ITEM_TYPE] == CONTENT_TYPE.PREFORMATTED:
                    anpa.append(b'\x12')
                else:
                    anpa.append(b'\x11')
                anpa.append(b'\x20')

                # keyword
                keyword = 'bc-{}'.format(self.append_legal(article=article, truncate=True)).replace(' ', '-')
                keyword = keyword[:24] if len(keyword) > 24 else keyword
                anpa.append(keyword.encode('ascii'))
                anpa.append(b'\x20')

                # version field
                anpa.append(b'\x20')

                # reference field
                anpa.append(b'\x20')

                # filing date
                anpa.append('{}-{}'.format(article['_updated'].strftime('%m'),
                                           article['_updated'].strftime('%d')).encode('ascii'))
                anpa.append(b'\x20')

                # add the word count
                anpa.append(str(article.get('word_count', '0000')).zfill(4).encode('ascii'))
                anpa.append(b'\x0D\x0A')

                anpa.append(b'\x02')  # STX

                self._process_headline(anpa, article, category['qcode'].encode('ascii'))

                keyword = self.append_legal(article=article, truncate=True).encode('ascii', 'ignore')
                anpa.append(keyword)
                take_key = article.get('anpa_take_key', '').encode('ascii', 'ignore')
                anpa.append((b'\x20' + take_key) if len(take_key) > 0 else b'')
                anpa.append(b'\x0D\x0A')

                if BYLINE in article:
                    anpa.append(article.get(BYLINE).encode('ascii', 'ignore'))
                    anpa.append(b'\x0D\x0A')

                if article.get('dateline', {}).get('text'):
                    anpa.append(article.get('dateline').get('text').encode('ascii', 'ignore'))

                body = self.append_body_footer(article)
                if article.get(EMBARGO):
                    embargo = '{}{}'.format('Embargo Content. Timestamp: ',
                                            get_utc_schedule(article, EMBARGO).isoformat())
                    body = embargo + body

                if article[ITEM_TYPE] == CONTENT_TYPE.PREFORMATTED:
                    anpa.append(body.encode('ascii', 'replace'))
                else:
                    anpa.append(BeautifulSoup(body, "html.parser").text.encode('ascii', 'replace'))

                anpa.append(b'\x0D\x0A')
                if article.get('more_coming', False):
                    anpa.append('MORE'.encode('ascii'))
                else:
                    anpa.append(article.get('source', '').encode('ascii'))
                sign_off = article.get('sign_off', '').encode('ascii')
                anpa.append((b'\x20' + sign_off) if len(sign_off) > 0 else b'')
                anpa.append(b'\x0D\x0A')

                anpa.append(b'\x03')  # ETX

                # time and date
                anpa.append(datetime.datetime.now().strftime('%d-%m-%y %H-%M-%S').encode('ascii'))

                anpa.append(b'\x04')  # EOT
                anpa.append(b'\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A')

                docs.append((pub_seq_num, b''.join(anpa)))

            return docs
        except Exception as ex:
            raise FormatterError.AnpaFormatterError(ex, subscriber)
Exemplo n.º 8
0
    def format(self, article, subscriber):
        try:
            docs = []
            for category in article.get('anpa_category'):
                pub_seq_num = superdesk.get_resource_service(
                    'subscribers').generate_sequence_number(subscriber)
                anpa = []

                # selector codes are only injected for those subscribers that are defined
                # in the mapper
                selectors = dict()
                SelectorcodeMapper().map(article,
                                         category.get('qcode').upper(),
                                         subscriber=subscriber,
                                         formatted_item=selectors)
                if 'selector_codes' in selectors and selectors[
                        'selector_codes']:
                    anpa.append(b'\x05')
                    anpa.append(selectors['selector_codes'].encode('ascii'))
                    anpa.append(b'\x0D\x0A')

                # start of message header (syn syn soh)
                anpa.append(b'\x16\x16\x01')
                anpa.append(
                    article.get('service_level', 'a').lower().encode('ascii'))

                # story number
                anpa.append(str(pub_seq_num).zfill(4).encode('ascii'))

                # field seperator
                anpa.append(b'\x0A')  # -LF
                anpa.append(
                    map_priority(article.get('priority')).encode('ascii'))
                anpa.append(b'\x20')

                anpa.append(category['qcode'].encode('ascii'))

                anpa.append(b'\x13')
                # format identifier
                if article[ITEM_TYPE] == CONTENT_TYPE.PREFORMATTED:
                    anpa.append(b'\x12')
                else:
                    anpa.append(b'\x11')
                anpa.append(b'\x20')

                # keyword
                keyword = 'bc-{}'.format(
                    self.append_legal(article=article,
                                      truncate=True)).replace(' ', '-')
                keyword = keyword[:24] if len(keyword) > 24 else keyword
                anpa.append(keyword.encode('ascii'))
                anpa.append(b'\x20')

                # version field
                anpa.append(b'\x20')

                # reference field
                anpa.append(b'\x20')

                # filing date
                anpa.append('{}-{}'.format(
                    article['_updated'].strftime('%m'),
                    article['_updated'].strftime('%d')).encode('ascii'))
                anpa.append(b'\x20')

                # add the word count
                anpa.append(
                    str(article.get('word_count',
                                    '0000')).zfill(4).encode('ascii'))
                anpa.append(b'\x0D\x0A')

                anpa.append(b'\x02')  # STX

                self._process_headline(anpa, article,
                                       category['qcode'].encode('ascii'))

                keyword = self.append_legal(article=article,
                                            truncate=True).encode(
                                                'ascii', 'ignore')
                anpa.append(keyword)
                take_key = article.get('anpa_take_key',
                                       '').encode('ascii', 'ignore')
                anpa.append((b'\x20' + take_key) if len(take_key) > 0 else b'')
                anpa.append(b'\x0D\x0A')

                if BYLINE in article:
                    anpa.append(article.get(BYLINE).encode('ascii', 'ignore'))
                    anpa.append(b'\x0D\x0A')

                if article.get('dateline', {}).get('text'):
                    anpa.append(
                        article.get('dateline').get('text').encode(
                            'ascii', 'ignore'))

                body = self.append_body_footer(article)
                if article.get(EMBARGO):
                    embargo = '{}{}'.format(
                        'Embargo Content. Timestamp: ',
                        get_utc_schedule(article, EMBARGO).isoformat())
                    body = embargo + body

                if article[ITEM_TYPE] == CONTENT_TYPE.PREFORMATTED:
                    anpa.append(body.encode('ascii', 'replace'))
                else:
                    anpa.append(
                        BeautifulSoup(body, "html.parser").text.encode(
                            'ascii', 'replace'))

                anpa.append(b'\x0D\x0A')
                if article.get('more_coming', False):
                    anpa.append('MORE'.encode('ascii'))
                else:
                    anpa.append(article.get('source', '').encode('ascii'))
                sign_off = article.get('sign_off', '').encode('ascii')
                anpa.append((b'\x20' + sign_off) if len(sign_off) > 0 else b'')
                anpa.append(b'\x0D\x0A')

                anpa.append(b'\x03')  # ETX

                # time and date
                anpa.append(datetime.datetime.now().strftime(
                    '%d-%m-%y %H-%M-%S').encode('ascii'))

                anpa.append(b'\x04')  # EOT
                anpa.append(
                    b'\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A'
                )

                docs.append((pub_seq_num, b''.join(anpa)))

            return docs
        except Exception as ex:
            raise FormatterError.AnpaFormatterError(ex, subscriber)
Exemplo n.º 9
0
    def format(self, article, subscriber):
        """
        Constructs a dictionary that represents the parameters passed to the IPNews InsertNews stored procedure
        :return: returns the sequence number of the subscriber and the constructed parameter dictionary
        """
        try:
            docs = []
            for category in article.get('anpa_category'):
                pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
                odbc_item = {'originator': article.get('source', None), 'sequence': pub_seq_num,
                             'category': category.get('qcode'),
                             'headline': article.get('headline', '').replace('\'', '\'\''),
                             'author': article.get('byline', '').replace('\'', '\'\''),
                             'keyword': article.get('slugline', None).replace('\'', '\'\''),
                             'subject_reference': set_subject(category, article)}

                if 'subject_reference' in odbc_item and odbc_item['subject_reference'] is not None \
                        and odbc_item['subject_reference'] != '00000000':
                    odbc_item['subject'] = subject_codes[odbc_item['subject_reference'][:2] + '000000']
                    if odbc_item['subject_reference'][2:5] != '000':
                        odbc_item['subject_matter'] = subject_codes[odbc_item['subject_reference'][:5] + '000']
                    else:
                        odbc_item['subject_matter'] = ''
                    if not odbc_item['subject_reference'].endswith('000'):
                        odbc_item['subject_detail'] = subject_codes[odbc_item['subject_reference']]
                    else:
                        odbc_item['subject_detail'] = ''
                else:
                    odbc_item['subject_reference'] = '00000000'

                odbc_item['take_key'] = article.get('anpa_take_key', None)  # @take_key
                odbc_item['usn'] = article.get('unique_id', None)  # @usn
                if article[ITEM_TYPE] == CONTENT_TYPE.PREFORMATTED:
                    odbc_item['article_text'] = article.get('body_html', '').replace('\'', '\'\'')  # @article_text
                elif article[ITEM_TYPE] == CONTENT_TYPE.TEXT:
                    soup = BeautifulSoup(article.get('body_html', ''))
                    text = StringIO()
                    for p in soup.findAll('p'):
                        text.write('\x19\r\n')
                        ptext = p.get_text('\n')
                        for l in ptext.split('\n'):
                            if len(l) > 80:
                                text.write(textwrap.fill(l, 80).replace('\n', ' \r\n'))
                            else:
                                text.write(l + ' \r\n')
                    odbc_item['article_text'] = text.getvalue().replace('\'', '\'\'')

                if 'genre' in article:
                    odbc_item['genre'] = article['genre'][0].get('name', None)
                else:
                    odbc_item['genre'] = 'Current'  # @genre
                if article.get(ITEM_TYPE, CONTENT_TYPE.TEXT) == CONTENT_TYPE.TEXT:
                    odbc_item['texttab'] = 'x'
                elif article.get(ITEM_TYPE, None) == CONTENT_TYPE.PREFORMATTED:
                    odbc_item['texttab'] = 't'
                odbc_item['wordcount'] = article.get('word_count', None)  # @wordcount
                odbc_item['news_item_type'] = 'News'
                odbc_item['priority'] = map_priority(article.get('priority'))  # @priority
                odbc_item['service_level'] = 'a'  # @service_level
                odbc_item['fullStory'] = 1
                odbc_item['ident'] = '0'  # @ident

                SelectorcodeMapper().map(article, category.get('qcode').upper(),
                                         subscriber=subscriber,
                                         formatted_item=odbc_item)
                headline_prefix = LocatorMapper().map(article, category.get('qcode').upper())
                if headline_prefix:
                    odbc_item['headline'] = '{}:{}'.format(headline_prefix, odbc_item['headline'])

                if article.get(EMBARGO):
                    embargo = '{}{}'.format('Embargo Content. Timestamp: ', article.get(EMBARGO).isoformat())
                    odbc_item['article_text'] = embargo + odbc_item['article_text']

                docs.append((pub_seq_num, odbc_item))

            return docs
        except Exception as ex:
            raise FormatterError.AAPIpNewsFormatterError(ex, subscriber)
Exemplo n.º 10
0
    def format(self, article, subscriber):
        """
        Constructs a dictionary that represents the parameters passed to the IPNews InsertNews stored procedure
        :return: returns the sequence number of the subscriber and the constructed parameter dictionary
        """
        try:
            docs = []
            for category in article.get("anpa_category"):
                pub_seq_num = superdesk.get_resource_service("subscribers").generate_sequence_number(subscriber)
                odbc_item = {
                    "originator": article.get("source", None),
                    "sequence": pub_seq_num,
                    "category": category.get("qcode"),
                    "headline": article.get("headline", "").replace("'", "''"),
                    "author": article.get("byline", "").replace("'", "''"),
                    "keyword": self.append_legal(article=article, truncate=True).replace("'", "''"),
                    "subject_reference": set_subject(category, article),
                }

                if (
                    "subject_reference" in odbc_item
                    and odbc_item["subject_reference"] is not None
                    and odbc_item["subject_reference"] != "00000000"
                ):
                    odbc_item["subject"] = subject_codes[odbc_item["subject_reference"][:2] + "000000"]
                    if odbc_item["subject_reference"][2:5] != "000":
                        odbc_item["subject_matter"] = subject_codes[odbc_item["subject_reference"][:5] + "000"]
                    else:
                        odbc_item["subject_matter"] = ""
                    if not odbc_item["subject_reference"].endswith("000"):
                        odbc_item["subject_detail"] = subject_codes[odbc_item["subject_reference"]]
                    else:
                        odbc_item["subject_detail"] = ""
                else:
                    odbc_item["subject_reference"] = "00000000"

                odbc_item["take_key"] = article.get("anpa_take_key", None)  # @take_key
                odbc_item["usn"] = article.get("unique_id", None)  # @usn
                if article[ITEM_TYPE] == CONTENT_TYPE.PREFORMATTED:  # @article_text
                    odbc_item["article_text"] = self.append_body_footer(article).replace("'", "''")
                elif article[ITEM_TYPE] == CONTENT_TYPE.TEXT:
                    soup = BeautifulSoup(self.append_body_footer(article), "html.parser")
                    text = StringIO()
                    for p in soup.findAll("p"):
                        text.write("\x19\r\n")
                        ptext = p.get_text("\n")
                        for l in ptext.split("\n"):
                            if len(l) > 80:
                                text.write(textwrap.fill(l, 80).replace("\n", " \r\n"))
                            else:
                                text.write(l + " \r\n")
                    odbc_item["article_text"] = text.getvalue().replace("'", "''")

                if "genre" in article:
                    odbc_item["genre"] = article["genre"][0].get("name", None)
                else:
                    odbc_item["genre"] = "Current"  # @genre
                if article.get(ITEM_TYPE, CONTENT_TYPE.TEXT) == CONTENT_TYPE.TEXT:
                    odbc_item["texttab"] = "x"
                elif article.get(ITEM_TYPE, None) == CONTENT_TYPE.PREFORMATTED:
                    odbc_item["texttab"] = "t"
                odbc_item["wordcount"] = article.get("word_count", None)  # @wordcount
                odbc_item["news_item_type"] = "News"
                odbc_item["priority"] = map_priority(article.get("priority"))  # @priority
                odbc_item["service_level"] = "a"  # @service_level
                odbc_item["fullStory"] = 1
                odbc_item["ident"] = "0"  # @ident

                SelectorcodeMapper().map(
                    article, category.get("qcode").upper(), subscriber=subscriber, formatted_item=odbc_item
                )
                headline_prefix = LocatorMapper().map(article, category.get("qcode").upper())
                if headline_prefix:
                    odbc_item["headline"] = "{}:{}".format(headline_prefix, odbc_item["headline"])

                if article.get(EMBARGO):
                    embargo = "{}{}".format("Embargo Content. Timestamp: ", article.get(EMBARGO).isoformat())
                    odbc_item["article_text"] = embargo + odbc_item["article_text"]

                docs.append((pub_seq_num, odbc_item))

            return docs
        except Exception as ex:
            raise FormatterError.AAPIpNewsFormatterError(ex, subscriber)
Exemplo n.º 11
0
    def format(self, article, subscriber):
        try:
            docs = []
            for category in article.get("anpa_category"):
                pub_seq_num = superdesk.get_resource_service("subscribers").generate_sequence_number(subscriber)
                anpa = []

                # selector codes are only injected for those subscribers that are defined
                # in the mapper
                selectors = dict()
                SelectorcodeMapper().map(
                    article, category.get("qcode").upper(), subscriber=subscriber, formatted_item=selectors
                )
                if "selector_codes" in selectors and selectors["selector_codes"]:
                    anpa.append(b"\x05")
                    anpa.append(selectors["selector_codes"].encode("ascii"))
                    anpa.append(b"\x0D\x0A")

                # start of message header (syn syn soh)
                anpa.append(b"\x16\x16\x01")
                anpa.append(article.get("service_level", "a").lower().encode("ascii"))

                # story number
                anpa.append(str(pub_seq_num).zfill(4).encode("ascii"))

                # field seperator
                anpa.append(b"\x0A")  # -LF
                anpa.append(map_priority(article.get("priority")).encode("ascii"))
                anpa.append(b"\x20")

                anpa.append(category["qcode"].encode("ascii"))

                anpa.append(b"\x13")
                # format identifier
                if article[ITEM_TYPE] == CONTENT_TYPE.PREFORMATTED:
                    anpa.append(b"\x12")
                else:
                    anpa.append(b"\x11")
                anpa.append(b"\x20")

                # keyword
                keyword = "bc-{}".format(article.get("slugline", "")).replace(" ", "-")
                keyword = keyword[:24] if len(keyword) > 24 else keyword
                anpa.append(keyword.encode("ascii"))
                anpa.append(b"\x20")

                # version field
                anpa.append(b"\x20")

                # reference field
                anpa.append(b"\x20")

                # filing date
                anpa.append(
                    "{}-{}".format(article["_updated"].strftime("%m"), article["_updated"].strftime("%d")).encode(
                        "ascii"
                    )
                )
                anpa.append(b"\x20")

                # add the word count
                anpa.append(str(article.get("word_count", "0000")).zfill(4).encode("ascii"))
                anpa.append(b"\x0D\x0A")

                anpa.append(b"\x02")  # STX

                self._process_headline(anpa, article, category["qcode"].encode("ascii"))

                keyword = article.get("slugline", "").encode("ascii", "ignore")
                anpa.append(keyword)
                take_key = article.get("anpa_take_key", "").encode("ascii", "ignore")
                anpa.append((b"\x20" + take_key) if len(take_key) > 0 else b"")
                anpa.append(b"\x0D\x0A")

                if BYLINE in article:
                    anpa.append(article.get(BYLINE).encode("ascii", "ignore"))
                    anpa.append(b"\x0D\x0A")

                if "dateline" in article and "text" in article["dateline"]:
                    anpa.append(article.get("dateline").get("text").encode("ascii", "ignore"))

                body = self.append_body_footer(article)
                if article.get(EMBARGO):
                    embargo = "{}{}".format("Embargo Content. Timestamp: ", article.get(EMBARGO).isoformat())
                    body = embargo + body

                if article[ITEM_TYPE] == CONTENT_TYPE.PREFORMATTED:
                    anpa.append(body.encode("ascii", "replace"))
                else:
                    anpa.append(BeautifulSoup(body, "html.parser").text.encode("ascii", "replace"))

                anpa.append(b"\x0D\x0A")
                if article.get("more_coming", False):
                    anpa.append("MORE".encode("ascii"))
                else:
                    anpa.append(article.get("source", "").encode("ascii"))
                sign_off = article.get("sign_off", "").encode("ascii")
                anpa.append((b"\x20" + sign_off) if len(sign_off) > 0 else b"")
                anpa.append(b"\x0D\x0A")

                anpa.append(b"\x03")  # ETX

                # time and date
                anpa.append(datetime.datetime.now().strftime("%d-%m-%y %H-%M-%S").encode("ascii"))

                anpa.append(b"\x04")  # EOT
                anpa.append(b"\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A")

                docs.append((pub_seq_num, b"".join(anpa)))

            return docs
        except Exception as ex:
            raise FormatterError.AnpaFormatterError(ex, subscriber)
Exemplo n.º 12
0
    def format(self, article, subscriber):
        """
        Constructs a dictionary that represents the parameters passed to the IPNews InsertNews stored procedure
        :return: returns the sequence number of the subscriber and the constructed parameter dictionary
        """
        try:
            docs = []
            for category in article.get('anpa_category'):
                pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
                odbc_item = {'originator': article.get('source', None), 'sequence': pub_seq_num,
                             'category': category.get('qcode'),
                             'headline': article.get('headline', '').replace('\'', '\'\''),
                             'author': article.get('byline', '').replace('\'', '\'\''),
                             'keyword': self.append_legal(article=article, truncate=True).replace('\'', '\'\''),
                             'subject_reference': set_subject(category, article)}

                if 'subject_reference' in odbc_item and odbc_item['subject_reference'] is not None \
                        and odbc_item['subject_reference'] != '00000000':
                    odbc_item['subject'] = subject_codes[odbc_item['subject_reference'][:2] + '000000']
                    if odbc_item['subject_reference'][2:5] != '000':
                        odbc_item['subject_matter'] = subject_codes[odbc_item['subject_reference'][:5] + '000']
                    else:
                        odbc_item['subject_matter'] = ''
                    if not odbc_item['subject_reference'].endswith('000'):
                        odbc_item['subject_detail'] = subject_codes[odbc_item['subject_reference']]
                    else:
                        odbc_item['subject_detail'] = ''
                else:
                    odbc_item['subject_reference'] = '00000000'

                odbc_item['take_key'] = article.get('anpa_take_key', None)  # @take_key
                odbc_item['usn'] = article.get('unique_id', None)  # @usn
                if article[ITEM_TYPE] == CONTENT_TYPE.PREFORMATTED:  # @article_text
                    odbc_item['article_text'] = self.append_body_footer(article).replace('\'', '\'\'')
                elif article[ITEM_TYPE] == CONTENT_TYPE.TEXT:
                    soup = BeautifulSoup(self.append_body_footer(article), "html.parser")
                    text = StringIO()
                    for p in soup.findAll('p'):
                        text.write('\x19\r\n')
                        ptext = p.get_text('\n')
                        for l in ptext.split('\n'):
                            if len(l) > 80:
                                text.write(textwrap.fill(l, 80).replace('\n', ' \r\n'))
                            else:
                                text.write(l + ' \r\n')
                    odbc_item['article_text'] = text.getvalue().replace('\'', '\'\'')

                if 'genre' in article:
                    odbc_item['genre'] = article['genre'][0].get('name', None)
                else:
                    odbc_item['genre'] = 'Current'  # @genre
                if article.get(ITEM_TYPE, CONTENT_TYPE.TEXT) == CONTENT_TYPE.TEXT:
                    odbc_item['texttab'] = 'x'
                elif article.get(ITEM_TYPE, None) == CONTENT_TYPE.PREFORMATTED:
                    odbc_item['texttab'] = 't'
                odbc_item['wordcount'] = article.get('word_count', None)  # @wordcount
                odbc_item['news_item_type'] = 'News'
                odbc_item['priority'] = map_priority(article.get('priority'))  # @priority
                odbc_item['service_level'] = 'a'  # @service_level
                odbc_item['fullStory'] = 1
                odbc_item['ident'] = '0'  # @ident

                SelectorcodeMapper().map(article, category.get('qcode').upper(),
                                         subscriber=subscriber,
                                         formatted_item=odbc_item)
                headline_prefix = LocatorMapper().map(article, category.get('qcode').upper())
                if headline_prefix:
                    odbc_item['headline'] = '{}:{}'.format(headline_prefix, odbc_item['headline'])

                if article.get(EMBARGO):
                    embargo = '{}{}'.format('Embargo Content. Timestamp: ', article.get(EMBARGO).isoformat())
                    odbc_item['article_text'] = embargo + odbc_item['article_text']

                docs.append((pub_seq_num, odbc_item))

            return docs
        except Exception as ex:
            raise FormatterError.AAPIpNewsFormatterError(ex, subscriber)