2017-08-06 21:29:21 +03:00
|
|
|
<?php
|
|
|
|
class DribbbleBridge extends BridgeAbstract {
|
|
|
|
|
|
|
|
const MAINTAINER = 'quentinus95';
|
|
|
|
const NAME = 'Dribbble popular shots';
|
|
|
|
const URI = 'https://dribbble.com';
|
|
|
|
const CACHE_TIMEOUT = 1800;
|
|
|
|
const DESCRIPTION = 'Returns the newest popular shots from Dribbble.';
|
|
|
|
|
2018-10-26 19:07:34 +03:00
|
|
|
public function getIcon() {
|
|
|
|
return 'https://cdn.dribbble.com/assets/
|
|
|
|
favicon-63b2904a073c89b52b19aa08cebc16a154bcf83fee8ecc6439968b1e6db569c7.ico';
|
|
|
|
}
|
|
|
|
|
2017-08-06 21:29:21 +03:00
|
|
|
public function collectData(){
|
2020-10-09 22:46:40 +03:00
|
|
|
$html = getSimpleHTMLDOM(self::URI)
|
2017-08-06 21:29:21 +03:00
|
|
|
or returnServerError('Error while downloading the website content');
|
|
|
|
|
|
|
|
$json = $this->loadEmbeddedJsonData($html);
|
|
|
|
|
|
|
|
foreach($html->find('li[id^="screenshot-"]') as $shot) {
|
2019-11-01 20:06:38 +03:00
|
|
|
$item = array();
|
2017-08-06 21:29:21 +03:00
|
|
|
|
|
|
|
$additional_data = $this->findJsonForShot($shot, $json);
|
|
|
|
if ($additional_data === null) {
|
|
|
|
$item['uri'] = self::URI . $shot->find('a', 0)->href;
|
2020-07-25 06:58:42 +03:00
|
|
|
$item['title'] = $shot->find('.shot-title', 0)->plaintext;
|
2017-08-06 21:29:21 +03:00
|
|
|
} else {
|
|
|
|
$item['timestamp'] = strtotime($additional_data['published_at']);
|
|
|
|
$item['uri'] = self::URI . $additional_data['path'];
|
|
|
|
$item['title'] = $additional_data['title'];
|
|
|
|
}
|
|
|
|
|
2020-07-25 06:58:42 +03:00
|
|
|
$item['author'] = trim($shot->find('.user-information .display-name', 0)->plaintext);
|
2017-08-06 21:29:21 +03:00
|
|
|
|
|
|
|
$description = $shot->find('.comment', 0);
|
|
|
|
$item['content'] = $description === null ? '' : $description->plaintext;
|
|
|
|
|
2020-10-09 22:46:40 +03:00
|
|
|
$preview_path = $shot->find('figure img', 1)->attr['data-srcset'];
|
2017-08-06 21:29:21 +03:00
|
|
|
$item['content'] .= $this->getImageTag($preview_path, $item['title']);
|
2019-11-01 20:06:38 +03:00
|
|
|
$item['enclosures'] = array($this->getFullSizeImagePath($preview_path));
|
2017-08-06 21:29:21 +03:00
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loadEmbeddedJsonData($html){
|
2019-11-01 20:06:38 +03:00
|
|
|
$json = array();
|
2017-08-06 21:29:21 +03:00
|
|
|
$scripts = $html->find('script');
|
|
|
|
|
|
|
|
foreach($scripts as $script) {
|
|
|
|
if(strpos($script->innertext, 'newestShots') !== false) {
|
|
|
|
// fix single quotes
|
2020-07-25 06:58:42 +03:00
|
|
|
$script->innertext = preg_replace('/\'(.*)\'(,?)$/im', '"\1"\2', $script->innertext);
|
2017-08-06 21:29:21 +03:00
|
|
|
|
|
|
|
// fix JavaScript JSON (why do they not adhere to the standard?)
|
2020-07-25 06:58:42 +03:00
|
|
|
$script->innertext = preg_replace('/^(\s*)(\w+):/im', '\1"\2":', $script->innertext);
|
|
|
|
|
|
|
|
// fix relative dates, so they are recognized by strtotime
|
|
|
|
$script->innertext = preg_replace('/"about ([0-9]+ hours? ago)"(,?)$/im', '"\1"\2', $script->innertext);
|
2017-08-06 21:29:21 +03:00
|
|
|
|
|
|
|
// find beginning of JSON array
|
|
|
|
$start = strpos($script->innertext, '[');
|
|
|
|
|
|
|
|
// find end of JSON array, compensate for missing character!
|
|
|
|
$end = strpos($script->innertext, '];') + 1;
|
|
|
|
|
|
|
|
// convert JSON to PHP array
|
|
|
|
$json = json_decode(substr($script->innertext, $start, $end - $start), true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $json;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function findJsonForShot($shot, $json){
|
|
|
|
foreach($json as $element) {
|
|
|
|
if(strpos($shot->getAttribute('id'), (string)$element['id']) !== false) {
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getImageTag($preview_path, $title){
|
|
|
|
return sprintf(
|
|
|
|
'<br /> <a href="%s"><img src="%s" alt="%s" /></a>',
|
|
|
|
$this->getFullSizeImagePath($preview_path),
|
|
|
|
$preview_path,
|
|
|
|
$title
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getFullSizeImagePath($preview_path){
|
2020-10-15 10:53:19 +03:00
|
|
|
return explode('?compress=1', $preview_path)[0];
|
2017-08-06 21:29:21 +03:00
|
|
|
}
|
|
|
|
}
|