/ clis / tiktok / search.js
search.js
 1  import { cli } from '@jackwener/opencli/registry';
 2  cli({
 3      site: 'tiktok',
 4      name: 'search',
 5      description: 'Search TikTok videos',
 6      domain: 'www.tiktok.com',
 7      args: [
 8          { name: 'query', required: true, positional: true, help: 'Search query' },
 9          { name: 'limit', type: 'int', default: 10, help: 'Number of results' },
10      ],
11      columns: ['rank', 'desc', 'author', 'url', 'plays', 'likes', 'comments', 'shares'],
12      pipeline: [
13          { navigate: { url: 'https://www.tiktok.com/explore', settleMs: 5000 } },
14          { evaluate: `(async () => {
15    const query = \${{ args.query | json }};
16    const limit = \${{ args.limit }};
17    const res = await fetch('/api/search/general/full/?keyword=' + encodeURIComponent(query) + '&offset=0&count=' + limit + '&aid=1988', { credentials: 'include' });
18    if (!res.ok) throw new Error('Search failed: HTTP ' + res.status);
19    const data = await res.json();
20    const items = (data.data || []).filter(function(i) { return i.type === 1 && i.item; });
21    return items.slice(0, limit).map(function(i, idx) {
22      var v = i.item;
23      var a = v.author || {};
24      var s = v.stats || {};
25      return {
26        rank: idx + 1,
27        desc: (v.desc || '').replace(/\\n/g, ' ').substring(0, 100),
28        author: a.uniqueId || '',
29        url: (a.uniqueId && v.id) ? 'https://www.tiktok.com/@' + a.uniqueId + '/video/' + v.id : '',
30        plays: s.playCount || 0,
31        likes: s.diggCount || 0,
32        comments: s.commentCount || 0,
33        shares: s.shareCount || 0,
34      };
35    });
36  })()
37  ` },
38      ],
39  });