Skip to content

Instantly share code, notes, and snippets.

@MinePlayersPE
Last active August 18, 2022 13:34
Show Gist options
  • Save MinePlayersPE/b70508f42db73479128018a8ea20ad50 to your computer and use it in GitHub Desktop.
Save MinePlayersPE/b70508f42db73479128018a8ea20ad50 to your computer and use it in GitHub Desktop.
tiktok parser/loader for vlc
-- warning: breaks easily because there are so many assumptions made here
-- it's already kinda half broken (randomly loading forever), no clue why
function probe()
return (vlc.access == "http" or vlc.access == "https")
and vlc.path:match("^www%.tiktok%.com/@[^/]+/video/(%d+)")
end
function parse()
local json = require("dkjson")
local aweme_id = vlc.path:match("^www%.tiktok%.com/@[^/]+/video/(%d+)");
-- workaround with less restrictive api
local api_stream = vlc.stream('https://api-h2.tiktokv.com/aweme/v1/aweme/detail/?aweme_id=' .. aweme_id .. '&version_code=220405&device_platform=android&os_version=10&carrier_region=US&aid=1180')
local api_json_str = api_stream:readline();
if (not api_json_str) or api_json_str == '' then
-- retry once
api_stream = vlc.stream('https://api-h2.tiktokv.com/aweme/v1/aweme/detail/?aweme_id=' .. aweme_id .. '&version_code=220405&device_platform=android&os_version=10&carrier_region=US&aid=1180')
api_json_str = api_stream:readline();
end
if (not api_json_str) or api_json_str == '' then vlc.msg.err('Couldn\'t access API, might be blocked') end
local api_json, _, err = json.decode(api_json_str)
if err then vlc.msg.err('Couldn\'t decode JSON: ' .. err) end;
local aweme_detail = api_json.aweme_detail
if not aweme_detail then vlc.msg.err('Couldn\'t find video info for ' .. aweme_id .. ', might be deleted') end
local info = {
name = aweme_detail.desc,
description = aweme_detail.desc,
}
if aweme_detail.author then
info['artist'] = aweme_detail.author.nickname
end
if aweme_detail.cover then
info['art_url'] = aweme_detail.cover.url_list[1]
end
if aweme_detail.video then
local video_urls = aweme_detail.video.play_addr_bytevc1 and aweme_detail.video.play_addr_bytevc1.url_list or aweme_detail.video.play_addr.url_list
for _, url in ipairs(video_urls) do
--[[
-- uncomment these lines to retrieve a permanent (albeit a bit unstable) api url
if url:find('aweme/v1') then
info['path'] = url
break
end
]]
end
if not info['path'] then
info['path'] = video_urls[1]
end
--info['path'] = 'https://api-h2.tiktokv.com/aweme/v1/play/?ratio=default&video_id=' .. aweme_detail.video.play_addr.uri
end
return {info}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment