|
| 1 | +import argparse |
| 2 | + |
| 3 | + |
| 4 | +def main() -> None: |
| 5 | + parser = argparse.ArgumentParser() |
| 6 | + parser.add_argument("--max-results", type=int, default=10) |
| 7 | + args = parser.parse_args() |
| 8 | + max_results = max(1, min(args.max_results, 50)) |
| 9 | + |
| 10 | + js = r"""(() => { |
| 11 | + const clean = (value) => String(value || '').replace(/\s+/g, ' ').trim(); |
| 12 | + const firstUrl = (value) => { |
| 13 | + if (typeof value === 'string' && value) return value; |
| 14 | + if (Array.isArray(value)) { |
| 15 | + for (const item of value) { |
| 16 | + const result = firstUrl(item); |
| 17 | + if (result) return result; |
| 18 | + } |
| 19 | + } |
| 20 | + if (value && typeof value === 'object') { |
| 21 | + for (const key of ['url', 'src', 'srcNoWatermark', 'playUrl']) { |
| 22 | + const result = firstUrl(value[key]); |
| 23 | + if (result) return result; |
| 24 | + } |
| 25 | + } |
| 26 | + return null; |
| 27 | + }; |
| 28 | + const isoTime = (value) => { |
| 29 | + const timestamp = Number(value); |
| 30 | + if (!Number.isFinite(timestamp) || timestamp <= 0) return null; |
| 31 | + const date = new Date(timestamp < 100000000000 ? timestamp * 1000 : timestamp); |
| 32 | + return Number.isNaN(date.getTime()) ? null : date.toISOString(); |
| 33 | + }; |
| 34 | + const stateValues = Object.values(window.INIT_STATE || {}); |
| 35 | + const state = stateValues.find((value) => value && Array.isArray(value.feeds)) || {feeds: []}; |
| 36 | + const items = state.feeds.map((feed) => { |
| 37 | + if (!feed || typeof feed !== 'object') return null; |
| 38 | + const photo = feed.photo && typeof feed.photo === 'object' ? feed.photo : null; |
| 39 | + if (!photo || !clean(photo.id)) return null; |
| 40 | + const author = feed.author && typeof feed.author === 'object' ? feed.author : {}; |
| 41 | + const comment = feed.comment && typeof feed.comment === 'object' ? feed.comment : {}; |
| 42 | + const photoId = clean(photo.id); |
| 43 | + const caption = clean(photo.caption); |
| 44 | + const coverUrl = firstUrl(photo.coverUrl); |
| 45 | + const playUrl = firstUrl(photo.manifestH265) || firstUrl(photo.manifest); |
| 46 | + const statistics = { |
| 47 | + like_count: photo.likeCount, |
| 48 | + comment_count: comment.us_c, |
| 49 | + collect_count: photo.collectCount, |
| 50 | + view_count: photo.viewCount, |
| 51 | + share_count: photo.shareCount, |
| 52 | + }; |
| 53 | + Object.keys(statistics).forEach((key) => { |
| 54 | + if (statistics[key] === null || statistics[key] === undefined) delete statistics[key]; |
| 55 | + }); |
| 56 | + return { |
| 57 | + title: caption || `Kuaishou video ${photoId}`, |
| 58 | + content: caption, |
| 59 | + author: clean(author.name), |
| 60 | + author_id: clean(author.id) || null, |
| 61 | + author_avatar: firstUrl(author.headerUrl), |
| 62 | + url: `https://www.kuaishou.com/short-video/${encodeURIComponent(photoId)}`, |
| 63 | + photo_id: photoId, |
| 64 | + create_time: photo.timestamp || null, |
| 65 | + published_at: isoTime(photo.timestamp), |
| 66 | + cover_url: coverUrl, |
| 67 | + play_url: playUrl, |
| 68 | + statistics, |
| 69 | + media: { |
| 70 | + type: 'video', |
| 71 | + play_url: playUrl, |
| 72 | + cover_url: coverUrl, |
| 73 | + duration_ms: photo.duration || null, |
| 74 | + width: photo.width || null, |
| 75 | + height: photo.height || null, |
| 76 | + }, |
| 77 | + tags: Array.isArray(feed.tags) |
| 78 | + ? feed.tags.map((tag) => clean(tag && tag.name)).filter(Boolean) |
| 79 | + : [], |
| 80 | + }; |
| 81 | + }).filter(Boolean).slice(0, MAX_RESULTS); |
| 82 | + return {count: items.length, items}; |
| 83 | +})()""" |
| 84 | + print(js.replace("MAX_RESULTS", str(max_results))) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + main() |
0 commit comments