-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCiteamParser.php
172 lines (151 loc) · 4.76 KB
/
CiteamParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
class CiteamParser
{
private $dealsGroups = array();
public function getDealsGroups()
{
$mainPage = DealsStatsEnv::getEnvConfig('citeam.base-url');
$mainPage .= DealsStatsEnv::getEnvConfig('citeam.main-page');
$site = WebPageHarvester::harvestPage($mainPage);
phpQuery::newDocument($site['html']);
foreach( pq('ul.cityList li a') as $li )
{
$name = pq($li)->text();
$url = pq($li)->attr('href');
$this->dealsGroups[$name]['url'] = $url;
}
return $this->dealsGroups;
}
public function getGroupOffers($groupName)
{
if(!isset($this->dealsGroups[$groupName]))
throw new Exception("Unknown group: $groupName", 1);
$page = DealsStatsEnv::getEnvConfig('citeam.base-url');
$page .= $this->dealsGroups[$groupName]['url'] . '.atom';
$page = WebPageHarvester::harvestPage($page);
// print_r($page['httpCode'].' - '.strlen($page['html']));
phpQuery::newDocument($page['html']);
foreach( pq('entry') as $entry )
{
$url = pq($entry)->find('link')->attr('href');
$details = $this->getOfferDetails($url);
$this->dealsGroups[$groupName]['offers']
[$details['id']] = $details;
}
return $this->dealsGroups[$groupName]['offers'];
}
/**
* Metoda sluzy do pobierania informacji o nowej ofercie
* Zalozenie jest takie, ze wczesniej nie mielismy o niej zadnych danych
* Szczegolna uwage nalezy zwrocic na bledy sugerujace zmiane kodu strony.
* W takim przypadku rzucamy blad i nie zapisujemy danych.
*/
public function getOfferDetails($url)
{
// echo "\tgetting info for $url\n";
$page = WebPageHarvester::harvestPage($url);
phpQuery::newDocument($page['html']);
/**
* Id Oferty
*
* Szukany znacznik:
* <a class="buyNow" href="/transakcje/new?offer=621" title="Kup Teraz!"
* rel="nofollow">Kup teraz!</a>
*/
$offerId = pq("a.buyNow[href^='/transakcje/new?offer']")->attr("href");
$offerId = preg_find('/\d+$/', $offerId);
if(empty($offerId))
{
// Jesli oferta nie jest aktywna, oznaczone jest to przez znaczik:
// <span class="buyNow inactive" title="Kup Teraz!">
// w takim przypadku szukamy id oferty w linkach do obrazkow.
// Nie powinno sie to jednak zdazyc przy nowych ofertach.
trigger_error("[CITEAM PARSER] Nowa oferta ($url) bez id".
" i aktywnego 'Kup teraz!'", E_USER_WARNING);
if(pq("span.buyNow.inactive")->size() === 1)
{
$offerId = pq("div.offerContent>img[src*='main_offer_photos']")
->attr('src');
$offerId = preg_find('/main_offer_photos\/(\d+)\//',
$offerId, 1);
}
}
// Nie udalo sie wykryc Id oferty
if(empty($offerId))
throw new Exception("Empty OfferId!", 1);
/**
* Tytul oferty
*
* W czystej postaci wystepuje w znacznikach dla Facebooka
*/
$offerTitle = pq("meta[property='og:title']")->attr('content');
if(empty($offerTitle))
throw new Exception("Empty OfferTitle!", 1);
/**
* Status oferty
*
* Oferta moze byc jeszcze dostepna (ending time), ale bez mozliwosci
* kupowania kuponow (wyprzedana)
*/
$offerStatus = 'active';
if(pq("a.buyNow[href^='/transakcje/new?offer']")->size() === 0
&& pq("span.buyNow.inactive")->size() === 1)
{
$offerStatus = 'inactive';
}
/**
* Ilosc sprzedanych kuponow
*
* Znacznik:
* <div class="box amount">
* <span class="counter">
*/
$offerSold = trim(pq("div.box.amount span.counter")->html());
// var_dump(pq("div.box.amount span.counter")->html());
// var_dump($offerSold);
if(!is_numeric($offerSold))
throw new Exception("Wrong 'offers sold' value", 1);
$offerSold = (int)$offerSold;
/**
* Cena
*
* Znacznik:
* <div class="priceBox">
* <div class="prices">
* <span class="promoPrice">199 </span>
* <span class="regPrice">420 </span>
*/
$price = pq("div.priceBox div span");
$offerPricePromo = trim($price->filter(".promoPrice")->html());
$offerPriceReg = trim($price->filter(".regPrice")->html());
$offerPricePromo = preg_find('/\d+(,\d+)?/', $offerPricePromo);
$offerPriceReg = preg_find('/\d+(,\d+)?/', $offerPriceReg);
if(empty($offerPricePromo) || empty($offerPriceReg))
throw new Exception("Wrong price value", 1);
/**
* Data waznosci
*
* Znacznik:
* <div id="countDown" data-ending-time="2011-05-08">
*/
$endingTime = pq("#countDown")->attr('data-ending-time');
$endingTime = strtotime($endingTime);
return array(
'id' => $offerId,
'title' => $offerTitle,
'sold' => $offerSold,
'pricePromo' => $offerPricePromo,
'priceRegular' => $offerPriceReg,
'status' => $offerStatus,
'endingTime' => $endingTime,
);
/* echo
"\tId:\t\t$offerId\n".
"\tTitle:\t\t$offerTitle\n".
"\tSold:\t\t$offerSold\n".
"\tPrice promo:\t$offerPricePromo\n".
"\tPrice reg:\t$offerPriceReg\n".
"\tStatus:\t\t$offerStatus\n\n";
*/
}
}