2014-05-26 00:30:46 +02:00
|
|
|
<?php
|
2017-02-11 16:16:56 +01:00
|
|
|
class TwitterBridge extends BridgeAbstract {
|
|
|
|
const NAME = 'Twitter Bridge';
|
|
|
|
const URI = 'https://twitter.com/';
|
|
|
|
const CACHE_TIMEOUT = 300; // 5min
|
|
|
|
const DESCRIPTION = 'returns tweets';
|
2016-12-17 16:43:47 +01:00
|
|
|
const MAINTAINER = 'pmaziere';
|
2017-02-11 16:16:56 +01:00
|
|
|
const PARAMETERS = array(
|
|
|
|
'global' => array(
|
|
|
|
'nopic' => array(
|
|
|
|
'name' => 'Hide profile pictures',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Activate to hide profile pictures in content'
|
2017-04-22 15:36:25 +02:00
|
|
|
),
|
|
|
|
'noimg' => array(
|
|
|
|
'name' => 'Hide images in tweets',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Activate to hide images in tweets'
|
2017-02-11 16:16:56 +01:00
|
|
|
)
|
|
|
|
),
|
|
|
|
'By keyword or hashtag' => array(
|
|
|
|
'q' => array(
|
|
|
|
'name' => 'Keyword or #hashtag',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'rss-bridge, #rss-bridge',
|
|
|
|
'title' => 'Insert a keyword or hashtag'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'By username' => array(
|
|
|
|
'u' => array(
|
|
|
|
'name' => 'username',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'sebsauvage',
|
|
|
|
'title' => 'Insert a user name'
|
|
|
|
),
|
|
|
|
'norep' => array(
|
|
|
|
'name' => 'Without replies',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Only return initial tweets'
|
2017-03-17 18:41:35 +01:00
|
|
|
),
|
|
|
|
'noretweet' => array(
|
|
|
|
'name' => 'Without retweets',
|
|
|
|
'required' => false,
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Hide retweets'
|
2017-02-11 16:16:56 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2015-11-05 16:50:18 +01:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
public function getName(){
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($this->queriedContext) {
|
2017-02-11 16:16:56 +01:00
|
|
|
case 'By keyword or hashtag':
|
|
|
|
$specific = 'search ';
|
|
|
|
$param = 'q';
|
|
|
|
break;
|
|
|
|
case 'By username':
|
|
|
|
$specific = '@';
|
|
|
|
$param = 'u';
|
|
|
|
break;
|
2017-02-14 22:20:55 +01:00
|
|
|
default: return parent::getName();
|
2017-02-11 16:16:56 +01:00
|
|
|
}
|
|
|
|
return 'Twitter ' . $specific . $this->getInput($param);
|
|
|
|
}
|
2016-08-25 01:49:30 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
public function getURI(){
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($this->queriedContext) {
|
2017-02-11 16:16:56 +01:00
|
|
|
case 'By keyword or hashtag':
|
|
|
|
return self::URI
|
|
|
|
. 'search?q='
|
|
|
|
. urlencode($this->getInput('q'))
|
|
|
|
. '&f=tweets';
|
|
|
|
case 'By username':
|
|
|
|
return self::URI
|
2017-07-03 19:52:56 +02:00
|
|
|
. urlencode($this->getInput('u'));
|
|
|
|
// Always return without replies!
|
|
|
|
// . ($this->getInput('norep') ? '' : '/with_replies');
|
2017-02-14 22:36:33 +01:00
|
|
|
default: return parent::getURI();
|
2017-02-11 16:16:56 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-25 01:49:30 +02:00
|
|
|
|
2016-08-25 01:24:53 +02:00
|
|
|
public function collectData(){
|
2016-07-08 19:06:35 +02:00
|
|
|
$html = '';
|
2016-08-28 00:01:42 +02:00
|
|
|
|
2016-09-25 23:22:33 +02:00
|
|
|
$html = getSimpleHTMLDOM($this->getURI());
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!$html) {
|
|
|
|
switch($this->queriedContext) {
|
2016-08-28 00:01:42 +02:00
|
|
|
case 'By keyword or hashtag':
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('No results for this query.');
|
2016-08-28 00:01:42 +02:00
|
|
|
case 'By username':
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Requested username can\'t be found.');
|
2016-08-28 00:01:42 +02:00
|
|
|
}
|
2014-05-26 00:30:46 +02:00
|
|
|
}
|
|
|
|
|
2016-08-28 01:25:33 +02:00
|
|
|
$hidePictures = $this->getInput('nopic');
|
2016-08-10 10:44:23 +02:00
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($html->find('div.js-stream-tweet') as $tweet) {
|
2017-03-17 18:41:35 +01:00
|
|
|
|
|
|
|
// Skip retweets?
|
|
|
|
if($this->getInput('noretweet')
|
2017-07-29 19:28:00 +02:00
|
|
|
&& $tweet->getAttribute('data-screen-name') !== $this->getInput('u')) {
|
2017-03-17 18:41:35 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-18 23:18:41 +02:00
|
|
|
// remove 'invisible' content
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($tweet->find('.invisible') as $invisible) {
|
2017-06-18 23:18:41 +02:00
|
|
|
$invisible->outertext = '';
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:26:32 +02:00
|
|
|
// Skip protmoted tweets
|
2017-08-03 17:56:39 +02:00
|
|
|
$heading = $tweet->previousSibling();
|
2017-08-03 00:26:32 +02:00
|
|
|
if(!is_null($heading) &&
|
2017-08-03 17:56:39 +02:00
|
|
|
$heading->getAttribute('class') === 'promoted-tweet-heading'
|
|
|
|
) {
|
2017-08-03 00:26:32 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-22 18:55:59 +02:00
|
|
|
$item = array();
|
2014-05-26 00:30:46 +02:00
|
|
|
// extract username and sanitize
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['username'] = $tweet->getAttribute('data-screen-name');
|
2014-05-26 00:30:46 +02:00
|
|
|
// extract fullname (pseudonym)
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['fullname'] = $tweet->getAttribute('data-name');
|
2016-08-10 10:26:29 +02:00
|
|
|
// get author
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['author'] = $item['fullname'] . ' (@' . $item['username'] . ')';
|
2014-05-26 00:30:46 +02:00
|
|
|
// get avatar link
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['avatar'] = $tweet->find('img', 0)->src;
|
2014-05-26 00:30:46 +02:00
|
|
|
// get TweetID
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['id'] = $tweet->getAttribute('data-tweet-id');
|
2016-07-08 19:06:35 +02:00
|
|
|
// get tweet link
|
2017-06-19 00:19:52 +02:00
|
|
|
$item['uri'] = self::URI . substr($tweet->find('a.js-permalink', 0)->getAttribute('href'), 1);
|
2014-05-26 00:30:46 +02:00
|
|
|
// extract tweet timestamp
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['timestamp'] = $tweet->find('span.js-short-timestamp', 0)->getAttribute('data-time');
|
2016-08-10 10:26:29 +02:00
|
|
|
// generate the title
|
2017-07-05 18:29:11 +02:00
|
|
|
$item['title'] = strip_tags($this->fixAnchorSpacing($tweet->find('p.js-tweet-text', 0), '<a>'));
|
2016-07-08 19:06:35 +02:00
|
|
|
|
2017-04-22 15:36:25 +02:00
|
|
|
$this->processContentLinks($tweet);
|
|
|
|
$this->processEmojis($tweet);
|
2016-08-09 21:47:29 +02:00
|
|
|
|
2016-08-09 21:59:55 +02:00
|
|
|
// get tweet text
|
2017-02-11 16:16:56 +01:00
|
|
|
$cleanedTweet = str_replace(
|
|
|
|
'href="/',
|
|
|
|
'href="' . self::URI,
|
|
|
|
$tweet->find('p.js-tweet-text', 0)->innertext
|
|
|
|
);
|
2016-08-09 21:47:29 +02:00
|
|
|
|
2017-06-18 23:18:41 +02:00
|
|
|
// fix anchors missing spaces in-between
|
|
|
|
$cleanedTweet = $this->fixAnchorSpacing($cleanedTweet);
|
|
|
|
|
2016-08-10 10:44:23 +02:00
|
|
|
// Add picture to content
|
|
|
|
$picture_html = '';
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!$hidePictures) {
|
2016-08-10 10:44:23 +02:00
|
|
|
$picture_html = <<<EOD
|
2017-02-11 16:16:56 +01:00
|
|
|
<a href="https://twitter.com/{$item['username']}">
|
|
|
|
<img
|
2017-04-22 15:36:25 +02:00
|
|
|
style="align:top; width:75px; border:1px solid black;"
|
2017-02-11 16:16:56 +01:00
|
|
|
alt="{$item['username']}"
|
|
|
|
src="{$item['avatar']}"
|
|
|
|
title="{$item['fullname']}" />
|
|
|
|
</a>
|
2016-08-10 10:44:23 +02:00
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
2017-04-22 15:36:25 +02:00
|
|
|
// Add embeded image to content
|
|
|
|
$image_html = '';
|
|
|
|
$image = $this->getImageURI($tweet);
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!$this->getInput('noimg') && !is_null($image)) {
|
2017-04-22 15:36:25 +02:00
|
|
|
// add enclosures
|
2017-05-02 21:45:22 +02:00
|
|
|
$item['enclosures'] = array($image . ':orig');
|
2017-04-22 15:36:25 +02:00
|
|
|
|
|
|
|
$image_html = <<<EOD
|
2017-05-02 21:45:22 +02:00
|
|
|
<a href="{$image}:orig">
|
2017-04-22 15:36:25 +02:00
|
|
|
<img
|
|
|
|
style="align:top; max-width:558px; border:1px solid black;"
|
2017-05-02 21:45:22 +02:00
|
|
|
src="{$image}:thumb" />
|
2017-04-22 15:36:25 +02:00
|
|
|
</a>
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
2016-08-10 10:44:23 +02:00
|
|
|
// add content
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['content'] = <<<EOD
|
2016-08-09 22:05:42 +02:00
|
|
|
<div style="display: inline-block; vertical-align: top;">
|
2016-08-10 10:44:23 +02:00
|
|
|
{$picture_html}
|
2016-08-09 22:05:42 +02:00
|
|
|
</div>
|
|
|
|
<div style="display: inline-block; vertical-align: top;">
|
|
|
|
<blockquote>{$cleanedTweet}</blockquote>
|
|
|
|
</div>
|
2017-04-22 15:36:25 +02:00
|
|
|
<div style="display: block; vertical-align: top;">
|
|
|
|
<blockquote>{$image_html}</blockquote>
|
|
|
|
</div>
|
|
|
|
EOD;
|
|
|
|
|
|
|
|
// add quoted tweet
|
|
|
|
$quotedTweet = $tweet->find('div.QuoteTweet', 0);
|
2017-07-29 19:28:00 +02:00
|
|
|
if($quotedTweet) {
|
2017-04-22 15:36:25 +02:00
|
|
|
// get tweet text
|
|
|
|
$cleanedQuotedTweet = str_replace(
|
|
|
|
'href="/',
|
|
|
|
'href="' . self::URI,
|
|
|
|
$quotedTweet->find('div.tweet-text', 0)->innertext
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->processContentLinks($quotedTweet);
|
|
|
|
$this->processEmojis($quotedTweet);
|
|
|
|
|
|
|
|
// Add embeded image to content
|
|
|
|
$quotedImage_html = '';
|
|
|
|
$quotedImage = $this->getQuotedImageURI($tweet);
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!$this->getInput('noimg') && !is_null($quotedImage)) {
|
2017-04-22 15:36:25 +02:00
|
|
|
// add enclosures
|
2017-05-02 21:45:22 +02:00
|
|
|
$item['enclosures'] = array($quotedImage . ':orig');
|
2017-04-22 15:36:25 +02:00
|
|
|
|
|
|
|
$quotedImage_html = <<<EOD
|
2017-05-02 21:45:22 +02:00
|
|
|
<a href="{$quotedImage}:orig">
|
2017-04-22 15:36:25 +02:00
|
|
|
<img
|
|
|
|
style="align:top; max-width:558px; border:1px solid black;"
|
2017-05-02 21:45:22 +02:00
|
|
|
src="{$quotedImage}:thumb" />
|
2017-04-22 15:36:25 +02:00
|
|
|
</a>
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item['content'] = <<<EOD
|
|
|
|
<div style="display: inline-block; vertical-align: top;">
|
|
|
|
<blockquote>{$cleanedQuotedTweet}</blockquote>
|
|
|
|
</div>
|
|
|
|
<div style="display: block; vertical-align: top;">
|
|
|
|
<blockquote>{$quotedImage_html}</blockquote>
|
|
|
|
</div>
|
|
|
|
<hr>
|
|
|
|
{$item['content']}
|
2016-08-09 21:47:29 +02:00
|
|
|
EOD;
|
2017-04-22 15:36:25 +02:00
|
|
|
}
|
2016-08-09 21:47:29 +02:00
|
|
|
|
2014-05-26 00:30:46 +02:00
|
|
|
// put out
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 15:36:25 +02:00
|
|
|
|
|
|
|
private function processEmojis($tweet){
|
|
|
|
// process emojis (reduce size)
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($tweet->find('img.Emoji') as $img) {
|
2017-04-22 15:36:25 +02:00
|
|
|
$img->style .= ' height: 1em;';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function processContentLinks($tweet){
|
|
|
|
// processing content links
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($tweet->find('a') as $link) {
|
|
|
|
if($link->hasAttribute('data-expanded-url')) {
|
2017-04-22 15:36:25 +02:00
|
|
|
$link->href = $link->getAttribute('data-expanded-url');
|
|
|
|
}
|
|
|
|
$link->removeAttribute('data-expanded-url');
|
|
|
|
$link->removeAttribute('data-query-source');
|
|
|
|
$link->removeAttribute('rel');
|
|
|
|
$link->removeAttribute('class');
|
|
|
|
$link->removeAttribute('target');
|
|
|
|
$link->removeAttribute('title');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-18 23:18:41 +02:00
|
|
|
private function fixAnchorSpacing($content){
|
|
|
|
// fix anchors missing spaces in-between
|
|
|
|
return str_replace(
|
|
|
|
'<a',
|
|
|
|
' <a',
|
|
|
|
$content
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-22 15:36:25 +02:00
|
|
|
private function getImageURI($tweet){
|
|
|
|
// Find media in tweet
|
|
|
|
$container = $tweet->find('div.AdaptiveMedia-container', 0);
|
2017-07-29 19:28:00 +02:00
|
|
|
if($container && $container->find('img', 0)) {
|
2017-04-22 15:36:25 +02:00
|
|
|
return $container->find('img', 0)->src;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getQuotedImageURI($tweet){
|
|
|
|
// Find media in tweet
|
|
|
|
$container = $tweet->find('div.QuoteMedia-container', 0);
|
2017-07-29 19:28:00 +02:00
|
|
|
if($container && $container->find('img', 0)) {
|
2017-04-22 15:36:25 +02:00
|
|
|
return $container->find('img', 0)->src;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2014-05-26 00:30:46 +02:00
|
|
|
}
|