Skip to content

Instantly share code, notes, and snippets.

@Bellov
Created March 27, 2017 11:06
Show Gist options
  • Save Bellov/00ab5d9f01c3c2694f26b4cffee7cf15 to your computer and use it in GitHub Desktop.
Save Bellov/00ab5d9f01c3c2694f26b4cffee7cf15 to your computer and use it in GitHub Desktop.
mapping
<?php
namespace Oferti\Console\Commands;
use Illuminate\Console\Command;
use Oferti\Models\Listing;
use Oferti\Models\Parther;
use Oferti\Models\Category;
use Oferti\Models\City;
use Oferti\Models\CategoryMapping;
use DB;
class LapniParser extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'parser:lapni';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Parser for Lapni.bg';
/**
* The console command description.
*
* @var string[]
*/
protected $uniqueCategories = [];
protected $categoryMap = [];
protected $cityMap = [];
const DEFAULT_CATEGORY = 13;
const DEFAULT_CITY = 'София';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$xml = simplexml_load_string(file_get_contents('http://www.lapni.bg/outer/affiliate_feed/3145'));
$lapnibg = [];
$this->loadMap();
foreach($xml as $item){
$lapni = [
'title' => (string)$item->title,
'type' => (string) $item->type,
'img' => (string) $item->img,
'link' => (string) $item->link,
'endtime' => (string) $item->endtime,
'categories' => $this->parseCategory($item),
'description' => (string) $item->description,
'sold' => (string) $item->sold,
'prices' => [
'orig' => (string) $item->prices->orig,
'lapni' => (string) $item->prices->lapni,
'discount' => (string) $item->prices->discount,
],
'cities' => [
'city' => (int) $item->cities->city,
],
];
var_dump($lapni['cities']);
die('avada kedavra');
/*----------------------------------------------------------*/
$lapnibgg = Listing::firstOrNew(['title' => $lapni['title']]);
$lapnibgg->description = $lapni['description'];
$lapnibgg->original_price = $lapni['prices']['orig'];
$lapnibgg->discouted_price = $lapni['prices']['discount'];
$lapnibgg->url= $lapni['link'];
$lapnibgg->title= $lapni['title'];
$lapnibgg->expires_at = $lapni['endtime'];
$lapnibgg->categories()->sync($this->mapCategories($lapni['categories']));
$lapnibgg->partner_id= 1;
$saved = $lapnibgg->save();
$lapnibg[] = $lapni;
}
$this->addNewToMap();
/*dd($lapnibg);*/
}
private function parseCategory($item) {
if ( !is_object($item) ) {
return [];
}
$category = (string)$item->category;
$categories = (string)$item->categories;
if ( !empty($categories) ) {
return explode(',',$categories);
}
if ( !empty($category) ) {
return [$category];
}
return [];
}
private function mapCategories($externalCategories) {
$categories = [];
foreach ( $externalCategories as $category ) {
if(!array_key_exists($category, $this->categoryMap)) {
echo "found new $category\n";
$this->uniqueCategories[$category] = true;
$categories[] = static::DEFAULT_CATEGORY;
continue;
}
$categories[] = $this->categoryMap[$category];
}
return array_unique($categories);
}
private function mapCities($item) {
if ( !is_object($item) ) {
return [];
}
}
private function loadMap() {
//Load category map
$mappings = CategoryMapping::all()->toArray();
foreach ( $mappings as $mapping ) {
$this->categoryMap[$mapping['external_name']] = $mapping['category_id'];
}
//test city mapping
/*$cities = City::all()->toArray();
foreach ( $cities as $city ) {
$this->cityMap[$city['name']] = $city['id'];
} */
}
private function addNewToMap() {
foreach ( $this->uniqueCategories as $category=>$null ) {
$model = new CategoryMapping([
'category_id' => static::DEFAULT_CATEGORY,
'external_name' => $category
]);
$model->save();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment