Skip to content

Instantly share code, notes, and snippets.

@nxmad
Created December 8, 2017 22:23
Show Gist options
  • Save nxmad/043278348151eae26e3f63537c289748 to your computer and use it in GitHub Desktop.
Save nxmad/043278348151eae26e3f63537c289748 to your computer and use it in GitHub Desktop.
<?php
namespace App\Crawlers;
use OTPHP\TOTP;
use App\Models\Item;
class MarketsCrawler extends AbstractCrawler
{
/**
* The list of supported apps.
*
* @var array
*/
protected $apps = [
578080, 730,
];
/**
* Update prices and quantities.
*
* @param null $apps
*/
public function load($apps = null)
{
if (is_null($apps)) {
$apps = $this->apps;
}
if (is_string($apps)) {
$apps = [$apps];
}
foreach ($apps as $app)
{
$items = Item::where('app', $app)
->get();
$opskins = $this->tryGet('https://api.opskins.com/IPricing/GetAllLowestListPrices/v1/', [
'appid' => $app,
])->response;
$bitskins = $this->tryGet('https://bitskins.com/api/v1/get_price_data_for_items_on_sale/', [
'app_id' => $app,
'api_key' => env('BITSKINS_KEY'),
'code' => $this->generateBitskinsCode(),
])->data->items;
foreach ($items as $item)
{
$ops = isset($opskins->{$item->title}) ? $opskins->{$item->title} : false;
$bit = array_first($bitskins, function ($filter) use ($item) {
return $filter->market_hash_name === $item->title;
}, false);
$data = [
'prices' => [
'opskins' => $ops ? $ops->price : null,
'bitskins' => $bit && $bit->total_items > 0 ? intval($bit->lowest_price * 100) : null,
],
'quantities' => [
'opskins' => $ops ? $ops->quantity : 0,
'bitskins' => $bit ? $bit->total_items : 0,
],
];
$data['price'] = $this->choosePrice($data['prices'], $data['quantities']);
$item->update($data);
}
}
}
/**
* Choose lowest obtainable price.
*
* @param $prices
* @param $quantities
*
* @return null|int
*/
protected function choosePrice($prices, $quantities)
{
$min = null;
foreach ($prices as $provider => $price)
{
if (is_null($min) || $price < $min)
{
if ($quantities[$provider] > 0) {
$min = $price;
}
}
}
return $min ? intval($min * .95) : null;
}
/**
* Generate TOTP for Bitskins.com
*
* @return string
*/
protected function generateBitskinsCode()
{
return TOTP::create(env('BITSKINS_OTP'))->now();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment