コード例 #1
0
 def test_is_in_subject(self):
     article = {
         'subject': [{'qcode': '04001005'}, {'qcode': '15011002'}],
     }
     f = SelectorcodeMapper()
     self.assertTrue(f._is_in_subject(article, '150'))
     self.assertFalse(f._is_in_subject(article, '151'))
     self.assertTrue(f._is_in_subject(article, '04001'))
コード例 #2
0
 def test_is_in_subject(self):
     article = {
         'subject': [{
             'qcode': '04001005'
         }, {
             'qcode': '15011002'
         }],
     }
     f = SelectorcodeMapper()
     self.assertTrue(f._is_in_subject(article, '150'))
     self.assertFalse(f._is_in_subject(article, '151'))
     self.assertTrue(f._is_in_subject(article, '04001'))
コード例 #3
0
 def test_set_selector_codes(self):
     article = {
         'task': {'desk': 1},
         'slugline': 'Test',
         'urgency': 3
     }
     subscriber = {'name': 'ipnews'}
     f = SelectorcodeMapper()
     odbc_item = {}
     with self.app.app_context():
         f.map(article, 'A', subscriber=subscriber, formatted_item=odbc_item)
         self.assertSetEqual(set(odbc_item['selector_codes'].split()),
                             set('and axd pnd cxd 0fh 0ir 0px 0ah 0hw cxx axx cnd 0nl az pxd pxx'.split()))
コード例 #4
0
 def test_set_selector_codes(self):
     article = {'task': {'desk': 1}, 'slugline': 'Test', 'urgency': 3}
     subscriber = {'name': 'ipnews'}
     f = SelectorcodeMapper()
     odbc_item = {}
     with self.app.app_context():
         f.map(article,
               'A',
               subscriber=subscriber,
               formatted_item=odbc_item)
         self.assertSetEqual(
             set(odbc_item['selector_codes'].split()),
             set('and axd pnd cxd 0fh 0ir 0px 0ah 0hw cxx axx cnd 0nl az pxd pxx'
                 .split()))
コード例 #5
0
 def test_join_selector_codes(self):
     f = SelectorcodeMapper()
     result = f._join_selector_codes('ipnewS', 'newsi', 'cnewsi', 'cnewsi')
     result_list = result.split()
     self.assertEqual(len(result_list), 12)
コード例 #6
0
 def test_join_selector_codes(self):
     f = SelectorcodeMapper()
     result = f._join_selector_codes('ipnewS', 'newsi', 'cnewsi', 'cnewsi')
     result_list = result.split()
     self.assertEqual(len(result_list), 12)
コード例 #7
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)