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'
|
2018-12-12 16:56:36 +01:00
|
|
|
),
|
|
|
|
'noimgscaling' => array(
|
|
|
|
'name' => 'Disable image scaling',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Activate to disable image scaling in tweets (keeps original image)'
|
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
|
|
|
)
|
2017-09-24 16:59:45 +02:00
|
|
|
),
|
|
|
|
'By list' => array(
|
|
|
|
'user' => array(
|
|
|
|
'name' => 'User',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'sebsauvage',
|
|
|
|
'title' => 'Insert a user name'
|
|
|
|
),
|
|
|
|
'list' => array(
|
|
|
|
'name' => 'List',
|
|
|
|
'required' => true,
|
|
|
|
'title' => 'Insert the list name'
|
|
|
|
),
|
|
|
|
'filter' => array(
|
|
|
|
'name' => 'Filter',
|
|
|
|
'exampleValue' => '#rss-bridge',
|
|
|
|
'required' => false,
|
|
|
|
'title' => 'Specify term to search for'
|
|
|
|
)
|
2017-02-11 16:16:56 +01:00
|
|
|
)
|
|
|
|
);
|
2015-11-05 16:50:18 +01:00
|
|
|
|
2018-11-26 18:05:41 +01:00
|
|
|
public function detectParameters($url){
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
// By keyword or hashtag (search)
|
|
|
|
$regex = '/^(https?:\/\/)?(www\.)?twitter\.com\/search.*(\?|&)q=([^\/&?\n]+)/';
|
|
|
|
if(preg_match($regex, $url, $matches) > 0) {
|
|
|
|
$params['q'] = urldecode($matches[4]);
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
// By hashtag
|
|
|
|
$regex = '/^(https?:\/\/)?(www\.)?twitter\.com\/hashtag\/([^\/?\n]+)/';
|
|
|
|
if(preg_match($regex, $url, $matches) > 0) {
|
|
|
|
$params['q'] = urldecode($matches[3]);
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
// By list
|
|
|
|
$regex = '/^(https?:\/\/)?(www\.)?twitter\.com\/([^\/?\n]+)\/lists\/([^\/?\n]+)/';
|
|
|
|
if(preg_match($regex, $url, $matches) > 0) {
|
|
|
|
$params['user'] = urldecode($matches[3]);
|
|
|
|
$params['list'] = urldecode($matches[4]);
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
// By username
|
|
|
|
$regex = '/^(https?:\/\/)?(www\.)?twitter\.com\/([^\/?\n]+)/';
|
|
|
|
if(preg_match($regex, $url, $matches) > 0) {
|
|
|
|
$params['u'] = urldecode($matches[3]);
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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-09-24 16:59:45 +02:00
|
|
|
case 'By list':
|
2018-01-12 13:07:40 +01:00
|
|
|
return $this->getInput('list') . ' - Twitter list by ' . $this->getInput('user');
|
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-09-24 16:59:45 +02:00
|
|
|
case 'By list':
|
|
|
|
return self::URI
|
|
|
|
. urlencode($this->getInput('user'))
|
|
|
|
. '/lists/'
|
|
|
|
. str_replace(' ', '-', strtolower($this->getInput('list')));
|
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.');
|
2017-09-24 16:59:45 +02:00
|
|
|
case 'By list':
|
|
|
|
returnServerError('Requested username or list 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')
|
2019-02-04 14:56:07 +01:00
|
|
|
&& strcasecmp($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) {
|
2019-06-01 21:15:30 +02:00
|
|
|
$invisible->remove();
|
2017-06-18 23:18:41 +02:00
|
|
|
}
|
|
|
|
|
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
|
2018-11-15 22:00:01 +01:00
|
|
|
$item['username'] = htmlspecialchars_decode($tweet->getAttribute('data-screen-name'), ENT_QUOTES);
|
2014-05-26 00:30:46 +02:00
|
|
|
// extract fullname (pseudonym)
|
2018-11-15 22:00:01 +01:00
|
|
|
$item['fullname'] = htmlspecialchars_decode($tweet->getAttribute('data-name'), ENT_QUOTES);
|
2016-08-10 10:26:29 +02:00
|
|
|
// get author
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['author'] = $item['fullname'] . ' (@' . $item['username'] . ')';
|
2019-02-04 14:56:07 +01:00
|
|
|
if(strcasecmp($tweet->getAttribute('data-screen-name'), $this->getInput('u'))) {
|
|
|
|
$item['author'] .= ' RT: @' . $this->getInput('u');
|
|
|
|
}
|
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
|
2018-11-15 22:00:01 +01:00
|
|
|
$item['title'] = strip_tags($this->fixAnchorSpacing(htmlspecialchars_decode(
|
|
|
|
$tweet->find('p.js-tweet-text', 0), ENT_QUOTES), '<a>'));
|
2016-07-08 19:06:35 +02:00
|
|
|
|
2017-09-24 16:59:45 +02:00
|
|
|
switch($this->queriedContext) {
|
|
|
|
case 'By list':
|
|
|
|
// Check if filter applies to list (using raw content)
|
2018-01-12 13:07:40 +01:00
|
|
|
if($this->getInput('filter')) {
|
2017-09-24 16:59:45 +02:00
|
|
|
if(stripos($tweet->find('p.js-tweet-text', 0)->plaintext, $this->getInput('filter')) === false) {
|
|
|
|
continue 2; // switch + for-loop!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
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)) {
|
2018-12-12 16:56:36 +01:00
|
|
|
// Set image scaling
|
|
|
|
$image_orig = $this->getInput('noimgscaling') ? $image : $image . ':orig';
|
|
|
|
$image_thumb = $this->getInput('noimgscaling') ? $image : $image . ':thumb';
|
|
|
|
|
2017-04-22 15:36:25 +02:00
|
|
|
// add enclosures
|
2018-12-12 16:56:36 +01:00
|
|
|
$item['enclosures'] = array($image_orig);
|
2017-04-22 15:36:25 +02:00
|
|
|
|
|
|
|
$image_html = <<<EOD
|
2018-12-12 16:56:36 +01:00
|
|
|
<a href="{$image_orig}">
|
2017-04-22 15:36:25 +02:00
|
|
|
<img
|
|
|
|
style="align:top; max-width:558px; border:1px solid black;"
|
2018-12-12 16:56:36 +01: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)) {
|
2018-12-12 16:56:36 +01:00
|
|
|
// Set image scaling
|
|
|
|
$quotedImage_orig = $this->getInput('noimgscaling') ? $quotedImage : $quotedImage . ':orig';
|
|
|
|
$quotedImage_thumb = $this->getInput('noimgscaling') ? $quotedImage : $quotedImage . ':thumb';
|
|
|
|
|
2017-04-22 15:36:25 +02:00
|
|
|
// add enclosures
|
2018-12-12 16:56:36 +01:00
|
|
|
$item['enclosures'] = array($quotedImage_orig);
|
2017-04-22 15:36:25 +02:00
|
|
|
|
|
|
|
$quotedImage_html = <<<EOD
|
2018-12-12 16:56:36 +01:00
|
|
|
<a href="{$quotedImage_orig}">
|
2017-04-22 15:36:25 +02:00
|
|
|
<img
|
|
|
|
style="align:top; max-width:558px; border:1px solid black;"
|
2018-12-12 16:56:36 +01:00
|
|
|
src="{$quotedImage_thumb}" />
|
2017-04-22 15:36:25 +02:00
|
|
|
</a>
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item['content'] = <<<EOD
|
2018-11-12 19:59:46 +01:00
|
|
|
{$item['content']}
|
|
|
|
<hr>
|
2017-04-22 15:36:25 +02:00
|
|
|
<div style="display: inline-block; vertical-align: top;">
|
|
|
|
<blockquote>{$cleanedQuotedTweet}</blockquote>
|
|
|
|
</div>
|
|
|
|
<div style="display: block; vertical-align: top;">
|
|
|
|
<blockquote>{$quotedImage_html}</blockquote>
|
|
|
|
</div>
|
2016-08-09 21:47:29 +02:00
|
|
|
EOD;
|
2017-04-22 15:36:25 +02:00
|
|
|
}
|
2018-11-15 22:00:01 +01:00
|
|
|
$item['content'] = htmlspecialchars_decode($item['content'], ENT_QUOTES);
|
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
|
|
|
}
|