Beispiel #1
0
def request_gtin(descriptions, brand):

    response = None

    try:
        for description in descriptions:

            url = "https://cosmos.bluesoft.com.br/pesquisar?utf8=%E2%9C%93&q={0}"
            url = url.format(description)

            # Realiza a mesma requisição no máximo 5x
            count_request = 0
            while count_request < 5:
                response = requests.get(url)
                if response.status_code == 200:
                    break
                count_request += 1

            gtin = None
            try:

                soup = BeautifulSoup(response.content,
                                     "lxml",
                                     from_encoding="utf8")
                r = soup.find('body').find('div', id='container-principal')
                r = r.find('section',
                           class_='col-md-6 col-lg-6 col-xs-12 main')
                r = r.find('div',
                           id='results').find('div',
                                              class_='list-group').find('ul')
                r = r.find('li', class_='product-list-item item').find(
                    'div', class_='content')

                # Verificando se o produto é da mesma marca
                title = r.find('h5').find('a').get_text().lower()
                if brand.lower() in title or 'moto' in title:
                    # Getting o GTIN
                    gtin = r.find('ul').find_all('a')[-1].get_text()
                    gtin = gtin.zfill(14)
                    break

            except:
                pass
    except:
        pass

    if response is not None and response.status_code != 200:
        print("WARNING: GTIN site status_code = {0}".format(
            response.status_code))
        print("URL: ", url, "Product description", descriptions[0])

    if gtin is None:
        IOUtil.save_log("WARNING: GTIN not found = ({0}, {1})".format(
            brand, descriptions[0]))

    return gtin
    def find_offer(self, item, list_of_item, field="id"):
        def _p(s):
            return s.strip().lower()

        # Lisa para armazenar os resultados
        result_find = []
        
        # Faz a busca do item
        for i in list_of_item:
            s_1 = _p(i[field])
            s_2 = _p(item[field])
            if (s_1 in s_2) or (s_2 in s_1):
                result_find.append( i )
                IOUtil.save_log("Olha isso: " + s_1 + ", " + s_2)
        
        # Se mais de um produto foi encontrado, então tem alguma coisa errada
        if len(result_find) > 1:
            IOUtil.save_log("BUG: more than one product {0} with the same {1}: ".format( len(result_find), field ) + item['id'])
        
        # Retorna o produto
        if len(result_find) == 0: return None
        return result_find[0] 
 def process_delete(self, offers):
     for offer in offers:
         try:
             os.remove(local_folder + image_folder + "/" + str(offer['id']) + ".jpg")
         except:
             IOUtil.save_log("WARNING: " + str(offer['id']) + ".jpg" + " not found.")