/** * @param $itemID * @param int $retry * @return APIItemV2 */ public static function getItemById($itemID, $retry = 3) { $cache = CacheHandler::getInstance('item_gw2api'); $cacheKey = $itemID . "::" . substr(md5($itemID), 0, 10); $ttl = 86400; if (!($API_JSON = $cache->get($cacheKey))) { for ($i = 0; $i < $retry; $i++) { try { $curl_item = CurlRequest::newInstance(getAppConfig('gw2spidy.gw2api_url') . "/v1/item_details.json?item_id={$itemID}")->exec(); $API_JSON = $curl_item->getResponseBody(); $cache->set($cacheKey, $API_JSON, MEMCACHE_COMPRESSED, $ttl); return self::getSingleItemByJSON($API_JSON); } catch (\Exception $e) { // - supress and short sleep usleep(0.2 * 1000 * 1000); // 0.2s } } $ttl = 60; $cache->set($cacheKey, null, MEMCACHE_COMPRESSED, $ttl); return null; } else { return self::getSingleItemByJSON($API_JSON); } }
/** * @param $url string The url for this API call. * @return mixed[] The API answer parsed as an array. * @throws Exception Thrown if a non positive HTTP Code was returned. */ protected function getApiData($url) { $curl = CurlRequest::newInstance($url)->exec(); if (!in_array($curl->getInfo("http_code"), array(200, 206))) { throw new Exception("Failed to retrieve API data [{$url}] [{$curl->getInfo('http_code')}]. \n" . substr($curl->getResponseBody(), 0, 200)); } $data = json_decode($curl->getResponseBody(), true); return $data; }
public static function getRecipeById($dataId) { $cache = CacheHandler::getInstance('recipe_gw2api'); $cacheKey = $dataId . "::" . substr(md5($dataId), 0, 10); $ttl = 86400; if (!($API_JSON = $cache->get($cacheKey))) { try { $curl_item = CurlRequest::newInstance(getAppConfig('gw2spidy.gw2api_url') . "/v1/recipe_details.json?recipe_id={$dataId}")->exec(); $API_JSON = $curl_item->getResponseBody(); $cache->set($cacheKey, $API_JSON, MEMCACHE_COMPRESSED, $ttl); } catch (\Exception $e) { $ttl = 600; $cache->set($cacheKey, null, MEMCACHE_COMPRESSED, $ttl); return null; } } return APIRecipe::getRecipeByJSON($API_JSON); }
"1":{ "DataID":3219, "Name":"Feast of Veggie Pizzas", "Rating":125, "Type":8, "Count":1, "CreatedItemId":12602, "RequiresRecipeItem":true, "Ingredients":[{"ItemID":12346,"Count":10}]} } } */ //Quick and dirty discipline name to ID translation. $disciplines = array('Huntsman' => 1, 'Artificer' => 2, 'Weaponsmith' => 3, 'Armorsmith' => 4, 'Leatherworker' => 5, 'Tailor' => 6, 'Jeweler' => 7, 'Chef' => 8); //Gather all recipes by recipe_id $curl = CurlRequest::newInstance(getAppConfig('gw2spidy.gw2api_url') . "/v1/recipes.json")->exec(); $data = json_decode($curl->getResponseBody(), true); $multi_curl = EpiCurl::getInstance(); $recipe_curls = array(); $recipe_count = count($data['recipes']); $error_values = array(); $i = 0; $ii = 0; foreach (array_chunk($data['recipes'], 1000) as $recipes) { //Add all curl requests to the EpiCurl instance. foreach ($recipes as $recipe_id) { $i++; $ch = curl_init(getAppConfig('gw2spidy.gw2api_url') . "/v1/recipe_details.json?recipe_id={$recipe_id}"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $recipe_curls[$recipe_id] = $multi_curl->addCurl($ch); echo "[{$i} / {$recipe_count}]: {$recipe_id}\n";
$item->setItemSubType($itemSubType); } $itemType->addSubType($itemSubType); $item->setItemSubType($itemSubType); } $item->setItemType($itemType); } $item->save(); } } catch (Exception $e) { echo "failed [[ {$e->getMessage()} ]] .. \n"; } } Propel::disableInstancePooling(); $pageSize = 200; $curl = CurlRequest::newInstance(getAppConfig('gw2spidy.gw2api_url') . "/v2/items?page=0&page_size={$pageSize}")->exec(); if ($curl->getInfo("http_code") != 200) { print "Failed curl request. Returned Status was {$curl->getInfo("http_code")}.\n"; print "Returned body: {$curl->getResponseBody()}\n"; exit(1); } processApiData($curl->getResponseBody(), 0); $numberOfPages = intval($curl->getResponseHeaders("X-Page-Total")); $multi_curl = EpiCurl::getInstance(); $item_curls = array(); $itemSubTypes = array(); //Add all curl requests to the EpiCurl instance. for ($page = 1; $page < $numberOfPages; $page++) { echo "REQUESTING [{$page} / {$numberOfPages}]\n"; $ch = curl_init(getAppConfig('gw2spidy.gw2api_url') . "/v2/items?page={$page}&page_size={$pageSize}"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);