2022-06-19 04:59:39 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-19 04:59:39 +03:00
|
|
|
class TikTokBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const NAME = 'TikTok Bridge';
|
|
|
|
const URI = 'https://www.tiktok.com';
|
|
|
|
const DESCRIPTION = 'Returns posts';
|
|
|
|
const MAINTAINER = 'VerifiedJoseph';
|
|
|
|
const PARAMETERS = [
|
|
|
|
'By user' => [
|
|
|
|
'username' => [
|
|
|
|
'name' => 'Username',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => '@tiktok',
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2022-06-19 04:59:39 +03:00
|
|
|
]];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-19 04:59:39 +03:00
|
|
|
const TEST_DETECT_PARAMETERS = [
|
|
|
|
'https://www.tiktok.com/@tiktok' => [
|
|
|
|
'context' => 'By user', 'username' => '@tiktok'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2022-06-19 04:59:39 +03:00
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-19 04:59:39 +03:00
|
|
|
const CACHE_TIMEOUT = 900; // 15 minutes
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-19 04:59:39 +03:00
|
|
|
private $feedName = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-19 04:59:39 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
|
|
|
$html = getSimpleHTMLDOM($this->getURI());
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-09-06 01:14:20 +03:00
|
|
|
$title = $html->find('h1', 0)->plaintext ?? self::NAME;
|
|
|
|
$this->feedName = htmlspecialchars_decode($title);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-19 04:59:39 +03:00
|
|
|
foreach ($html->find('div.tiktok-x6y88p-DivItemContainerV2') as $div) {
|
|
|
|
$item = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-08 18:07:20 +03:00
|
|
|
// todo: find proper link to tiktok item
|
2022-06-19 04:59:39 +03:00
|
|
|
$link = $div->find('a', 0)->href;
|
2023-07-08 18:07:20 +03:00
|
|
|
|
2022-06-19 04:59:39 +03:00
|
|
|
$image = $div->find('img', 0)->src;
|
|
|
|
$views = $div->find('strong.video-count', 0)->plaintext;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-08 18:07:20 +03:00
|
|
|
if ($link === 'https://www.tiktok.com/') {
|
|
|
|
$link = $this->getURI();
|
|
|
|
}
|
2022-06-19 04:59:39 +03:00
|
|
|
$item['uri'] = $link;
|
2023-07-02 07:40:25 +03:00
|
|
|
|
|
|
|
$a = $div->find('a', 1);
|
|
|
|
if ($a) {
|
|
|
|
$item['title'] = $a->plaintext;
|
|
|
|
} else {
|
|
|
|
$item['title'] = $this->getName();
|
|
|
|
}
|
2022-06-19 04:59:39 +03:00
|
|
|
$item['enclosures'][] = $image;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-19 04:59:39 +03:00
|
|
|
$item['content'] = <<<EOD
|
|
|
|
<a href="{$link}"><img src="{$image}"/></a>
|
|
|
|
<p>{$views} views<p>
|
|
|
|
EOD;
|
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-19 04:59:39 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
|
|
|
switch ($this->queriedContext) {
|
|
|
|
case 'By user':
|
|
|
|
return self::URI . '/' . $this->processUsername();
|
|
|
|
default:
|
|
|
|
return parent::getURI();
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-19 04:59:39 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
switch ($this->queriedContext) {
|
|
|
|
case 'By user':
|
|
|
|
return $this->feedName . ' (' . $this->processUsername() . ') - TikTok';
|
|
|
|
default:
|
|
|
|
return parent::getName();
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-06-19 04:59:39 +03:00
|
|
|
private function processUsername()
|
|
|
|
{
|
2023-07-08 18:07:20 +03:00
|
|
|
$username = trim($this->getInput('username'));
|
|
|
|
if (preg_match('#^https?://www\.tiktok\.com/@(.*)$#', $username, $m)) {
|
|
|
|
return '@' . $m[1];
|
|
|
|
}
|
|
|
|
if (substr($username, 0, 1) !== '@') {
|
|
|
|
return '@' . $username;
|
2022-06-19 04:59:39 +03:00
|
|
|
}
|
2023-07-08 18:07:20 +03:00
|
|
|
return $username;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-08 18:07:20 +03:00
|
|
|
public function detectParameters($url)
|
|
|
|
{
|
|
|
|
if (preg_match('/tiktok\.com\/(@[\w]+)/', $url, $matches) > 0) {
|
|
|
|
return [
|
|
|
|
'context' => 'By user',
|
|
|
|
'username' => $matches[1]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2022-06-19 04:59:39 +03:00
|
|
|
}
|
|
|
|
}
|