Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://angg.twu.net/LUA/youtube.lua -- http://angg.twu.net/LUA/youtube.lua.html -- (find-angg "LUA/youtube.lua") -- -- This is old. -- The new version is here: -- (find-blogme3 "youtube.lua") -- (find-blogme3 "anggdefs.lua" "youtube") youtube_local_videos = {} -- indexed by hash youtube_split_fname0 = function (str) local a, part = str:match "^(.-)(%.part)$" str = a or str local b, ext = str:match "^(.-)(%.%w%w%w?%w?%w?)$" str = b or str local dir, c = str:match "^(.*/)([^/]*)$" str = c or str local d, hash = str:match "^(.-)(...........)$" if hash and hash:match "[^-_A-Za-z0-9]" then d, hash = nil, nil end str = d or str return dir, str, hash, ext, part end youtube_split_url0 = function (li) local url, rest = li:match "^%s*(https?://%S*)%s*(.-)%s*$" if not url then return end local hash, time for key,value in url:gmatch "[?&](%w*)=([^?&]*)" do if key == "v" then hash = value end if key == "t" then time = value end end return hash, time, url, rest end youtube_simplify_local_title = function (str) str = str:match("^(.-)-*$") -- remove trailing "-"s str = str:gsub("_", " ") -- convert "_"s to spaces str = str:match("^(.-)%s*$") -- remove trailing whitespace return str end youtube_make_url = function (hash, time) return "http://www.youtube.com/watch?v=" .. hash end youtube_good_ext = function (ext) if ext == ".mp4" or ext == ".webm" then return true end end youtube_split_fname = function (fname, A) A = A or {} local dir, ftitle, hash, ext = youtube_split_fname0(fname) if not hash then return end if not youtube_good_ext(ext) then return end -- A.fname = A.fname or fname -- A.dir = A.dir or dir A.ftitle = A.ftitle or ftitle A.hash = A.hash or hash A.part = A.part or part -- A.title = A.title or youtube_simplify_local_title(A.ftitle) A.url = youtube_make_url(A.hash) return A, A.hash end youtube_add_url = function (li) local hash, time, url, rest = youtube_split_url0(li) if not hash then return end local A = youtube_local_videos[hash] if not A then A = {} A.hash = hash A.title = rest A.url = youtube_make_url(hash) -- or just URL else A.title = rest -- URL line titles are better than local file titles end A.mentioned = true -- for when we want a list of videos pointed to return A end youtube_add_urls = function (bigstr) for _,li in ipairs(splitlines(bigstr)) do youtube_add_url(li) end end youtube_add_local_video = function (li) local A = youtube_split_fname(li) if not A then return end youtube_local_videos[A.hash] = A return A end youtube_add_local_videos = function (bigstr) for _,li in ipairs(splitlines(bigstr)) do youtube_add_local_video(li) end end youtube_add_local_videos_run = function (cmd) youtube_add_local_videos(getoutput(cmd)) end youtube_local_videos_print = function () for k,A in pairs(youtube_local_videos) do print(A.url.." "..A.title) end end HREF = function (url, str) return "<a href=\""..url.."\">"..str.."</a>" end yurl_line = function (li) local A = youtube_add_url(li) if A then if A.fname then return "("..HREF(A.url, "YT")..") " .. HREF(A.fname, A.title) else return "("..HREF(A.url, "YT")..") " .. A.title end else return li end end yurl_lines = function (bigstr) return (bigstr:gsub("[^\n]+", yurl_line)) end demoinput = [[ http://www.youtube.com/watch?v=aLrZ2Q1mzuU Belly Roll http://www.youtube.com/watch?v=XEmKb7ZZYHs Chest Circles http://www.youtube.com/watch?v=zqZNZHUr6B0 Chest Lifts http://www.youtube.com/watch?v=Qn1uodvpzf0 Choo-Choo Shimmy http://www.youtube.com/watch?v=x7z7TRJWQ0U Double Hip Locks http://www.youtube.com/watch?v=22222222222 Not a local video http://www.youtube.com/watch?v=x7z7TRJWQ0U Double Hip Locks etc ]] --[[ -- (find-THLfile "bd/") -- (find-sh "rm -Rv ~/TH/L/bd/; mkdir ~/TH/L/bd/") -- (find-sh "cp -lv /sda5/videos/bellydance/* ~/TH/L/bd/") -- (find-fline "/sda5/videos/bellydance/") -- (find-blogme3 "anggdefs.lua" "basic-words-for-html") -- (find-TH "tmp") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "youtube.lua" ls = getoutput("cd ~/TH/L/ && ls bd/*") youtube_add_local_videos(ls) youtube_add_urls(demoinput) = yurl_line "http://www.youtube.com/watch?v=Qn1uodvpzf0 Choo-Choo Shimmy" = yurl_lines(demoinput) for _,li in isplitlines(demoinput) do local A = youtube_add_url(li) -- print(li) if A and A.fname then print("("..HREF(A.url, "YT")..") "..HREF(A.fname, A.title)) else print(li) end -- PP(A) end -- (find-fline "/sda5/videos/bellydance/") -- (find-sh "ls /sda5/videos/bellydance/*") -- (find-THLsh "ls bd/*") ls = getoutput("ls /sda5/videos/bellydance/*") ls = getoutput("cd ~/TH/L/ && ls bd/*") = ls for _, li in ipairs(splitlines(ls)) do local A, hash = youtube_split_fname(li) if hash then youtube_local_videos[hash] = A print(A.url.." "..A.title) end -- PP(youtube_split_fname0(li)) end http://www.youtube.com/watch?v=EnA1R25vK_4 Basic Egyptian http://www.youtube.com/watch?v=EYb-tPt_F4o Basic Posture http://www.youtube.com/watch?v=BfFhpmGN3bs Belly Isolation http://www.youtube.com/watch?v=aLrZ2Q1mzuU Belly Roll http://www.youtube.com/watch?v=XEmKb7ZZYHs Chest Circles http://www.youtube.com/watch?v=zqZNZHUr6B0 Chest Lifts h = youtube_split_url "http://www.youtube.com/watch?v=M4bMFdLH3FU" PP(youtube_local_videos[h]) --]] -- Local Variables: -- coding: raw-text-unix -- End: