From 61b307a9f93690fca50d9dabe3f83cb0a1513562 Mon Sep 17 00:00:00 2001 From: Dag Date: Sat, 8 Jul 2023 17:07:20 +0200 Subject: [PATCH] fix(tiktok): feed item link (#3511) * fix(tiktok): feed item link * fix(tiktok): support entire url, for convenience --- bridges/TikTokBridge.php | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/bridges/TikTokBridge.php b/bridges/TikTokBridge.php index a426d065..48de2ba5 100644 --- a/bridges/TikTokBridge.php +++ b/bridges/TikTokBridge.php @@ -26,18 +26,6 @@ class TikTokBridge extends BridgeAbstract private $feedName = ''; - public function detectParameters($url) - { - if (preg_match('/tiktok\.com\/(@[\w]+)/', $url, $matches) > 0) { - return [ - 'context' => 'By user', - 'username' => $matches[1] - ]; - } - - return null; - } - public function collectData() { $html = getSimpleHTMLDOM($this->getURI()); @@ -48,10 +36,15 @@ class TikTokBridge extends BridgeAbstract foreach ($html->find('div.tiktok-x6y88p-DivItemContainerV2') as $div) { $item = []; + // todo: find proper link to tiktok item $link = $div->find('a', 0)->href; + $image = $div->find('img', 0)->src; $views = $div->find('strong.video-count', 0)->plaintext; + if ($link === 'https://www.tiktok.com/') { + $link = $this->getURI(); + } $item['uri'] = $link; $a = $div->find('a', 1); @@ -93,10 +86,25 @@ EOD; private function processUsername() { - if (substr($this->getInput('username'), 0, 1) !== '@') { - return '@' . $this->getInput('username'); + $username = trim($this->getInput('username')); + if (preg_match('#^https?://www\.tiktok\.com/@(.*)$#', $username, $m)) { + return '@' . $m[1]; + } + if (substr($username, 0, 1) !== '@') { + return '@' . $username; + } + return $username; + } + + public function detectParameters($url) + { + if (preg_match('/tiktok\.com\/(@[\w]+)/', $url, $matches) > 0) { + return [ + 'context' => 'By user', + 'username' => $matches[1] + ]; } - return $this->getInput('username'); + return null; } }