def _upload_article(self, keyword):
        """
        Get a keyword and uploads an article.
        :param keyword:
        """
        keyword, browse_node = re.findall('(.*)(?:\?bn=(\d+)?)', keyword)[0]
        products = self.product_searcher.search(config.PRODUCT_GROUP, keyword, browse_node=browse_node)
        if len(products) < self.MIN_PRODUCTS:
            return

        article_builder = ArticleBuilder(keyword, products)
        title = article_builder.get_title()
        table_builder = TableBuilder(title, config.URL, config.USER_NAME,
                                     config.PASSWORD)

        table_id = table_builder.build(products)
        wordpress_uploader = WordPressUploader(title, config.URL,
                                               config.USER_NAME, config.PASSWORD)

        content = article_builder.build(table_id)
        # Chose The size of the main image
        main_image_url = products[0].get_img_url('LargeImage')
        main_image_url = main_image_url if main_image_url != 'null' else products[0].get_img_url()
        categories = products[0].get_categories()
        wordpress_uploader.upload_article(content, main_image_url,
                                          article_builder.get_tags(), categories)
class ArticleBuilderTests(unittest.TestCase):
    def setUp(self):
        self.keyword = 'boots'
        self.product_searcher = ProductSearcher(config.CONFIG)
        self.products = self.product_searcher.search(config.PRODUCT_GROUP, self.keyword)
        self.article_builder = ArticleBuilder(self.keyword, self.products)

    def test_title_sanity(self):
        our_title = "The Best {0}".format(self.keyword.title())
        title = self.article_builder.get_title()
        self.assertTrue(title == our_title)

    def test_get_tags_sanity(self):
        our_tags = []
        for product in self.products:
            if product.manufacturer != 'null':
                our_tags.append(product.manufacturer)

        tags = self.article_builder.get_tags()
        self.assertTrue(tags == list(set(our_tags)))

    def test_build_sanity(self):
        table_id = 5
        article = self.article_builder.build(table_id)
        our_article = TABLE_FORMAT.format(table_id)

        for index, product in enumerate(self.products):
            shorten_url = common.get_short_url(product.page_url)
            alignment = CLASS_FORMAT.format('alignleft' if index % 2 == 0 else 'alignright')
            title = TITLE_FORMAT.format(shorten_url, product.title)
            img = IMG_FORMAT.format(shorten_url, alignment, product.get_img_url('LargeImage'), product.title)
            review = REVIEW_FORMAT.format(product.get_review())
            our_article += PRODUCT_FORMAT.format(title, img, review)

        self.assertTrue(our_article == article)
 def setUp(self):
     self.keyword = 'boots'
     self.product_searcher = ProductSearcher(config.CONFIG)
     self.products = self.product_searcher.search(config.PRODUCT_GROUP, self.keyword)
     self.article_builder = ArticleBuilder(self.keyword, self.products)