PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/cron/gameGetAmazonGameInfoTask.class.php

https://github.com/yinkyweb/game
PHP | 233 lines | 177 code | 16 blank | 40 comment | 5 complexity | 8859222563abb147a70035a637251da0 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * gameGetAmazonGameInfoTask.class.php
  4. *
  5. * PHP versions 5
  6. *
  7. * @category game
  8. * @package game
  9. * @author Yasunori Mahata <nori@mahata.net>
  10. * @copyright 2009 mahata.net
  11. * @license http://www.opensource.org/licenses/bsd-license.php
  12. * @link http://mahata.net
  13. */
  14. define('ACCESS_KEY_ID', '12PZGM3FVPA6P78SK5G2');
  15. define('SECRET_ACCESS_KEY', 'Lkgt8DZOhPbay3XJw4yz8tKYHF9MkpD4ja89YZQe');
  16. define('BASE_URL', 'http://ecs.amazonaws.jp/onca/xml');
  17. /**
  18. * A class to fetch amazon data
  19. *
  20. * @category game
  21. * @package game
  22. * @author Yasunori Mahata <nori@mahata.net>
  23. * @copyright 2009 mahata.net
  24. * @license http://www.opensource.org/licenses/bsd-license.php
  25. * @link http://mahata.net
  26. */
  27. class gameGetAmazonGameInfoTask extends sfBaseTask
  28. {
  29. /**
  30. * Do configuration of the script
  31. *
  32. * @return void
  33. */
  34. protected function configure()
  35. {
  36. $this->addArguments(array(
  37. // new sfCommandArgument('first_product', sfCommandArgument::OPTIONAL, 'a first product to search'),
  38. ));
  39. $this->addOptions(array(
  40. // new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
  41. new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'),
  42. new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
  43. new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'),
  44. // add your own options here
  45. ));
  46. $this->namespace = 'game';
  47. $this->name = 'getAmazonGameInfo';
  48. $this->briefDescription = 'Get Amazon Game Information.';
  49. $this->detailedDescription = <<<EOF
  50. The [game:getAmazonGameInfo|INFO] task does things.
  51. Call it with:
  52. [php symfony game:getAmazonGameInfo|INFO]
  53. EOF;
  54. }
  55. /**
  56. * Main function
  57. *
  58. * @param array $arguments Arguments
  59. * @param array $options Options
  60. *
  61. * @return boolean
  62. */
  63. protected function execute($arguments = array(), $options = array())
  64. {
  65. // initialize the database connection
  66. $databaseManager = new sfDatabaseManager($this->configuration);
  67. $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
  68. $title_list = file(sfConfig::get("sf_root_dir") . '/lib/task/gamelist.txt');
  69. foreach ($title_list as $title) {
  70. $title = trim($title);
  71. if ('' === $title) {
  72. continue;
  73. }
  74. echo "fetching ... {$title}\n";
  75. $url = $this->get_amazon_url($title);
  76. echo "{$url}\n";
  77. $xml = simplexml_load_file($url);
  78. foreach ($xml->Items->Item as $item) {
  79. $this->insert_amazon_product($item);
  80. $this->insert_amazon_review($item);
  81. $this->insert_amazon_similar_product($item);
  82. }
  83. }
  84. return true;
  85. }
  86. private function insert_amazon_product($item)
  87. {
  88. $criteria = new Criteria();
  89. $criteria->add(AmazonProductPeer::ASIN, $item->ASIN, Criteria::EQUAL);
  90. $amazon_product = AmazonProductPeer::doSelectOne($criteria);
  91. if (is_null($amazon_product)) {
  92. echo "asin " . $item->ASIN . " doesn't exist yet.\n";
  93. $amazon_product = new AmazonProduct();
  94. } else {
  95. echo "asin " . $item->ASIN . " already exist.\n";
  96. }
  97. //$amazon_product = new AmazonProduct();
  98. $amazon_product->setAsin($item->ASIN);
  99. $amazon_product->setSmallImageUrl($item->SmallImage->URL);
  100. $amazon_product->setMediumImageUrl($item->MediumImage->URL);
  101. $amazon_product->setLargeImageUrl($item->LargeImage->URL);
  102. $amazon_product->setBrand($item->ItemAttributes->Brand);
  103. $amazon_product->setEan($item->ItemAttributes->EAN);
  104. // $amazon_product->setGenre($item->ItemAttributes->Genre);
  105. $amazon_product->setPlatform(sfConfig::get('app_platform_' . $item->ItemAttributes->HardwarePlatform));
  106. $amazon_product->setDefaultPrice($item->ItemAttributes->ListPrice->Amount);
  107. $amazon_product->setAmazonPrice(@$item->Offers->Offer->OfferListing->Price->Amount);
  108. $amazon_product->setLowestNewPrice(@$item->OfferSummary->LowestNewPrice->Amount);
  109. $amazon_product->setLowestUsedPrice(@$item->OfferSummary->LowestUsedPrice->Amount);
  110. $amazon_product->setReleaseAt($item->ItemAttributes->ReleaseDate);
  111. $amazon_product->setTitle($item->ItemAttributes->Title);
  112. echo $item->ItemAttributes->Title . ', ' . $item->ItemAttributes->HardwarePlatform . "\n";
  113. try {
  114. $amazon_product->save();
  115. } catch (Exception $e) {
  116. var_dump($e->getMessage());
  117. }
  118. }
  119. private function insert_amazon_review($item)
  120. {
  121. if (isset($item->CustomerReviews->Review)) {
  122. foreach ($item->CustomerReviews->Review as $review) {
  123. $customer_id = ('' === trim($review->Reviewer->CustomerId)) ? (md5($review->Content) . '__UNKNOWN_USER__') : $review->Reviewer->CustomerId;
  124. $customer_name = ('' === trim($review->Reviewer->Name)) ? '名無しさん' : $review->Reviewer->Name;
  125. $criteria = new Criteria();
  126. $criteria->add(AmazonReviewPeer::ASIN, $item->ASIN, Criteria::EQUAL);
  127. $criteria->add(AmazonReviewPeer::AMAZON_CUSTOMER_ID, $customer_id, Criteria::EQUAL);
  128. $amazon_review = AmazonReviewPeer::doSelectOne($criteria);
  129. if (is_null($amazon_review)) {
  130. echo "asin " . $item->ASIN . ", customer_id " . $review->Reviewer->CustomerId . " doesn't exist yet.\n";
  131. $amazon_review = new AmazonReview();
  132. } else {
  133. echo "asin " . $item->ASIN . ", customer_id " . $review->Reviewer->CustomerId . " already exist.\n";
  134. }
  135. // $amazon_review = new AmazonReview();
  136. $amazon_review->setAsin($item->ASIN);
  137. $amazon_review->setRating($review->Rating);
  138. $amazon_review->setHelpfulVote($review->HelpfulVotes);
  139. $amazon_review->setTotalVote($review->TotalVotes);
  140. $amazon_review->setAmazonCustomerId($customer_id);
  141. $amazon_review->setAmazonCustomerName($customer_name);
  142. $amazon_review->setTitle($review->Summary);
  143. $amazon_review->setArticle($review->Content);
  144. $amazon_review->setCommentAt($review->Date);
  145. try {
  146. $amazon_review->save();
  147. } catch (Exception $e) {
  148. var_dump($e->getMessage());
  149. }
  150. }
  151. }
  152. }
  153. private function insert_amazon_similar_product($item)
  154. {
  155. if (isset($item->SimilarProducts->SimilarProduct)) {
  156. foreach ($item->SimilarProducts->SimilarProduct as $similar_product) {
  157. $criteria = new Criteria();
  158. $criteria->add(AmazonSimilarProductPeer::ROOT_ASIN, $item->ASIN, Criteria::EQUAL);
  159. $criteria->add(AmazonSimilarProductPeer::SIMILAR_ASIN, $similar_product->ASIN, Criteria::EQUAL);
  160. $amazon_similar_product = AmazonSimilarProductPeer::doSelectOne($criteria);
  161. if (is_null($amazon_similar_product)) {
  162. echo "asin " . $item->ASIN . ", similar_id " . $similar_product->ASIN . " doesn't exist.\n";
  163. $amazon_similar_product = new AmazonSimilarProduct();
  164. } else {
  165. echo "asin " . $item->ASIN . ", similar_id " . $similar_product->ASIN . " already exist.\n";
  166. }
  167. // $amazon_similar_product = new AmazonSimilarProduct();
  168. $amazon_similar_product->setRootAsin($item->ASIN);
  169. $amazon_similar_product->setSimilarAsin($similar_product->ASIN);
  170. try {
  171. $amazon_similar_product->save();
  172. } catch (Exception $e) {
  173. var_dump($e->getMessage());
  174. }
  175. }
  176. }
  177. }
  178. private function get_amazon_url($query)
  179. {
  180. $params = array();
  181. $params['Service'] = 'AWSECommerceService';
  182. $params['AWSAccessKeyId'] = ACCESS_KEY_ID;
  183. $params['Version'] = '2009-03-31';
  184. $params['Operation'] = 'ItemSearch';
  185. $params['SearchIndex'] = 'VideoGames';
  186. $params['ResponseGroup'] = 'Large';
  187. $params['Keywords'] = $query;
  188. $params['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
  189. ksort($params);
  190. $canonical_string = '';
  191. foreach ($params as $k => $v) {
  192. $canonical_string .= '&' . $this->urlencode_rfc3986($k) . '=' . $this->urlencode_rfc3986($v);
  193. }
  194. $canonical_string = substr($canonical_string, 1);
  195. $parsed_url = parse_url(BASE_URL);
  196. $string_to_sign = "GET\n{$parsed_url['host']}\n{$parsed_url['path']}\n{$canonical_string}";
  197. $signature = base64_encode(hash_hmac('sha256', $string_to_sign, SECRET_ACCESS_KEY, true));
  198. return BASE_URL . '?' . $canonical_string . '&Signature=' . $this->urlencode_rfc3986($signature);
  199. }
  200. private function urlencode_rfc3986($str)
  201. {
  202. return str_replace('%7E', '~', rawurlencode($str));
  203. }
  204. }