Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VimeoBridge] Add new bridge #933

Merged
merged 1 commit into from
Jun 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions bridges/VimeoBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

class VimeoBridge extends BridgeAbstract {

const NAME = 'Vimeo Bridge';
const URI = 'https://vimeo.com/';
const DESCRIPTION = 'Returns search results from Vimeo';
const MAINTAINER = 'logmanoriginal';

const PARAMETERS = array(
array(
'q' => array(
'name' => 'Search Query',
'type' => 'text',
'required' => true
),
'type' => array(
'name' => 'Show results for',
'type' => 'list',
'defaultValue' => 'Videos',
'values' => array(
'Videos' => 'search',
'On Demand' => 'search/ondemand',
'People' => 'search/people',
'Channels' => 'search/channels',
'Groups' => 'search/groups'
)
)
)
);

public function getURI() {
if(($query = $this->getInput('q'))
&& ($type = $this->getInput('type'))) {
return self::URI . $type . '/sort:latest?q=' . $query;
}

return parent::getURI();
}

public function collectData() {

$html = getSimpleHTMLDOM($this->getURI(),
$header = array(),
$opts = array(),
$lowercase = true,
$forceTagsClosed = true,
$target_charset = DEFAULT_TARGET_CHARSET,
$stripRN = false, // We want to keep newline characters
$defaultBRText = DEFAULT_BR_TEXT,
$defaultSpanText = DEFAULT_SPAN_TEXT)
or returnServerError('Could not request ' . $this->getURI());

$json = null; // Holds the JSON data

/**
* Search results are included as JSON formatted string inside a script
* tag that has the variable 'vimeo.config'. The data is condensed into
* a single line of code, so we can just search for the newline.
*
* Everything after "vimeo.config = _extend((vimeo.config || {}), " is
* the JSON formatted string.
*/
foreach($html->find('script') as $script) {
foreach(explode("\n", $script) as $line) {
$line = trim($line);

if(strpos($line, 'vimeo.config') !== 0)
continue;

// 45 = strlen("vimeo.config = _extend((vimeo.config || {}), ");
// 47 = 45 + 2, because we don't want the final ");"
$json = json_decode(substr($line, 45, strlen($line) - 47));
}
}

if(is_null($json)) {
returnClientError('No results for this query!');
}

foreach($json->api->initial_json->data as $element) {
switch($element->type) {
case 'clip': $this->addClip($element); break;
case 'ondemand': $this->addOnDemand($element); break;
case 'people': $this->addPeople($element); break;
case 'channel': $this->addChannel($element); break;
case 'group': $this->addGroup($element); break;

default: returnServerError('Unknown type: ' . $element->type);
}
}

}

private function addClip($element) {
$item = array();

$item['uri'] = $element->clip->link;
$item['title'] = $element->clip->name;
$item['author'] = $element->clip->user->name;
$item['timestamp'] = strtotime($element->clip->created_time);

$item['enclosures'] = array(
end($element->clip->pictures->sizes)->link
);

$item['content'] = "<img src={$item['enclosures'][0]} />";

$this->items[] = $item;
}

private function addOnDemand($element) {
$item = array();

$item['uri'] = $element->ondemand->link;
$item['title'] = $element->ondemand->name;

// Only for films
if(isset($element->ondemand->film))
$item['timestamp'] = strtotime($element->ondemand->film->release_time);

$item['enclosures'] = array(
end($element->ondemand->pictures->sizes)->link
);

$item['content'] = "<img src={$item['enclosures'][0]} />";

$this->items[] = $item;
}

private function addPeople($element) {
$item = array();

$item['uri'] = $element->people->link;
$item['title'] = $element->people->name;

$item['enclosures'] = array(
end($element->people->pictures->sizes)->link
);

$item['content'] = "<img src={$item['enclosures'][0]} />";

$this->items[] = $item;
}

private function addChannel($element) {
$item = array();

$item['uri'] = $element->channel->link;
$item['title'] = $element->channel->name;

$item['enclosures'] = array(
end($element->channel->pictures->sizes)->link
);

$item['content'] = "<img src={$item['enclosures'][0]} />";

$this->items[] = $item;
}

private function addGroup($element) {
$item = array();

$item['uri'] = $element->group->link;
$item['title'] = $element->group->name;

$item['enclosures'] = array(
end($element->group->pictures->sizes)->link
);

$item['content'] = "<img src={$item['enclosures'][0]} />";

$this->items[] = $item;
}
}