Esempio n. 1
0
 def test_czy_dobry_url(self, mmock):
     NOKAUT_KEY = 'A_KEY'
     NOKAUT_KEY_WORD = 'A_NAME'
     stream = mock.MagicMock()
     mmock.return_value = stream
     stream.read.return_value = EXAMPLE_RESPONSE
     nokaut_api(NOKAUT_KEY, NOKAUT_KEY_WORD)
     do_parsowania = mmock.call_args[0]
     do_parsowania = urlparse.urlparse((str(do_parsowania))[2:-3])
     query_parse = urlparse.parse_qs(do_parsowania.query)
     self.assertEqual(query_parse['key'][0], NOKAUT_KEY)
     self.assertEqual(query_parse['keyword'][0], NOKAUT_KEY_WORD)
     self.assertEqual(query_parse['format'][0], 'rest')
     self.assertEqual(query_parse['method'][0], 'nokaut.Product.getByKeyword')
     self.assertEqual(do_parsowania.scheme, 'http')
     self.assertEqual(do_parsowania.netloc, 'api.nokaut.pl')
     self.assertEqual(do_parsowania.path, '/')
Esempio n. 2
0
def res_view(request):
    search_phrase = request.GET.get('search_field')
    nokaut_key = request.registry.settings.get('nokaut.key')
    user = request.user

    try :
        all = allegro_api(search_phrase)
    except AllegroNoItemEx:
        all = (None, None)

    try:
        nok = nokaut_api(search_phrase, nokaut_key)
    except NokautNoItemEx:
        nok = (None, None)

    nok_price, nok_link = nok[1], nok[0]
    all_price, all_link = all[1], all[0]

    mode = None
    if all_price and nok_price:
        if all_price < nok_price:
            mode = 'allegro'
        elif all_price > nok_price:
            mode = 'nokaut'
    elif all_price:
        mode = 'allegro'
    elif nok_price:
        mode = 'nokaut'

    if user is not None:
        prev = DBSession.query(UserSearch)\
                        .filter(UserSearch.search_id == user.id)\
                        .filter(UserSearch.search_content == search_phrase).first()

        if prev is not None:
            prev.search_quantity += 1
        else:
            search = UserSearch(
                search_id = user.id,
                search_content = search_phrase,
                all_link = all_link or '#',
                all_price = all_price or 0,
                nok_link = nok_link or '#',
                nok_price = nok_price or 0,
                search_quantity = 1
            )
            DBSession.add(search)

    return {
        'product_name' : search_phrase,
        'allegro_link' : all_link,
        'nokaut_link' : nok_link,
        'allegro_price' : all_price,
        'nokaut_price' : nok_price,
        'won' : mode
        }
Esempio n. 3
0
    def search():
        try :
            all_link, all_price = allegro_api(search_phrase)
        except AllegroNoItemEx:
            all_link, all_price = None, None

        try:
            nok_link, nok_price = nokaut_api(search_phrase, nokaut_key)
        except NokautNoItemEx:
            nok_link, nok_price = None, None

        return all_link, all_price, nok_link, nok_price
Esempio n. 4
0
 def test_czy_wyjscie(self, mmock):
     stream = mock.MagicMock()
     mmock.return_value = stream
     stream.read.return_value = EXAMPLE_RESPONSE
     self.assertEqual(
         nokaut_api(
             'A_KEY', 'A_NAME'
         ),
         (
             139.00,
             'http://www.nokaut.pl/futeraly-fotograficzne/easycover-na-aparat-canon-450-500d.html',
             'http://nokautimg4.pl/p-cb-eb-cbeba28bb6ba027093e79d196c51b09d500x500/easycover-na-aparat-canon-450-500d.jpg'
         )
     )
def search_result_view(request):

    data = request.GET.get('item')

    if not data:
        return HTTPFound('/')

    allegro_state = nokaut_state = 'price'

    try:
        allegro_price, allegro_url = allegro_api(data)
    except AllegroError:
        allegro_state = 'price'
        allegro_price = 'No product'
        allegro_url = ''

    try:
        nokaut_price, nokaut_url = nokaut_api(data, request.registry.settings.get('nokaut_key'))
    except NokautError:
        nokaut_state = 'price'
        nokaut_price = 'No product'
        nokaut_url = ''

    if nokaut_price != allegro_price:
        if allegro_price < nokaut_price:
            allegro_state = 'price win'
        else:
            nokaut_state = 'price win'

    response = {
        'product': data,
        'allegro_price_state': allegro_state,
        'allegro_price': allegro_price,
        'allegro_url': allegro_url,
        'nokaut_price_state': nokaut_state,
        'nokaut_price': nokaut_price,
        'nokaut_url': nokaut_url,
    }

    product = Product(
        name=data,
        a_price=allegro_price,
        a_url=allegro_url,
        n_price=nokaut_price,
        n_url=nokaut_url,
    )
    DBSession.add(product)

    return response
Esempio n. 6
0
def res_view(request):
    search_phrase = request.GET.get('search_field')
    nokaut_key = request.registry.settings.get('nokaut.key')

    try :
        all = allegro_api(search_phrase)
    except AllegroNoItemEx:
        all = (None, None)

    try:
        nok = nokaut_api(search_phrase,nokaut_key)
    except NokautNoItemEx:
        nok = (None, None)

    nok_price, nok_link = nok[1], nok[0]
    all_price, all_link = all[1], all[0]

    if (nok_price == None and all_price == None) or (nok_price == all_price):
        all_mode, nok_mode = '', ''
    elif nok_price != None and all_price == None:
        all_mode, nok_mode = '', 'win'
    elif nok_price == None and all_price != None:
        all_mode, nok_mode = 'win', ''
    else :
        if all_price < nok_price:
            all_mode, nok_mode = 'win', ''
        elif all_price > nok_price:
            all_mode, nok_mode = '', 'win'

    return {
            'product_name' : search_phrase,
            'allegro_link' : all_link,
            'nokaut_link' : nok_link,
            'allegro_price' : all_price,
            'nokaut_price' : nok_price,
            'allegro_price_mode' : all_mode,
            'nokaut_price_mode' : nok_mode
        }
Esempio n. 7
0
def result_view(request, _=None):
    name = request.GET.get('product')
    if not name:
        url = request.route_url('home')
        return HTTPFound(location=url)
    model = History()
    product = DBSession.query(History).filter(name == History.name).first()
    if product is not None:
        product.count += 1
        if (datetime.datetime.now() - product.date).days > 1:
            model = product
        else:
            return dict(entry=product)
    model.name = name
    name_encode = name.encode('utf-8')
    try:
        price, url = result(name_encode)
    except (FindEroor, ConnectionError) as e:
        model.status_allegro = str(e)
    else:
        model.status_allegro = ''
        model.price_allegro = price
        model.url_allegro = url
    try:
        price, url = nokaut_api(name_encode, request.registry.settings.get('nokaut.key'))
    except NokautError as e:
        model.status_nokaut = str(e)
    else:
        model.status_nokaut = ''
        model.price_nokaut = price
        model.url_nokaut = url

    model.user_id_ = request.user.id_
    DBSession.add(model)
    DBSession.flush()
    return dict(
        entry=model,
    )
def search_result_view(request):

    name = request.GET.get('item')

    if not name:
        return {'error': 'Give a product name to compare'}

    date = datetime.date.today()
    user_id = request.user.id
    error = None
    product_exists = DBSession.query(Product)\
                              .filter(
                                  Product.name == name,
                                  Product.user_id == user_id,
                              ).first()

    if product_exists:
        if (date - product_exists.date).days < 2:
            product_exists.popularity += 1
            return dict(
                product=product_exists,
                error=error,
            )

    try:
        allegro_price, allegro_url = allegro_api(name)
    except AllegroError:
        allegro_price = 0
        allegro_url = ''

    try:
        nokaut_price, nokaut_url = nokaut_api(
            name,
            request.registry.settings.get('nokaut_key')
        )
    except NokautError:
        nokaut_price = 0
        nokaut_url = ''

    popularity = 1

    prod = DBSession.query(Product)\
                    .filter(
                        Product.name == name,
                        Product.user_id == user_id
                    ).first()

    if prod:
        popularity = prod.popularity + 1

    product = Product(
        name=name,
        a_price=allegro_price,
        a_url=allegro_url,
        n_price=nokaut_price,
        n_url=nokaut_url,
        popularity=popularity,
        user_id=user_id,
        date=date,
    )
    DBSession.add(product)

    return dict(
        product=product,
        error=error,
    )
Esempio n. 9
0
 def test_success_url(self, urllib2):
     urllib2.urlopen().read.return_value = NOKAUT_CORRECT_RESPONSE
     _, price = lib.nokaut_api('rakieta kosmiczna', self.nokaut_key)
     self.assertEqual(price, 29.00)
Esempio n. 10
0
 def test_success_price(self, urllib2):
     urllib2.urlopen().read.return_value = NOKAUT_CORRECT_RESPONSE
     url, _ = lib.nokaut_api('rakieta kosmiczna', self.nokaut_key)
     self.assertEqual(url, 'http://www.nokaut.pl/zabawki-kreatywne/zrob-to-sam-rakieta-kosmiczna-4m-3235.html')
def my_view(request):

    product_name = request.GET.get('product')
    product_name_encode = product_name.encode('utf-8')
    nokaut_key = request.registry.settings.get('nokaut.key')
    if not product_name:
        return dict(
            status='NO_ITEM'
        )

    product = DBSession.query(Product) \
        .filter(Product.name == product_name) \
        .filter(Product.user_id == request.user.id) \
        .order_by(Product.time.desc()).first()

    if product is None:
        model = Product()
        model.name = product_name
        model.user_id = request.user.id
    elif request.is_xhr == True or (datetime.datetime.now() - product.time).days > 2:
        model = Product()
        model = product
        model.count += 1
    else:
        product.count += 1
        return dict(
            status='OK',
            product=product
        )

    try:
        url_a, price_a = downolad_data(product_name_encode)
    except AllegroError as e:
        url_a = None
        price_a = None
    else:
        model.url_a = url_a
        model.price_a = price_a
    try:
        price_n, url_n, img_url = nokaut_api(nokaut_key, product_name)
    except NokautError as e:
        url_n = None
        price_n = None
    else:
        model.url_n = url_n
        model.price_n = price_n

    path = os.path.abspath(os.path.dirname(__file__))
    image_path = '%s/static/img/tmp/%s.jpg' % (path, product_name)
    image_path_thum = '%s/static/img/tmp/t_%s.jpg' % (path, product_name)
    urllib.urlretrieve(img_url, image_path)
    
    im = Image.open(image_path)
    im.thumbnail((36, 36))
    im.save(image_path_thum, "JPEG")

    FONT = '%s/static/img/Fonts/Ubuntu-LI.ttf' % path
    img = Image.open(image_path).convert("RGB")
    watermark = Image.new("RGBA", (img.size[0], img.size[1]))
    draw = ImageDraw.ImageDraw(watermark, "RGBA")
    font = ImageFont.truetype(FONT, 100)
    draw.text((63,422), "Malwina", font=font, fill='#FF0000')
    mask = watermark.convert("L").point(lambda x: min(x, 55))
    watermark.putalpha(mask)
    img.paste(watermark, None, watermark)
    img.save(image_path, 'JPEG')

    DBSession.add(model)

    return dict(
        status='OK',
        product=model
    )