示例#1
0
    def close_spider(self, spider):
        # print('day la')
        # print(self.variations)
        # return
        num_page = len(self.productItems)
        for page in range(num_page):
            num_item = len(self.productItems[page]['name'])
            for i in range(num_item):
                try:
                    new_Item = Item()
                    new_Item.title = self.productItems[page]['name'][i]
                    new_Item.image = self.productItems[page]['images'][i][
                        'path']
                    new_Item.category = self.productItems[page]['category'][i]
                    new_Item.price = self.productItems[page]['price'][i]
                    new_Item.slug = self.productItems[page]['slug'][i]
                    new_Item.description = self.productItems[page][
                        'description'][0]
                    new_Item.label = self.productItems[page]['label'][0]
                    new_Item.save()
                except Exception as e:
                    print(e)
                    pass

        num_variation = len(self.variations)
        for vari in range(num_variation):
            curr_vari = self.variations[vari]
            item = Item.objects.filter(title=curr_vari['itemName'][0]).first()
            if (item != None):
                if (item.description == ""):
                    item.description = curr_vari['description'][0]
                    item.save()

                try:
                    num_variationValue = len(curr_vari['value'])
                    new_variation = Variation()
                    new_variation.item = item
                    new_variation.name = curr_vari['variation'][0]
                    new_variation.save()
                except Exception as e:
                    print(e)
                    pass

                variation = Variation.objects.filter(
                    item=item, name=curr_vari['variation'][0]).first()
                for j in range(num_variationValue):
                    try:
                        new_variationValue = ItemVariation()
                        new_variationValue.variation = variation
                        new_variationValue.value = curr_vari['value'][j]
                        new_variationValue.attachment = curr_vari['images'][j][
                            'path'] if (len(curr_vari['images']) -
                                        1) >= j else ''
                        new_variationValue.save()
                    except Exception as e:
                        print(e)
                        pass
示例#2
0
    def handle(self, *args, **options):
        url = 'http://www.unisport.dk/api/sample/'
        
        # Read items from URL
        logging.info('Reading data...')
        request = urllib2.urlopen(url)
        items_str = request.read()
        items = ast.literal_eval(items_str)['latest']
        logging.info('Data evaluated.')
        logging.info('Now importing...')

        # Go through items
        for item_ in items:
            try:
                # Create item and save into Db
                item = Item()
                item.dbid = item_['id']
                item.price = float(item_['price'].replace(',','.'))
                item.delivery = item_['delivery']
                item.kid_adult = int(item_['kid_adult'])
                item.kids = int(item_['kids'])
                item.women = int(item_['women'])
                item.name = item_['name'].decode('unicode_escape')
                item.sizes = item_['sizes']
                item.url = item_['url']
                item.free_porto = int(item_['free_porto'])
                item.package = item_['package']
                item.price_old = float(item_['price_old'].replace(',','.'))
                item.img_url = item_['img_url']
                item.save()
                logging.info('Imported item %s' % item.dbid)
            except:
                logging.error('Failed to import %s' % item_['dbid'])

        # All done
        logging.info('All imported')
示例#3
0
def pull_vinted_products():
    # sleep(10)
    creds = {
        'login': settings.VINTED_LOGIN,
        'password': settings.VINTED_PASSWORD
    }
    session = Vinted(creds=creds)
    session.login().content.decode('utf-8')

    friend_id = '12813951'  # '36544471'
    print("Getting items for ", friend_id)
    i_member_info, i_all_items = session.get_items4member(friend_id)
    print("Got items from vinted", i_all_items)

    for item in i_all_items:
        an_item = Item()
        an_item.id = int(item["id"])
        an_item.title = item["title"]
        an_item.price = float(item["price_numeric"])
        an_item.discount_price = float(item["price_numeric"])
        an_item.category = 'OW'
        an_item.label = "P"
        an_item.slug = slugify("{}{}".format(an_item.title, an_item.id))

        url = item["photos"][0]['full_size_url']
        img_filename = "{}.jpg".format(an_item.id)  # (uuid.uuid4().hex)
        img_temp = NamedTemporaryFile(delete=True)
        img_temp.write(urlopen(url).read())
        img_temp.flush()
        an_item.image.save(img_filename, File(img_temp))

        an_item.save()

        print("Item {} processed.".format(an_item.title))

    return None