コード例 #1
0
 def test_something(self):
     item = {
         'title': 'Title!',
         'published': 'Mon, 25 Nov 2019 06:02:00 -0500',
         'link':
         'https://news.yahoo.com/donald-trump-chosen-one-sent-110225965.html',
         'description': 'DESCRIPTION...'
     }
     number = 0
     corrected_pack_of_alts = ['ALT', 'ALT']
     corrected_pack_of_images_links = ['LINK', 'LINK']
     main_source = 'https://news.yahoo.com/rss/'
     pack1, pack2 = getting_novelty(item, number,
                                    corrected_pack_of_images_links,
                                    corrected_pack_of_alts, main_source)
     self.assertEqual(
         pack1,
         Novelty(
             number_of_novelty=1,
             title_of_novelty='Title!',
             time_of_novelty='Mon, 25 Nov 2019 06:02:00 -0500',
             source_link=
             'https://news.yahoo.com/donald-trump-chosen-one-sent-110225965.html',
             description='DESCRIPTION...',
             images_links='LINK',
             alt_text='ALT',
             date_corrected='20191125',
             main_source='https://news.yahoo.com/rss/'))
     self.assertEqual(pack2, (
         1, 'Title!', 'Mon, 25 Nov 2019 06:02:00 -0500',
         'https://news.yahoo.com/donald-trump-chosen-one-sent-110225965.html',
         'DESCRIPTION...', 'LINK', 'ALT', '20191125',
         'https://news.yahoo.com/rss/'))
コード例 #2
0
 def test_right(self):
     self.assertIsNone(
         converting_to_html(Path("bla-bla"), [
             Novelty(
                 1, 'Title of', 'Date of', 'Link', 'Descript',
                 'http://l1.yimg.com/uu/api/res/1.2'
                 '/ksVTE6rKNuLy29FWWowIRw'
                 '--/YXBwaWQ9eXRhY2h5b247aD04Njt3'
                 'PTEzMDs-/https://media'
                 '.zenfs.com/en/the_independent_6'
                 '35/0928c8eca47092b771dabf2f16e88ed4', 'fdfd', 'g', 'h')
         ]))
コード例 #3
0
def getting_from_database_to_pack():
    """
    Getting news from database back to the pack to use them for our needs
    """
    logging.info("Getting news from DB!")
    pack_of_news = []
    with sqlite3.connect("database.db") as conn:
        cursor = conn.cursor()
        try:
            for item in cursor.execute("SELECT * FROM projects"):
                (number, title, date, source, description, im_links, alt, date_corr, main_source) = item
                number = len(pack_of_news) + 1
                novelty = Novelty(number, title, date, source, description, im_links, alt, date_corr, main_source)
                pack_of_news.append(novelty)
            logging.info("Got news from DB!")
        except sqlite3.OperationalError:
            print("Get some news first!")
    return pack_of_news
コード例 #4
0
def getting_novelty(item, number, corrected_pack_of_images_links, corrected_pack_of_alts, main_source):
    """
    It was really difficult to get images because of variety of templates how sites leave link for image
    Here we create object of Novelty class and fill it with our title, description and etc.
    Then if some problems with images or alt.text occur we use
    """

    try:
        alt_text = corrected_pack_of_alts[number]
    except IndexError:
        alt_text = 'No alternative text.'
    # Making a readable 'table' of class Novelty parameters
    number_of_novelty = number + 1
    title_of_novelty = unescape(item.get('title', ''))
    time_of_publishing = item.get('published', '')
    source_link = item.get('link', '')
    description = unescape(clean_html(item.get('description', '')))
    image_link = corrected_pack_of_images_links[number]
    alternative_text = unescape(alt_text)
    corrected_time = getting_corrected_time(item)
    main_link = main_source
    # creating Novelty class object
    novelty = Novelty(number_of_novelty,
                      title_of_novelty,
                      time_of_publishing,
                      source_link,
                      description,
                      image_link,
                      alternative_text,
                      corrected_time,
                      main_link)
    # creating a tuple from Novelty class object to put it in the database
    novelty_for_database = (novelty.number_of_novelty, novelty.title_of_novelty, novelty.time_of_novelty,
                            novelty.source_link, novelty.description, novelty.images_links,
                            novelty.alt_text, novelty.date_corrected, novelty.main_source)
    return novelty, novelty_for_database
コード例 #5
0
 def test_not_full_info(self):
     self.assertIsNone(
         converting_to_html(
             Path("bla-bla"),
             [Novelty(1, '', 'b', '', 'd', 'e', '', 'g', 'h')]))
コード例 #6
0
 def test_wrong_type(self):
     self.assertRaises(TypeError, Novelty('', '', '', '', '', '', '', '',
                                          ''))
コード例 #7
0
 def test_all_right(self):
     self.assertTrue(Novelty(1, '', '', '', '', '', '', '', ''))
コード例 #8
0
 def test_converting_if_novelty(self):
     pack_of_news = [
         Novelty(1, "AAA", '12:04:2020', 'Bla-bla-bla', 'abcdefgh', 'link',
                 'alt_text', '20200412', 'a')
     ]
     self.assertTrue(converting_to_json(pack_of_news))
コード例 #9
0
 def test_converting_if_none(self):
     pack_of_news = [
         Novelty(None, "AAA", '12:04:2020', 'Bla-bla-bla', 'abcdefgh',
                 '123', 'alt_text', '20212', 'a')
     ]
     self.assertRaises(TypeError, converting_to_json(pack_of_news))
コード例 #10
0
 def test_converting_if_integers(self):
     pack_of_news = [
         Novelty(1, "AAA", '12:04:2020', 'Bla-bla-bla', 'abcdefgh', 123,
                 'alt_text', 20200412, 'a')
     ]
     self.assertRaises(TypeError, converting_to_json(pack_of_news))
コード例 #11
0
 def test_getting_not_correct_item(self):
     item = Novelty(1, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
     novelty = f"\n{1}.\nTitle: {'a'}\nDate: {'b'}\nLink: {'c'}\nDescription:\n {'d'}\nImages links:{'e'}\n" \
               f"Alternative text:{'f'}\n Main source: {'h'}"
     self.assertNotEqual(getting_info_into_file(item), novelty, 'Should be')
コード例 #12
0
 def test_getting_correct_item(self):
     item = Novelty(1, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
     novelty = f"\n{1}.\nTitle: '{'a'}'\nDate: {'b'}\nLink: '{'c'}'\nDescription:\n '{'d'}'\n" \
               f"Images links:'{'e'}'\nAlternative text:'{'f'}'\n Main source: {'h'}"
     self.assertRaises(AssertionError and TypeError,
                       getting_info_into_file(item), novelty)