2016-08-10 23:18:35 +02:00
|
|
|
<?php
|
2016-09-10 19:11:09 +02:00
|
|
|
class KununuBridge extends BridgeAbstract {
|
2017-02-11 16:16:56 +01:00
|
|
|
const MAINTAINER = 'logmanoriginal';
|
|
|
|
const NAME = 'Kununu Bridge';
|
|
|
|
const URI = 'https://www.kununu.com/';
|
2016-09-25 17:04:28 +02:00
|
|
|
const CACHE_TIMEOUT = 86400; // 24h
|
2017-02-11 16:16:56 +01:00
|
|
|
const DESCRIPTION = 'Returns the latest reviews for a company and site of your choice.';
|
2016-08-10 23:18:35 +02:00
|
|
|
|
2016-12-17 17:11:58 +01:00
|
|
|
const PARAMETERS = array(
|
|
|
|
'global' => array(
|
2017-02-11 16:16:56 +01:00
|
|
|
'site' => array(
|
|
|
|
'name' => 'Site',
|
|
|
|
'type' => 'list',
|
|
|
|
'required' => true,
|
|
|
|
'title' => 'Select your site',
|
|
|
|
'values' => array(
|
|
|
|
'Austria' => 'at',
|
|
|
|
'Germany' => 'de',
|
|
|
|
'Switzerland' => 'ch',
|
|
|
|
'United States' => 'us'
|
2016-12-17 17:11:58 +01:00
|
|
|
)
|
2017-02-11 16:16:56 +01:00
|
|
|
),
|
|
|
|
'full' => array(
|
|
|
|
'name' => 'Load full article',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'required' => false,
|
|
|
|
'exampleValue' => 'checked',
|
|
|
|
'title' => 'Activate to load full article'
|
2016-12-17 17:11:58 +01:00
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
2017-02-11 16:16:56 +01:00
|
|
|
'company' => array(
|
|
|
|
'name' => 'Company',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'kununu-us',
|
|
|
|
'title' => 'Insert company name (i.e. Kununu US) or URI path (i.e. kununu-us)'
|
2016-12-17 17:11:58 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2016-08-10 23:18:35 +02:00
|
|
|
|
2016-12-17 18:04:21 +01:00
|
|
|
private $companyName = '';
|
2016-08-29 12:20:18 +02:00
|
|
|
|
2016-12-17 17:11:58 +01:00
|
|
|
public function getURI(){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_null($this->getInput('company')) && !is_null($this->getInput('site'))) {
|
2016-12-17 18:04:21 +01:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$company = $this->fixCompanyName($this->getInput('company'));
|
2016-12-17 18:04:21 +01:00
|
|
|
$site = $this->getInput('site');
|
|
|
|
$section = '';
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($site) {
|
2016-12-17 18:04:21 +01:00
|
|
|
case 'at':
|
|
|
|
case 'de':
|
|
|
|
case 'ch':
|
|
|
|
$section = 'kommentare';
|
|
|
|
break;
|
|
|
|
case 'us':
|
|
|
|
$section = 'reviews';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::URI . $site . '/' . $company . '/' . $section;
|
2016-12-17 17:11:58 +01:00
|
|
|
}
|
2016-08-29 12:20:18 +02:00
|
|
|
|
2016-12-17 18:04:21 +01:00
|
|
|
return parent::getURI();
|
2016-12-17 17:11:58 +01:00
|
|
|
}
|
2016-08-29 12:20:18 +02:00
|
|
|
|
2016-12-17 17:11:58 +01:00
|
|
|
function getName(){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_null($this->getInput('company'))) {
|
2017-02-11 16:16:56 +01:00
|
|
|
$company = $this->fixCompanyName($this->getInput('company'));
|
|
|
|
return ($this->companyName ?: $company) . ' - ' . self::NAME;
|
2016-12-17 18:04:21 +01:00
|
|
|
}
|
|
|
|
|
2017-02-14 22:20:55 +01:00
|
|
|
return parent::getName();
|
2016-12-17 17:11:58 +01:00
|
|
|
}
|
2016-08-10 23:18:35 +02:00
|
|
|
|
2016-08-29 12:20:18 +02:00
|
|
|
public function collectData(){
|
2016-12-17 17:11:58 +01:00
|
|
|
$full = $this->getInput('full');
|
2016-08-10 23:18:35 +02:00
|
|
|
|
|
|
|
// Load page
|
2016-12-17 18:04:21 +01:00
|
|
|
$html = getSimpleHTMLDOMCached($this->getURI());
|
2016-08-29 12:20:18 +02:00
|
|
|
if(!$html)
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Unable to receive data from ' . $this->getURI() . '!');
|
2016-08-10 23:18:35 +02:00
|
|
|
// Update name for this request
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->companyName = $this->extractCompanyName($html);
|
2016-08-10 23:18:35 +02:00
|
|
|
|
|
|
|
// Find the section with all the panels (reviews)
|
|
|
|
$section = $html->find('section.kununu-scroll-element', 0);
|
|
|
|
if($section === false)
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Unable to find panel section!');
|
2016-08-10 23:18:35 +02:00
|
|
|
|
|
|
|
// Find all articles (within the panels)
|
|
|
|
$articles = $section->find('article');
|
|
|
|
if($articles === false || empty($articles))
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Unable to find articles!');
|
2016-08-10 23:18:35 +02:00
|
|
|
|
|
|
|
// Go through all articles
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($articles as $article) {
|
2016-08-22 18:55:59 +02:00
|
|
|
$item = array();
|
2016-08-10 23:18:35 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$item['author'] = $this->extractArticleAuthorPosition($article);
|
|
|
|
$item['timestamp'] = $this->extractArticleDate($article);
|
|
|
|
$item['title'] = $this->extractArticleRating($article)
|
|
|
|
. ' : '
|
|
|
|
. $this->extractArticleSummary($article);
|
|
|
|
|
|
|
|
$item['uri'] = $this->extractArticleUri($article);
|
2016-08-10 23:18:35 +02:00
|
|
|
|
|
|
|
if($full)
|
2017-02-11 16:16:56 +01:00
|
|
|
$item['content'] = $this->extractFullDescription($item['uri']);
|
2016-08-10 23:18:35 +02:00
|
|
|
else
|
2017-02-11 16:16:56 +01:00
|
|
|
$item['content'] = $this->extractArticleDescription($article);
|
2016-08-10 23:18:35 +02:00
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fixes relative URLs in the given text
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function fixUrl($text){
|
2016-08-30 11:23:55 +02:00
|
|
|
return preg_replace('/href=(\'|\")\//i', 'href="'.self::URI, $text);
|
2016-08-10 23:18:35 +02:00
|
|
|
}
|
|
|
|
|
2016-12-17 18:04:21 +01:00
|
|
|
/*
|
|
|
|
* Returns a fixed version of the provided company name
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function fixCompanyName($company){
|
2016-12-17 18:04:21 +01:00
|
|
|
$company = trim($company);
|
|
|
|
$company = str_replace(' ', '-', $company);
|
|
|
|
$company = strtolower($company);
|
2017-02-11 16:16:56 +01:00
|
|
|
return $this->encodeUmlauts($company);
|
2016-12-17 18:04:21 +01:00
|
|
|
}
|
|
|
|
|
2016-08-10 23:18:35 +02:00
|
|
|
/**
|
|
|
|
* Encodes unmlauts in the given text
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function encodeUmlauts($text){
|
2016-08-10 23:18:35 +02:00
|
|
|
$umlauts = Array("/ä/","/ö/","/ü/","/Ä/","/Ö/","/Ü/","/ß/");
|
|
|
|
$replace = Array("ae","oe","ue","Ae","Oe","Ue","ss");
|
|
|
|
|
|
|
|
return preg_replace($umlauts, $replace, $text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the company name from the review html
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function extractCompanyName($html){
|
2016-12-17 18:04:21 +01:00
|
|
|
$company_name = $html->find('h1[itemprop=name]', 0);
|
2016-12-17 17:21:29 +01:00
|
|
|
if(is_null($company_name))
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Cannot find company name!');
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2016-08-10 23:18:35 +02:00
|
|
|
return $company_name->plaintext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the date from a given article
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function extractArticleDate($article){
|
2016-08-10 23:18:35 +02:00
|
|
|
// They conviniently provide a time attribute for us :)
|
2016-12-17 18:04:21 +01:00
|
|
|
$date = $article->find('meta[itemprop=dateCreated]', 0);
|
2016-12-17 17:21:29 +01:00
|
|
|
if(is_null($date))
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Cannot find article date!');
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2016-12-17 18:04:21 +01:00
|
|
|
return strtotime($date->content);
|
2016-08-10 23:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the rating from a given article
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function extractArticleRating($article){
|
2016-08-10 23:18:35 +02:00
|
|
|
$rating = $article->find('span.rating', 0);
|
2016-12-17 17:21:29 +01:00
|
|
|
if(is_null($rating))
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Cannot find article rating!');
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2016-08-10 23:18:35 +02:00
|
|
|
return $rating->getAttribute('aria-label');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the summary from a given article
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function extractArticleSummary($article){
|
2016-12-17 18:04:21 +01:00
|
|
|
$summary = $article->find('[itemprop=name]', 0);
|
2016-12-17 17:21:29 +01:00
|
|
|
if(is_null($summary))
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Cannot find article summary!');
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2016-08-10 23:18:35 +02:00
|
|
|
return strip_tags($summary->innertext);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URI from a given article
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function extractArticleUri($article){
|
2016-12-17 18:04:21 +01:00
|
|
|
$anchor = $article->find('ku-company-review-more', 0);
|
2016-12-17 17:21:29 +01:00
|
|
|
if(is_null($anchor))
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Cannot find article URI!');
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2016-12-17 18:04:21 +01:00
|
|
|
return self::URI . $anchor->{'review-url'};
|
2016-08-10 23:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the position of the author from a given article
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function extractArticleAuthorPosition($article){
|
2016-12-17 18:04:21 +01:00
|
|
|
// We need to parse the user-content manually
|
|
|
|
$user_content = $article->find('div.user-content', 0);
|
|
|
|
if(is_null($user_content))
|
|
|
|
returnServerError('Cannot find user content!');
|
2016-08-10 23:18:35 +02:00
|
|
|
|
|
|
|
// Go through all h2 elements to find index of required span (I know... it's stupid)
|
|
|
|
$author_position = 'Unknown';
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($user_content->find('div') as $content) {
|
|
|
|
if(stristr(strtolower($content->plaintext), 'position')) { /* This works for at, ch, de, us */
|
2016-12-17 18:04:21 +01:00
|
|
|
$author_position = $content->next_sibling()->plaintext;
|
2016-08-10 23:18:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2016-08-10 23:18:35 +02:00
|
|
|
return $author_position;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the description from a given article
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function extractArticleDescription($article){
|
2016-12-17 18:04:21 +01:00
|
|
|
$description = $article->find('[itemprop=reviewBody]', 0);
|
2016-12-17 17:21:29 +01:00
|
|
|
if(is_null($description))
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Cannot find article description!');
|
2016-08-22 01:25:56 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
return $this->fixUrl($description->innertext);
|
2016-08-10 23:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the full description from a given uri
|
|
|
|
*/
|
2017-02-11 16:16:56 +01:00
|
|
|
private function extractFullDescription($uri){
|
2016-08-10 23:18:35 +02:00
|
|
|
// Load full article
|
2016-09-25 23:22:33 +02:00
|
|
|
$html = getSimpleHTMLDOMCached($uri);
|
2016-08-10 23:18:35 +02:00
|
|
|
if($html === false)
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Could not load full description!');
|
2016-08-10 23:18:35 +02:00
|
|
|
|
|
|
|
// Find the article
|
|
|
|
$article = $html->find('article', 0);
|
2016-12-17 17:21:29 +01:00
|
|
|
if(is_null($article))
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Cannot find article!');
|
2016-08-10 23:18:35 +02:00
|
|
|
|
|
|
|
// Luckily they use the same layout for the review overview and full article pages :)
|
2017-02-11 16:16:56 +01:00
|
|
|
return $this->extractArticleDescription($article);
|
2016-08-10 23:18:35 +02:00
|
|
|
}
|
|
|
|
}
|