Browse Source

Fix SteamBridge (#637) (#639)

Fixes #639
Antoine Cadoret 6 years ago
parent
commit
cb91cd5d2f
1 changed files with 70 additions and 45 deletions
  1. 70 45
      bridges/SteamBridge.php

+ 70 - 45
bridges/SteamBridge.php

@@ -4,7 +4,7 @@ class SteamBridge extends BridgeAbstract {
 	const NAME = 'Steam Bridge';
 	const URI = 'https://store.steampowered.com/';
 	const CACHE_TIMEOUT = 3600; // 1h
-	const DESCRIPTION = 'Returns games list';
+	const DESCRIPTION = 'Returns apps list';
 	const MAINTAINER = 'jacknumber';
 	const PARAMETERS = array(
 		'Wishlist' => array(
@@ -47,16 +47,6 @@ class SteamBridge extends BridgeAbstract {
 					'AED' => 'ae',
 				),
 			),
-			'sort' => array(
-				'name' => 'Sort by',
-				'type' => 'list',
-				'values' => array(
-					'Rank' => 'rank',
-					'Date Added' => 'added',
-					'Name' => 'name',
-					'Price' => 'price',
-				)
-			),
 			'only_discount' => array(
 				'name' => 'Only discount',
 				'type' => 'checkbox',
@@ -68,49 +58,44 @@ class SteamBridge extends BridgeAbstract {
 
 		$username = $this->getInput('username');
 		$params = array(
-			'cc' => $this->getInput('currency'),
-			'sort' => $this->getInput('sort')
+			'cc' => $this->getInput('currency')
 		);
 
-		$url = self::URI . 'wishlist/id/' . $username . '/?' . http_build_query($params);
+		$url = self::URI . 'wishlist/id/' . $username . '?' . http_build_query($params);
 
-		$jsonDataRegex = '/var g_rg(?:WishlistData|AppInfo) = ([^;]*)/';
-		$content = getContents($url)
-			or returnServerError("Could not request Steam Wishlist. Tried:\n - $url");
-
-		preg_match_all($jsonDataRegex, $content, $matches, PREG_SET_ORDER, 0);
+		$targetVariable = 'g_rgAppInfo';
+		$sort = array();
 
-		$appList = json_decode($matches[0][1], true);
-		$fullAppList = json_decode($matches[1][1], true);
-		//var_dump($matches[1][1]);
-		//var_dump($fullAppList);
-		$sortedElementList = array_fill(0, count($appList), 0);
-		foreach($appList as $app) {
+		$html = '';
+		$html = getSimpleHTMLDOM($url)
+			or returnServerError("Could not request Steam Wishlist. Tried:\n - $url");
 
-			$sortedElementList[$app["priority"] - 1] = $app["appid"];
+		$jsContent = $html->find('.responsive_page_template_content script', 0)->innertext;
 
+		if(preg_match('/var ' . $targetVariable . ' = (.*?);/s', $jsContent, $matches)) {
+			$appsData = json_decode($matches[1]);
+		} else {
+			returnServerError("Could not parse JS variable ($targetVariable) in page content.");
 		}
 
-		foreach($sortedElementList as $appId) {
+		foreach($appsData as $id => $element) {
 
-			$app = $fullAppList[$appId];
-			$gameTitle = $app["name"];
-			$gameUri = "http://store.steampowered.com/app/" . $appId . "/";
-			$gameImg = $app["capsule"];
+			$appType = $element->type;
+			$appIsBuyable = 0;
+			$appHasDiscount = 0;
+			$appIsFree = 0;
 
-			$item = array();
-			$item['uri'] = $gameUri;
-			$item['title'] = $gameTitle;
-
-			if(count($app["subs"]) > 0) {
-				if($app["subs"][0]["discount_pct"] != 0) {
+			if($element->subs) {
+				$appIsBuyable = 1;
 
-					$item['promoValue'] = $app["subs"][0]["discount_pct"];
-					$item['oldPrice'] = $app["subs"][0]["price"] / 100 / ((100 - $gamePromoValue / 100));
-					$item['newPrice'] = $app["subs"][0]["price"] / 100;
-					$item['price'] = $item['newPrice'];
+				if($element->subs[0]->discount_pct) {
 
-					$item['hasPromo'] = true;
+					$appHasDiscount = 1;
+					$discountBlock = str_get_html($element->subs[0]->discount_block);
+					$appDiscountValue = $discountBlock->find('.discount_pct', 0)->plaintext;
+					$appOldPrice = $discountBlock->find('.discount_original_price', 0)->plaintext;
+					$appNewPrice = $discountBlock->find('.discount_final_price', 0)->plaintext;
+					$appPrice = $appNewPrice;
 
 				} else {
 
@@ -118,15 +103,55 @@ class SteamBridge extends BridgeAbstract {
 						continue;
 					}
 
-					$item['price'] = $app["subs"][0]["price"] / 100;
-					$item['hasPromo'] = false;
+					$appPrice = $element->subs[0]->price / 100;
+				}
+
+			} else {
+
+				if($this->getInput('only_discount')) {
+					continue;
+				}
+
+				if(isset($element->free) && $element->free = 1) {
+					$appIsFree = 1;
 				}
+			}
 
+			$item = array();
+			$item['uri'] = "http://store.steampowered.com/app/$id/";
+			$item['title'] = $element->name;
+			$item['type'] = $appType;
+			$item['cover'] = str_replace('_292x136', '', $element->capsule);
+			$item['timestamp'] = $element->added;
+			$item['isBuyable'] = $appIsBuyable;
+			$item['hasDiscount'] = $appHasDiscount;
+			$item['isFree'] = $appIsFree;
+			$item['priority'] = $element->priority;
+
+			if($appIsBuyable) {
+				$item['price'] = floatval(str_replace(',', '.', $appPrice));
 			}
 
-			$this->items[] = $item;
+			if($appHasDiscount) {
+
+				$item['discount']['value'] = $appDiscountValue;
+				$item['discount']['oldPrice'] = floatval(str_replace(',', '.', $appOldPrice));
+				$item['discount']['newPrice'] = floatval(str_replace(',', '.', $appNewPrice));
+
+			}
+
+			$item['enclosures'] = array();
+			$item['enclosures'][] = str_replace('_292x136', '', $element->capsule);
+
+			foreach($element->screenshots as $screenshot) {
+				$item['enclosures'][] = substr($element->capsule, 0, -31) . $screenshot;
+			}
+
+			$sort[$id] = $element->priority;
 
+			$this->items[] = $item;
 		}
 
+		array_multisort($sort, SORT_ASC, $this->items);
 	}
 }