Exemple #1
0
    def find(self, product):
        try:
            apiki_buscape = BP(applicationID=BUSCAPE_APP_ID)
            apiki_buscape.set_sandbox()

            product = self._remove_accents(product)

            bp_offers = apiki_buscape.find_offer_list(keyword=product.replace(' ',','), format='json')
            bp_offers = bp_offers.get('data')
            bp_offers = json.loads(bp_offers)

            offers = []
            for bp_offer in bp_offers['offer']:
                offer = {}
                offer['product'] = bp_offer['offer']['offername']
                offer['seller'] = bp_offer['offer']['seller']['sellername']
                offer['currency'] = bp_offer['offer']['price']['currency']['abbreviation']
                offer['price'] = bp_offer['offer']['price']['value']
                
                if 'thumbnail' in bp_offer['offer']:
                    offer['thumbnail'] = bp_offer['offer']['thumbnail']['url']

                for link in bp_offer['offer']['links']:
                    offer['url'] = link['link']['url']
                    break

                offers.append(offer)

            return offers

        except Exception as e:
            return []
class BuscapeTest(unittest.TestCase):
    def setUp(self):
        self.applicationID = '2b613573535a6d324874493d'
        self.b = Buscape(applicationID=self.applicationID)
        self.b.set_sandbox()

    def _get_code(self, resp):
        json_resp = json.loads(resp['data'])
        return json_resp['details']['code']

    def test_set_default_format(self):
        buscape = Buscape(self.applicationID)
        self.assertEqual(buscape.format, 'xml')
        buscape.set_default_format('json')
        self.assertEqual(buscape.format, 'json')

    def test_set_clientId(self):
        buscape = Buscape(self.applicationID)
        buscape.set_sandbox()
        buscape.set_clientIp('200.200.200.200')
        url = buscape.find_category_list(keyword='teste')['url']
        self.assertTrue('clientIp' in url)

    def assertRaisesMessage(self, excClass, message, callableObj, *args,
                            **kwargs):
        try:
            callableObj(*args, **kwargs)
        except excClass, e:

            if excClass == URLError:
                reason = e.reason
            elif excClass == HTTPError:
                reason = e.msg
            else:
                reason = e.message

            if message is not None and reason == message:
                return True
            else:
                raise (self.failureException, "\nMessage expected: %s \nMessag"
                       "e raised: %s" % (message, reason))
        else:
 def test_set_clientId(self):
     buscape = Buscape(self.applicationID)
     buscape.set_sandbox()
     buscape.set_clientIp('200.200.200.200')
     url = buscape.find_category_list(keyword='teste')['url']
     self.assertTrue('clientIp' in url)