Esempio n. 1
0
    def post(self, request, *args, **kwargs):
        api_logger.info('UserTriggersApi post', extra={'data': request.data})

        serializer = serializers_request.UserTriggersSerializer(
            data=request.data)
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data

        api_logger.info('Adding Trigger for user ID %d' % request.user.id)

        trigger = Triggers()
        trigger.user = request.user
        trigger.auction_id = data['auction_id']
        trigger.time = reverse_key_val_choice_from_tuple(
            Triggers.TIMES, data['time'])
        trigger.mode = reverse_key_val_choice_from_tuple(
            Triggers.MODES, data['mode'])
        trigger.mode_value = data['mode_value']
        trigger.provider = reverse_key_val_choice_from_tuple(
            settings.PROVIDERS, data['provider'])
        trigger.save()

        TaskScheduler.schedule('ac_engine.tasks.TriggerTask',
                               minutes=trigger.task_interval,
                               task_name=trigger.task_name,
                               kwargs=json.dumps({'trigger_id': trigger.id}))

        return Response({'triggerId': trigger.id}, 201)
Esempio n. 2
0
    def _get_items_detailed(self, ids):
        # using python threads
        from multiprocessing.pool import ThreadPool
        # thread emulation using processes
        # from multiprocessing.dummy import Pool as ThreadPool

        pool_size = int(ceil(len(ids) / 25.0))
        pool_size = pool_size if pool_size < 8 else 8
        pool = ThreadPool(pool_size)

        api_logger.info('THREAD POOL SIZE: %d' % pool_size)

        def _get_data(idz):

            ids_soap = self.c.factory.create('ArrayOfLong')
            ids_soap.item = idz

            tmp = self.c.service.doGetItemsInfo(
                sessionHandle=self.token,
                itemsIdArray=ids_soap,
                getDesc=0,
                getImageUrl=0,
                getAttribs=1,
                getPostageOptions=1,
                getCompanyInfo=0,
            )

            return tmp.arrayItemListInfo.item

        items = pool.map(_get_data, chunks(ids, 25))
        return list(itertools.chain.from_iterable(items))
Esempio n. 3
0
 def delete(self, request, *args, **kwargs):
     api_logger.info('UserTriggersApi delete', extra={'kwargs': kwargs})
     if 'trigger_id' not in kwargs:
         raise exceptions.NotFound()
     TaskScheduler.terminate_by_task_name(
         Triggers.objects.get(id=kwargs.get('trigger_id')).task_name)
     Triggers.objects.filter(id=kwargs.get('trigger_id')).delete()
     return Response(status=200)
Esempio n. 4
0
 def get(self, request):
     api_logger.info('UserAuctionsApi called',
                     extra={'user_id': request.user.id})
     auctions = []
     if request.user.allegro_user_name:
         from ac_engine_allegro.data.data_grabber import DataGrabber as Allegro
         from ac_engine_ebay.data.data_grabber import DataGrabber as Ebay
         a = Allegro()
         auctions = a.get_user_auctions(request.user.allegro_user_name)
     return Response(auctions)
Esempio n. 5
0
    def post(self, request):
        api_logger.info('ComputingComputeApi', extra={'data': request.data})
        serializer = serializers_request.ComputingComputeSerializer(
            data=request.data)
        serializer.is_valid(raise_exception=True)
        stat_logger.info('Running: %s @ %s' %
                         (serializer.validated_data['actions'],
                          serializer.validated_data['sources']))

        task = ComputingTask().delay(input_data=serializer.validated_data,
                                     user_id=request.user.id)
        return Response({'computingHash': task.task_id})
Esempio n. 6
0
    def get_user_auctions(self, user_name):

        user_id = self.get_user_id(user_name)

        sort_options = self.c.factory.create('SortOptionsType')
        sort_options.sortType = 'endingTime'
        sort_options.sortOrder = 'asc'
        filter_options = self.c.factory.create('ArrayOfFilteroptionstype')

        opt = self.c.factory.create('FilterOptionsType')
        opt.filterId = 'userId'
        opt.filterValueId = self.c.factory.create('ArrayOfString')
        opt.filterValueId.item.append(str(user_id))
        filter_options.item.append(opt)

        items = self.c.service.doGetItemsList(
            webapiKey=settings.ALLEGRO_KEY,
            countryId=settings.ALLEGRO_COUNTRY,
            resultSize=120,
            resultScope=2,
            sortOptions=sort_options,
            filterOptions=filter_options)

        if not items.itemsCount and not items.itemsFeaturedCount:
            return []

        ids = [item.itemId for item in items.itemsList.item]
        api_logger.info("get_user_auctions ID's: %s" % str(ids))

        items_detailed = self._get_items_detailed(ids)

        objs = []

        for item in items_detailed:
            cats = item.itemCats.item

            objs.append({
                'id':
                item.itemInfo.itId,
                'name':
                item.itemInfo.itName,
                'categoryId':
                cats[-2].catId if len(cats) > 1 else cats[-1].catId,
                'categoryName':
                cats[-2].catName + " w kategorii " +
                cats[-1].catName if len(cats) > 1 else cats[-1].catName,
                'url':
                "http://allegro.pl/hello-allegro-i{item_id}.html".format(
                    item_id=item.itemInfo.itId)
            })

        return objs
Esempio n. 7
0
    def get(self, request, *args, **kwargs):
        api_logger.info('UserTriggersApi get', extra={'kwargs': kwargs})
        if kwargs.get('trigger_id'):
            try:
                result = Triggers.objects.get(pk=kwargs['trigger_id'])
                return Response(TriggerSerializer(result).data)

            except Triggers.DoesNotExist:
                raise exceptions.NotFound()
        else:
            results = Triggers.objects.filter(user=request.user.id,
                                              error=False)
            return Response(
                {'triggers': TriggerListSerializer(results, many=True).data})
Esempio n. 8
0
    def post(self, request):
        api_logger.info('ComputingPollingApi', extra={'data': request.data})

        serializer = serializers_request.ComputingPollingApiSerializer(
            data=request.data)
        serializer.is_valid(raise_exception=True)
        try:
            result = Results.objects.get(
                user=request.user.id,
                computing_hash=serializer.validated_data['computingHash'])
        except ObjectDoesNotExist:
            return Response(status=204)

        if result.error:
            return Response(status=500)

        return Response(PollingSerializer(result).data)
Esempio n. 9
0
    def put(self, request):
        api_logger.info('UserDataApi', extra={'data': request.data})

        serializer = serializers_request.UserDataSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data

        for f in ('first_name', 'last_name'):
            if data[f]:
                setattr(request.user, f, data[f])

        if data['allegro_login']:
            from ac_engine_allegro.data.data_grabber import DataGrabber
            request.user.allegro_user_name = data["allegro_login"]
            request.user.allegro_user_id = DataGrabber().get_user_id(
                data["allegro_login"], fresh=True)

        if data['ebay_login']:
            request.user.ebay_user_name = data["ebay_login"]
            request.user.ebay_user_id = 1  # TODO ebay

        request.user.save()
        return Response(status=200)
Esempio n. 10
0
    def search(self,
               name="",
               quantity=100,
               finished=False,
               buy_now_only=False,
               category_id=None,
               **kwargs):

        from time import time
        time_start = time()

        items = self._get_items_general(quantity, category_id, name,
                                        buy_now_only, finished, **kwargs)
        ids = [item.itemId for item in items]

        api_logger.info(str(ids))

        objects = []

        if self.ADVANCED_MODE:
            items_detailed = self._get_items_detailed(ids)

            for item in items_detailed:
                objects.append(DataContainerDetailed(item))
        else:
            items_detailed = items

            for item in items_detailed:
                objects.append(DataContainerSimple(item))

        api_logger.info(
            "ITEMS AFTER FILTRATION: {} (from: {}) TYPE: {}".format(
                len(items_detailed), quantity,
                'historic' if finished else 'current'))

        if objects[-1] is None:
            raise AllecenaException(
                'Something is seriously wrong with introductory data processing'
            )

        api_logger.info('Time spent on downloading {} items: {}s\n'.format(
            quantity, round(time() - time_start, 2)))

        return objects