· 8 years ago · Jan 16, 2017, 12:16 PM
1public function handle() {
2 $oauthToken = '1/4b950e30fb1f92b1a0335a229268d7c0e69dd4f05c0a28d39c2487d825e1173b';
3 $apiUrl = 'http://nokaut.io/api/v2/';
4
5 $config = new Config();
6 $config->setApiAccessToken($oauthToken);
7 $config->setApiUrl($apiUrl);
8
9
10 $apiKit = new ApiKit($config);
11
12 //8519-kosmetyki | delikatesy- 6118 | 5916 - zwierzeta
13 $categories = array(5916, 8519, 6118);
14 $productsRepository = $apiKit->getProductsRepository();
15 $offersRepository = $apiKit->getOffersRepository();
16 $cenobranie = DB::connection('cenobranie');
17 $shops_added = array();
18
19
20 $nokaut_eshops = array(7473,17142,27623,24881,26559,11451,25271,27439,14907,3820,15744,1524,23049,27475,27264,26241,27463,18816,25240,27485,19540,14714,3196,18420,
21 27267,15556,8249,27357,6808,27740,27296,26118,1531,9676,27000,24533,392,1367,19307,20245,6221,18543,15807,24525,27622,27101,14387,1227,22286,
22 22758,26319,21445,632,25175,4342,23210,23273,7350,25770,12500,25959,20674,27719,27582,27334,24471,26254,27519,4894,3885,17005,26552,3243,8181,
23 7967,26327,6345,26530,26380,26033,20417,19579,16293,23672,25695,15613,4006,27280,24858,26967,25748,3004,25475,7000,23219,17391,8262,24998,1967,
24 13082,21499,27262,26748,20674,25096,392,24195,24576,15018,27181,26189,22777,18816,14563,18420,24537,22707,21182,16435,23604,20245,20037,27711,
25 17142,25711,17005,2416,15624,8344,15178,16166,24077,16021,10744,4342,27201,15878,22164,632,22286,7473,22616,26212,11935,25091,27623,26161,23590,
26 14916,20985,23942,13837,26559,25537,14039,1227,26964,15807,26647,10524,25573,24557,19089,20954,2997,25067,534,20417,6811,25631,8129,23195,10252,
27 27025,8882,1257,25801,26592,15551,12654,4414,27361,126,26131,25416,14289,3342,24716,24998,11218,27467,25288,10333,22387,15624,10253,2416,24881,
28 3243,25814,17169,25711,25964,21835,13871,10119,26959,27475,8249,1367,11941,26241,27661,14039,18574,11458,15359,26143,24077,27439,27740,26654,3820,
29 24017,25587,27292,20245,7473,1524,24044,27663,17410,27521,15018,15129,19395,27623,25988,19326,26366,26688,17142,27368,10046,27417,15565,13172,26033,
30 24576,26056,3004,27506,14563,6221,26552,25697,25709,24779,13224,17539,26559,27152,1023,20766,392,27295,25775,12874,22367,25679,6022,26672,25748,22079,
31 13494,9638,24858,27037,23493,6345,4279,8262,27070,25967,22286,27486,7005,22888,25240,14916,7967,3608,25793,25143,27101,26386,25658,3885,16647,22454,
32 4894,22732,27162,20674,1227,27612);
33
34
35 /*
36 $context = stream_context_create(array (
37 'http' => array (
38 'header' => 'Authorization: Basic ' . base64_encode("acdevpages:eVSJBheIKIM-xwd6jB7f")
39 )
40 ));
41 */
42
43 /* do wyrzucenia */
44
45
46 //$bar = $this->output->createProgressBar(count($nokaut_eshops));
47
48
49 foreach (array_chunk($nokaut_eshops, 100) as $values) {
50 echo implode(',', $values) . PHP_EOL;
51 $query = new ProductsQuery($apiUrl);
52 $query->setFields(array('properties', 'product_id', 'title', 'producer_name', 'id', '_metadata.paging'));
53 $query->addFilter(new MultipleWithOperator('shop_id', 'in', $values));
54 $query->addFilter(new MultipleWithOperator('category_ids', 'in', $categories));
55 $products = $productsRepository->fetchProductsByQuery($query);
56
57 $bar = $this->output->createProgressBar($products->getMetadata()->getPaging()->getTotal());
58 for ($i = 1; $i <= $products->getMetadata()->getPaging()->getTotal(); $i++) {
59 if ($i == 1) {
60 $limit = 200;
61 $offset = 0;
62 } else {
63 $limit = 200;
64 $offset = 200 * $i;
65 }
66 $query->setLimit($limit);
67 $query->setOffset($offset);
68 $products = $productsRepository->fetchProductsByQuery($query);
69
70 foreach ($products as $product) {
71 //$prefix1 = substr($product->getPhotoID(), 0, 2);
72 //$prefix2 = substr($product->getPhotoID(), 2, 2);
73
74 /*
75 $categoriesRepository = $apiKit->getCategoriesAsyncRepository();
76 $category = $categoriesRepository->fetchById($product->getCategoryId());
77
78 $category_path = array();
79 for ($i = 1; $i <= $category->getDepth(); $i++) {
80 $category = $categoriesRepository->fetchById($category->getParentId());
81 $category_path[$i] = $category->getTitle();
82 }
83 */
84 $productEan = null;
85 if (count($product->getProperties()) > 0) {
86 foreach ($product->getProperties() as $property) {
87 if ($property->getName() == "EAN") {
88 $productEan = $property->getValue();
89 }
90 }
91 }
92 $offersFetch = $offersRepository->fetchOffersByProductId($product->getId(), array('price', 'description', 'shop_product_id', 'shop.name', 'click_url'));
93
94
95 foreach ($offersFetch as $offer) {
96 $cenobranie->table('products_nokaut')->insert([
97 'product_id' => $offer->getShopProductId(),
98 'shop' => str_slug(str_replace('.', '-', $offer->getShop()->getName())),
99 'url' => 'http://nokaut.click' . $offer->getClickUrl(),
100 'name' => $product->getTitle(),
101 'img' => str_slug($product->getTitle()) . '.jpg',
102 'price' => $offer->getPrice() * 100,
103 //'category' => implode(' / ', array_reverse($category_path)),
104 //'safe_category' => str_slug(implode('-', array_reverse($category_path))),
105 'category' => '',
106 'safe_category' => '',
107 'brand' => $product->getProducerName() ? $product->getProducerName() : 'Brak',
108 'description' => $offer->getDescription() ? $offer->getDescription() : 'Brak',
109 'EAN' => $productEan,
110 'created_at' => date('Y-m-d H:i:s'),
111 'distributor' => 'nokaut',
112 'distributor_id' => $product->getId()
113 ]);
114 //$shop = str_slug(str_replace('.', '-', $offer->getShop()->getName()));
115 //$img = 'p-' . $prefix1 . '-' . $prefix2 . '-' . $product->getPhotoID() . '500x500/' . str_slug($product->getTitle()) . '.jpg';
116
117 //file_get_contents('http://cenobranie.dev06.alleceny.pl/importer/get_image.php?secret=nuyNPrX3YwkYJxc3W5CKMGsuYDkBdw7Yr6cEVA8m&shop=' . $shop . '&img=' . $img, false, $context);
118
119 /*
120 if (!in_array($offer->getShop()->getId(), $shops_added)) {
121
122 $id_new_eshop = DB::connection('alleceny')->table('eshop')->insertGetId([
123 'name' => $offer->getShop()->getName(),
124 'fullname' => $offer->getShop()->getName(),
125 'slug' => str_slug(str_replace('.', '-', $offer->getShop()->getName())),
126 'status' => 0,
127 'integrated' => 0,
128 'distributor_id' => $offer->getShop()->getId()
129 ]);
130 DB::connection('alleceny')->table('cenobranie')->insertGetId([
131 'id_eshop' => $id_new_eshop,
132 'cenobranie_slug' => str_slug(str_replace('.', '-', $offer->getShop()->getName())),
133 ]);
134 $shops_added[] = $offer->getShop()->getId();
135 }
136
137 */
138 // }
139
140
141 }
142
143 }
144 $bar->advance();
145 }
146 $bar->finish();
147 }
148 }