class TableBuilderTests(unittest.TestCase):
    TITLE = 'title'
    TABLE_PATH = r'article.csv'
    OUR_TABLE_PATH = r'articles\{0}\article2.csv'
    IMG_FORMAT = '<a target="_blank" rel="nofollow" href="http://amzn.to/1F9ASJc"><img src={0} /></a>'

    def setUp(self):
        self.product_group = 'Shoes'
        self.keyword = 'Boots'
        self.seach = ProductSearcher(CONFIG)
        self.table_builder = TableBuilder(self.TITLE, URL, USER_NAME, PASSWORD)
        self.article_dir = self.table_builder.article_dir

    def test_build_sanity(self):
        products = self.seach.search(self.product_group, self.keyword)
        fieldnames = {}
        self.table_builder.build(products)
        our_table_path = self.OUR_TABLE_PATH.format(self.TITLE)
        with open(our_table_path, 'wb') as f:
            writer = csv.DictWriter(f, fieldnames=self.table_builder.fieldnames_list)
            writer.writeheader()
            for product in products:
                fieldnames['Picture'] = self.IMG_FORMAT.format(product.get_img_url('SmallImage'))
                fieldnames['Name'] = product.title
                fieldnames['Rating'] = product.get_rating()
                fieldnames['Price'] = product.get_price()
                writer.writerow(fieldnames)

        with open(path.join(self.article_dir, self.TABLE_PATH)) as f:
            content = f.read()

        with open(our_table_path) as f:
            out_content = f.read()

        self.assertTrue(content == out_content)
Ejemplo n.º 2
0
    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)
 def setUp(self):
     self.product_group = 'Shoes'
     self.keyword = 'Boots'
     self.seach = ProductSearcher(CONFIG)
     self.table_builder = TableBuilder(self.TITLE, URL, USER_NAME, PASSWORD)
     self.article_dir = self.table_builder.article_dir