def db_handler(self, id, data):
        session = self.session_maker()
        db_data = Data()
        db_data.id = id
        db_data.tender_price = self._tender_price_handler(data['tender_price'])
        db_data.type = data['type']
        db_data.tender_date = self._tender_date_handler(data['tender_date'])
        db_data.tender_object = data['tender_object']
        db_data.customer = data['customer']
        db_data.tender_adress = data['tender_adress']
        db_data.tender_delivery = data['tender_delivery']
        db_data.tender_terms = data['tender_term']
        db_data.document_links = '\n'.join(data['document_links'])
        db_data.tender_link = data['link']

        winner = Winners()
        winner.data_id = id
        winner.name = data['tender_winner'][0]
        winner.position = data['tender_winner'][1]
        winner.price = data['tender_winner'][2]
        db_data.winner.append(winner)

        for i in data['tender_object_info'][1:]:
            if len(i) == 6:
                data = Objects()
                data.position = i[0]
                data.name = i[1]
                data.unit = i[2]
                data.quantity = i[3]
                data.unit_price = i[4]
                data.price = i[5]
                db_data.objects.append(data)
        
        session.add(db_data)
        session.commit()
        db_data.winner = [winner]
        session.close()
        return db_data
Exemple #2
0
    async def parse_ea44(self, session: aiohttp.ClientSession,
                         order: Data) -> Data:
        """ парсит данные одной закупки """

        # проверка на наличие записи в базе
        data = self.db_session.query(Data).filter(Data.id == order.id).all()
        if data:
            return data[0]

        text = await self._get_request(session, order.tender_link)

        order_document = document_fromstring(text)
        # парсим главную информацию о закупке - номер цена заказчик дата

        card_info_container = order_document.cssselect('.cardMainInfo')[0]
        tender_id = card_info_container.cssselect(
            '.cardMainInfo__purchaseLink')

        if not tender_id:
            tender_id = ''
        else:
            tender_id = self._normalizer(tender_id[0].text_content())

        tender_object = card_info_container.xpath(
            './div[1]/div[2]/div[1]/span[2]')
        if not tender_object:
            tender_object = ''
        else:
            tender_object = self._normalizer(tender_object[0].text_content())

        customer = card_info_container.xpath('./div[1]/div[2]/div[2]/span[2]')
        if not customer:
            customer = ''
        else:
            customer = self._normalizer(customer[0].text_content())

        tender_price = card_info_container.cssselect('.cost')
        if not tender_price:
            tender_price = ''
        else:
            tender_price = self._normalizer(tender_price[0].text_content())

        tender_date = card_info_container.xpath(
            './div[2]/div[2]/div[1]/div[1]/span[2]')
        if not tender_date:
            tender_date = ''
        else:
            tender_date = self._normalizer(tender_date[0].text_content())

        # общая информация о закупке - адресс электронной площадки и обьект закупки
        general_information_container = order_document.xpath(
            '//div[@class="wrapper"]/div[2]')

        tender_adress = general_information_container[0].xpath(
            './/div[@class="col"]/section[3]/span[2]')
        if not tender_adress:
            tender_adress = ''
        else:
            tender_adress = self._normalizer(tender_adress[0].text_content())

        # условия контракта
        condition_container = self.get_cotract_conditions_container(
            order_document.xpath('//div[@id="custReqNoticeTable"]/div'))
        if condition_container is not None:
            tender_delivery_adress = condition_container.xpath(
                './/div[@class="col"]/section[2]/span[2]')
            if not tender_delivery_adress:
                tender_delivery_adress = ''
            else:
                tender_delivery_adress = self._normalizer(
                    tender_delivery_adress[0].text_content())

            tender_term = condition_container.xpath(
                './/div[@class="row"]/section[3]/span[2]')
            if not tender_term:
                tender_term = ''
            else:
                tender_term = self._normalizer(tender_term[0].text_content())
        else:
            tender_delivery_adress = ''
            tender_term = ''

        # парсинг информации о обьекте закупки
        tender_object_info = self.parse_tender_object_info(order_document)

        # парсинг победителя
        try:
            winner = await self.parse_tender_winner(session, order.tender_link)
        except Exception:
            winner = []
        if len(winner) < 3:
            winner = ['', '', '']

        # парсинг ссылок документов
        term_document_link = order.tender_link.replace('common-info',
                                                       'documents')
        term_document_data = await self._get_request(session,
                                                     term_document_link)
        term_document_links = document_fromstring(term_document_data).xpath(
            '//span[@class="section__value"]/a[@title]/@href')
        order.tender_object = tender_object
        order.customer = customer
        order.tender_price = self._tender_price_handler(tender_price)
        order.tender_date = self._tender_date_handler(tender_date)
        order.tender_adress = tender_adress
        order.tender_delivery = tender_delivery_adress
        order.tender_term = tender_term
        for object_info in self._handle_tender_objects(tender_object_info):
            order.objects.append(object_info)
        for document_link_data in term_document_links:
            tender_link = TenderLinks()
            tender_link.link = document_link_data
            tender_link.data_id = order.id
            order.document_links.append(tender_link)
        order.winner.append(
            Winners(name=winner[0], position=winner[1], price=winner[2]))
        order.type = 'fz44'
        return order