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
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