<commit_before><?php

namespace Wishlist\CoreBundle\Services;

use Wishlist\CoreBundle\Entity\Item;

class AmazonSearchService
{
    protected $accessId;
    protected $accessKey;
    const VERSION = "2011-08-01";
    const ResponseGroup = "ItemAttributes,Images";

    function __construct($accessId, $accessKey, $associateTag)
    {
        $this->accessId = $accessId;
        $this->accessKey = $accessKey;
        $this->associateTag = $associateTag;
    }
    
    protected function createRequestBase($operation, $params)
    {
        $request=
             "http://webservices.amazon.com/onca/xml"
           . "?Service=AWSECommerceService"
           . "&AssociateTag=" . $this->associateTag
           . "&AWSAccessKeyId=" . $this->accessId
           . "&Operation=" . $operation
           . "&Version=" . AmazonSearchService::VERSION
           . "&ResponseGroup=" . AmazonSearchService::ResponseGroup
           . "&Timestamp=" . gmdate("Y-m-d\TH:i:s\Z")
           . $params;

        $strToSign = $this->createStringToSign($request);
        $request .= "&Signature=" . strtr( base64_encode(hash_hmac("sha256", $strToSign, $this->accessKey, true)), array('+' => '%2B', '=' => '%3D') );

        return $request;
    }

    protected function createItemSearchRequest($searchIndex, $keywords)
    {
        $params = 
             "&SearchIndex=" . $searchIndex
           . "&Keywords=" . strtr( $keywords, array('+' => '%20') );

        return $this->createRequestBase("ItemSearch", $params);
    }

    protected function createItemLookupRequest($asin)
    {
        $params = "&ItemId=" . $asin;

        return $this->createRequestBase("ItemLookup", $params);
    }
    
    protected function CreateStringToSign($request) 
    {
        $str = strtr( substr($request, strpos($request, "?")+1), array(',' => '%2C', ':' => '%3A', '+' => '%20') );
        $params = explode("&", $str);
        natsort($params);

        $str = "GET\nwebservices.amazon.com\n/onca/xml\n";
        foreach ($params as $param) {
            $str .= $param."&";
        }

        return rtrim($str, '&');
    }

    protected function responseToItems($response)
    {
        $array = array();
        foreach($response->Items->Item as $current) {

            if( !isset($current->ItemAttributes->Title) || 
                !isset($current->ItemAttributes->ListPrice->Amount) || 
                !isset($current->DetailPageURL) || 
                !isset($current->ASIN) ) 
            {
                //If any of the required info is not present skip this item.
                continue;
            }

            $item = new Item();<commit_msg>Making amazon search a little more robust.<commit_after><?php

namespace Wishlist\CoreBundle\Services;

use Wishlist\CoreBundle\Entity\Item;

class AmazonSearchService
{
    protected $accessId;
    protected $accessKey;
    const VERSION = "2011-08-01";
    const ResponseGroup = "ItemAttributes,Images";

    function __construct($accessId, $accessKey, $associateTag)
    {
        $this->accessId = $accessId;
        $this->accessKey = $accessKey;
        $this->associateTag = $associateTag;
    }
    
    protected function createRequestBase($operation, $params)
    {
        $request=
             "http://webservices.amazon.com/onca/xml"
           . "?Service=AWSECommerceService"
           . "&AssociateTag=" . $this->associateTag
           . "&AWSAccessKeyId=" . $this->accessId
           . "&Operation=" . $operation
           . "&Version=" . AmazonSearchService::VERSION
           . "&ResponseGroup=" . AmazonSearchService::ResponseGroup
           . "&Timestamp=" . gmdate("Y-m-d\TH:i:s\Z")
           . $params;

        $strToSign = $this->createStringToSign($request);
        $request .= "&Signature=" . strtr( base64_encode(hash_hmac("sha256", $strToSign, $this->accessKey, true)), array('+' => '%2B', '=' => '%3D') );

        return $request;
    }

    protected function createItemSearchRequest($searchIndex, $keywords)
    {
        $params = 
             "&SearchIndex=" . $searchIndex
           . "&Keywords=" . strtr( $keywords, array('+' => '%20') );

        return $this->createRequestBase("ItemSearch", $params);
    }

    protected function createItemLookupRequest($asin)
    {
        $params = "&ItemId=" . $asin;

        return $this->createRequestBase("ItemLookup", $params);
    }
    
    protected function CreateStringToSign($request) 
    {
        $str = strtr( substr($request, strpos($request, "?")+1), array(',' => '%2C', ':' => '%3A', '+' => '%20', '(' => '%28', ')' => '%29') );
        $params = explode("&", $str);
        natsort($params);

        $str = "GET\nwebservices.amazon.com\n/onca/xml\n";
        foreach ($params as $param) {
            $str .= $param."&";
        }

        return rtrim($str, '&');
    }

    protected function responseToItems($response)
    {
        $array = array();
        foreach($response->Items->Item as $current) {

            if( !isset($current->ItemAttributes->Title) || 
                !isset($current->ItemAttributes->ListPrice->Amount) || 
                !isset($current->DetailPageURL) || 
                !isset($current->ASIN) ) 
            {
                //If any of the required info is not present skip this item.
                continue;
            }

            $item = new Item();
{'fraction_non_alphanumeric': 0.16402313864526963, 'fraction_numerical': 0.008956894943086397, 'mean_word_length': 2.264311814859927, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3952480', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1563, 'n_words': 287}