Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import requests
- import datetime
- from xml.etree import ElementTree
- from django.core.management.base import BaseCommand, CommandError
- from django.conf import settings
- from django.utils import timezone
- from parsers.models import CategoryTng, ProductTng, ParserLog
- from settings.models import Pricing
- from goods.models import Provider
- url = "http://www.tngtoys.ru/Go/ProductData?branch_id=138"
- url_rossosh = "http://www.tngtoys.ru/prices/PriceRossosh.xml"
- class Command(BaseCommand):
- def get_barcodes(self, tree):
- barcodes = []
- for rossosh in tree.iter('product'):
- rossosh_barcode = rossosh.find('ShtrihKod').text
- barcodes.append(rossosh_barcode)
- return barcodes
- def download_file(self, url):
- local_filename = os.path.join(settings.ROOT_DIR, 'ostatki_tng.xml')
- r = requests.get(url, stream=True)
- with open(local_filename, 'wb') as f:
- for chunk in r.iter_content(chunk_size=1024):
- if chunk:
- f.write(chunk)
- return local_filename
- def get_categories(self, tree):
- categories = []
- for cat in tree.iter('category'):
- cat_id = cat.get('id')
- cat_name = cat.text
- categories.append({
- 'id': cat_id,
- 'name': cat_name
- })
- return categories
- def get_offers(self, tree):
- products = []
- for offer in tree.iter('offer'):
- offer_id = offer.attrib['id']
- offer_price = offer.find('price').text
- offer_currency = offer.find('currencyId').text
- offer_category = offer.find('categoryId').text
- offer_picture = offer.find('picture').text
- offer_name = offer.find('name').text
- offer_description = offer.find('description').text
- offer_ref = offer.find('ref').text
- offer_code = offer.find('code').text
- offer_manufacturer = offer.find('manufacturer').text
- offer_weight = offer.find('weight').text
- offer_size_x = offer.find('size_x').text
- offer_size_y = offer.find('size_y').text
- offer_size_z = offer.find('size_z').text
- offer_volume = offer.find('volume').text
- offer_new = offer.find('new').text
- offer_hit = offer.find('hit').text
- offer_action = offer.find('action').text
- offer_sale = offer.find('sale').text
- offer_in_box = offer.find('in_box').text
- offer_in_carton = offer.find('in_carton').text
- offer_stock = offer.find('stock').text
- offer_nds = offer.find('nds').text
- offer_metka = offer.find('metka').text
- offer_shtrihkod = offer.find('ShtrihKod').text
- offer_country = offer.find('country').text
- offer_nomengroup = offer.find('NomenGroup').text
- products.append({
- 'offer_id': offer_id,
- 'price': offer_price,
- 'price_opt': offer_price,
- 'currency': offer_currency,
- 'category': offer_category,
- 'picture': offer_picture,
- 'name': offer_name,
- 'description': offer_description,
- 'ref': offer_ref,
- 'code': offer_code,
- 'manufacturer': offer_manufacturer,
- 'weight': offer_weight,
- 'size_x': offer_size_x,
- 'size_y': offer_size_y,
- 'size_z': offer_size_z,
- 'volume': offer_volume,
- 'new': offer_new,
- 'hit': offer_hit,
- 'action': offer_action,
- 'sale': offer_sale,
- 'in_box': offer_in_box,
- 'in_carton': offer_in_carton,
- 'stock': offer_stock,
- 'nds': offer_nds,
- 'metka': offer_metka,
- 'shtrihkod': offer_shtrihkod,
- 'country': offer_country,
- 'nomengroup': offer_nomengroup,
- })
- return products
- def get_price_mrc(self, current_price):
- tng_provider = Provider.objects.filter(code='TG').first()
- current_pricing = Pricing.objects.filter(price_from__lt=current_price, price_to__gt=current_price,
- provider__id=tng_provider.id).first()
- if current_pricing:
- current_coef = current_pricing.coef
- price_mrc = float(current_price) * current_coef
- else:
- price_mrc = None
- if price_mrc:
- return int(price_mrc)
- else:
- return 0
- def handle(self, *args, **options):
- parser_log = ParserLog()
- parser_log.name = "Запустили скрипт tng_parser"
- parser_log.content = "Инициализация"
- parser_log.save()
- response = requests.get(url, verify=False)
- tree = ElementTree.fromstring(response.content)
- response_rossosh = requests.get(url_rossosh, verify=False)
- tree_rossosh = ElementTree.fromstring(response_rossosh.content)
- _root, created = CategoryTng.objects.get_or_create(name='_root')
- zero_category, z_creat = CategoryTng.objects.get_or_create(name='Без категории', category_id=0, parent=_root)
- print(datetime.datetime.now())
- # self.download_file(url)
- # print('Скачивание завершено')
- products = self.get_offers(tree)
- categories = self.get_categories(tree)
- barcodes = self.get_barcodes(tree_rossosh)
- # brands = []
- # Обнуляем остатки
- ProductTng.objects.all().update(count_moscow=0, count_spb=0)
- for cat in categories:
- ext_category = CategoryTng.objects.filter(category_id=cat['id']).first()
- if not ext_category:
- category = CategoryTng()
- category.category_id = cat['id']
- category.name = cat['name']
- category.parent = _root
- category.save()
- else:
- pass
- for prod in products:
- current_category = int(prod['category'])
- current_barcode = prod['shtrihkod']
- category = CategoryTng.objects.filter(category_id=current_category).first()
- product_tng = ProductTng.objects.filter(barcode=prod['shtrihkod']).first()
- if current_barcode in barcodes:
- # brands.append(prod['manufacturer'])
- try:
- stock_int = int(prod['stock'])
- if not product_tng:
- product = ProductTng()
- product.offer_id = prod['offer_id']
- product.price = prod['price']
- product.price_opt = prod['price']
- product.currency = prod['currency']
- product.image = prod['picture']
- product.name = prod['name']
- product.description = prod['description']
- product.artikul = prod['ref']
- product.code = prod['code']
- product.manufacturer = prod['manufacturer']
- product.weight = prod['weight']
- product.size_x = prod['size_x']
- product.size_y = prod['size_y']
- product.size_z = prod['size_z']
- product.volume = prod['volume']
- product.new = prod['new']
- product.hit = prod['hit']
- product.action = prod['action']
- product.sale = prod['sale']
- product.in_box = prod['in_box']
- product.in_carton = prod['in_carton']
- # product.stock = prod['stock'] if prod['stock'] else 0
- product.count_moscow = stock_int if stock_int > 0 else 0
- product.nds = prod['nds']
- product.metka = prod['metka']
- product.barcode = prod['shtrihkod']
- product.country = prod['country']
- product.nomengroup = prod['nomengroup']
- current_price = prod['price']
- if current_price:
- price_mrc = round(self.get_price_mrc(current_price), -1)
- if price_mrc:
- product.price_mrc = price_mrc
- product.category = category if category else zero_category
- product.save()
- product.product_id = 't' + str(product.id)
- product.save()
- else:
- product_tng.offer_id = prod['offer_id']
- product_tng.price = prod['price']
- product_tng.price_opt = prod['price']
- product_tng.currency = prod['currency']
- product_tng.image = prod['picture']
- product_tng.name = prod['name']
- product_tng.description = prod['description']
- product_tng.artikul = prod['ref']
- product_tng.code = prod['code']
- product_tng.manufacturer = prod['manufacturer']
- product_tng.weight = prod['weight']
- product_tng.size_x = prod['size_x']
- product_tng.size_y = prod['size_y']
- product_tng.size_z = prod['size_z']
- product_tng.volume = prod['volume']
- product_tng.new = prod['new']
- product_tng.hit = prod['hit']
- product_tng.action = prod['action']
- product_tng.sale = prod['sale']
- product_tng.in_box = prod['in_box']
- product_tng.in_carton = prod['in_carton']
- product_tng.count_moscow = stock_int if stock_int > 0 else 0
- product_tng.nds = prod['nds']
- product_tng.metka = prod['metka']
- product_tng.barcode = prod['shtrihkod']
- product_tng.country = prod['country']
- product_tng.nomengroup = prod['nomengroup']
- current_price = prod['price']
- if current_price:
- price_mrc = round(self.get_price_mrc(current_price), -1)
- if price_mrc:
- product_tng.price_mrc = price_mrc
- product_tng.category = category if category else zero_category
- product_tng.save()
- product_tng.product_id = 't' + str(product_tng.id)
- product_tng.save()
- except Exception as e:
- print(str(e))
- print(prod['stock'])
- print(prod['shtrihkod'])
- print(type(product_tng))
- empty_mrc = ProductTng.objects.filter(price_mrc=None).all()
- for e in empty_mrc:
- e.price_mrc = round(self.get_price_mrc(e.price_opt), -1)
- print('пустая МРЦ заполнена' + str(e.id))
- e.save()
- print(datetime.datetime.now())
- print("Скрипт tng_parser завершил свою работу")
- parser_log.date_submitted = timezone.now()
- parser_log.content = 'Скрипт tng_parser завершил свою работу'
- parser_log.save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement