rss-bridge/bridges/SteamBridge.php

158 lines
3.9 KiB
PHP
Raw Normal View History

2017-07-17 15:45:58 +02:00
<?php
class SteamBridge extends BridgeAbstract {
const NAME = 'Steam Bridge';
2018-03-07 11:24:33 +01:00
const URI = 'https://store.steampowered.com/';
2017-07-17 15:45:58 +02:00
const CACHE_TIMEOUT = 3600; // 1h
const DESCRIPTION = 'Returns apps list';
2017-07-17 15:45:58 +02:00
const MAINTAINER = 'jacknumber';
const PARAMETERS = array(
'Wishlist' => array(
'username' => array(
'name' => 'Username',
'required' => true,
),
'currency' => array(
'name' => 'Currency',
'type' => 'list',
'values' => array(
// source: http://steam.steamlytics.xyz/currencies
'USD' => 'us',
'GBP' => 'gb',
'EUR' => 'fr',
'CHF' => 'ch',
'RUB' => 'ru',
'BRL' => 'br',
'JPY' => 'jp',
'SEK' => 'se',
'IDR' => 'id',
'MYR' => 'my',
'PHP' => 'ph',
'SGD' => 'sg',
'THB' => 'th',
'KRW' => 'kr',
'TRY' => 'tr',
'MXN' => 'mx',
'CAD' => 'ca',
'NZD' => 'nz',
'CNY' => 'cn',
'INR' => 'in',
'CLP' => 'cl',
'PEN' => 'pe',
'COP' => 'co',
'ZAR' => 'za',
'HKD' => 'hk',
'TWD' => 'tw',
'SRD' => 'sr',
'AED' => 'ae',
),
),
'only_discount' => array(
'name' => 'Only discount',
'type' => 'checkbox',
)
)
);
public function collectData(){
$username = $this->getInput('username');
$params = array(
'cc' => $this->getInput('currency')
2017-07-17 15:45:58 +02:00
);
$url = self::URI . 'wishlist/id/' . $username . '?' . http_build_query($params);
2017-07-17 15:45:58 +02:00
$targetVariable = 'g_rgAppInfo';
$sort = array();
2017-07-17 15:45:58 +02:00
$html = '';
$html = getSimpleHTMLDOM($url)
or returnServerError("Could not request Steam Wishlist. Tried:\n - $url");
2017-07-17 15:45:58 +02:00
$jsContent = $html->find('.responsive_page_template_content script', 0)->innertext;
2017-07-17 15:45:58 +02:00
if(preg_match('/var ' . $targetVariable . ' = (.*?);/s', $jsContent, $matches)) {
$appsData = json_decode($matches[1]);
} else {
returnServerError("Could not parse JS variable ($targetVariable) in page content.");
2018-03-07 11:24:33 +01:00
}
2017-07-17 15:45:58 +02:00
foreach($appsData as $id => $element) {
2017-07-17 15:45:58 +02:00
$appType = $element->type;
$appIsBuyable = 0;
$appHasDiscount = 0;
$appIsFree = 0;
2017-07-17 15:45:58 +02:00
if($element->subs) {
$appIsBuyable = 1;
2017-07-17 15:45:58 +02:00
if($element->subs[0]->discount_pct) {
2017-07-17 15:45:58 +02:00
$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;
2017-07-17 15:45:58 +02:00
2018-03-07 11:24:33 +01:00
} else {
2017-07-17 15:45:58 +02:00
2018-03-07 11:24:33 +01:00
if($this->getInput('only_discount')) {
continue;
}
2017-07-17 15:45:58 +02:00
$appPrice = $element->subs[0]->price / 100;
}
} else {
if($this->getInput('only_discount')) {
continue;
}
if(isset($element->free) && $element->free = 1) {
$appIsFree = 1;
2018-03-07 11:24:33 +01:00
}
}
2017-07-17 15:45:58 +02:00
$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));
2017-07-17 15:45:58 +02:00
}
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;
2018-03-07 11:24:33 +01:00
$this->items[] = $item;
2017-07-17 15:45:58 +02:00
}
2018-03-07 11:24:33 +01:00
array_multisort($sort, SORT_ASC, $this->items);
2017-07-17 15:45:58 +02:00
}
}