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' => [
|
2023-12-28 15:53:06 +03:00
|
|
|
'username' => [
|
|
|
|
'name' => 'Username',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => '@tiktok',
|
|
|
|
]
|
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
|
|
|
public function collectData()
|
|
|
|
{
|
2023-12-28 15:53:06 +03:00
|
|
|
$html = getSimpleHTMLDOMCached('https://www.tiktok.com/embed/' . $this->processUsername());
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-12-28 15:53:06 +03:00
|
|
|
$author = $html->find('span[data-e2e=creator-profile-userInfo-TUXText]', 0)->plaintext ?? self::NAME;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-12-28 15:53:06 +03:00
|
|
|
$videos = $html->find('div[data-e2e=common-videoList-VideoContainer]');
|
2023-07-09 00:21:55 +03:00
|
|
|
|
2023-12-28 15:53:06 +03:00
|
|
|
foreach ($videos as $video) {
|
2023-07-20 06:50:45 +03:00
|
|
|
$item = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-12-28 15:53:06 +03:00
|
|
|
// Handle link "untracking"
|
|
|
|
$linkParts = parse_url($video->find('a', 0)->href);
|
|
|
|
$link = $linkParts['scheme'] . '://' . $linkParts['host'] . '/' . $linkParts['path'];
|
|
|
|
|
|
|
|
$image = $video->find('video', 0)->poster;
|
|
|
|
$views = $video->find('div[data-e2e=common-Video-Count]', 0)->plaintext;
|
|
|
|
|
|
|
|
$enclosures = [$image];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-20 06:50:45 +03:00
|
|
|
$item['uri'] = $link;
|
2023-12-28 15:53:06 +03:00
|
|
|
$item['title'] = 'Video';
|
|
|
|
$item['author'] = '@' . $author;
|
|
|
|
$item['enclosures'] = $enclosures;
|
2022-06-19 04:59:39 +03:00
|
|
|
$item['content'] = <<<EOD
|
|
|
|
<a href="{$link}"><img src="{$image}"/></a>
|
2023-12-28 15:53:06 +03:00
|
|
|
<p>{$views} views<p><br/>
|
2022-06-19 04:59:39 +03:00
|
|
|
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':
|
2023-12-28 15:53:06 +03:00
|
|
|
return $this->processUsername() . ' - TikTok';
|
2022-06-19 04:59:39 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|