Created
July 5, 2018 09:07
-
-
Save folknor/4d7da1168497eae5eaf404f20c980716 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/lua | |
local _FEEDS = { | |
"https://www.youtube.com/feeds/videos.xml?channel_id=UCzQUP1qoWDoEbmsQxvdjxgQ", -- JRE | |
"https://www.youtube.com/feeds/videos.xml?channel_id=UCNAxrHudMfdzNi6NxruKPLw", -- Sam | |
} | |
local sh = require("sh") | |
local _t = tostring | |
local feedparser = require("feedparser") | |
local lfs = require("lfs") | |
local fd = sh.command("fd") | |
local ytdl = sh.command("youtube-dl") | |
local dkjson = require("dkjson") | |
local logger = sh.command("logger") | |
local podcastFolder = os.getenv("PODCAST_FOLDER") | |
if type(podcastFolder) ~= "string" or #podcastFolder == 0 then | |
logger("-t ytcheck", "'Set PODCAST_FOLDER env var plz folk nub.'") | |
os.exit(1) | |
end | |
if podcastFolder:find("\\") then | |
logger("-t ytcheck", "'folk you idiot, you put \\ in the path again.'") | |
os.exit(1) | |
end | |
if podcastFolder:sub(#podcastFolder) ~= "/" then podcastFolder = podcastFolder .. "/" end | |
local podcastFolderExists = lfs.attributes(podcastFolder, "mode") | |
if not podcastFolderExists then | |
logger("-t ytcheck", "'dafuq PODCAST_FOLDER'") | |
os.exit(1) | |
end | |
lfs.chdir(podcastFolder) | |
for _, feed in next, _FEEDS do | |
local rawFeed = _t(sh.command("curl")("-s", feed)) | |
if rawFeed and type(rawFeed) == "string" and rawFeed ~= "" then | |
local parsed = feedparser.parse(rawFeed) | |
if type(parsed) == "table" and parsed.feed then | |
local name = parsed.feed.title:gsub("[%/%?%<%>%\\%:%*%|%q%.%c]", "") | |
name = name:gsub("%s+$", "") | |
-- Check that the output folder exists | |
local fullPath = podcastFolder .. name | |
lfs.mkdir(fullPath) -- No problem just doing it every time | |
lfs.chdir(fullPath) | |
local toDownload = {} | |
-- Find for any missing videos | |
local vidyas = parsed.entries | |
for _, vidya in next, vidyas do | |
local id = vidya.id and vidya.id:match("^yt:video:([%w%_%-]+)$") | |
if id then | |
local found = _t(fd(id)) | |
if found and found == "" then | |
local json = dkjson.decode(_t(ytdl("-j", vidya.link))) | |
if json and not json.is_live then | |
toDownload[#toDownload+1] = id | |
end | |
end | |
end | |
end | |
logger("-t ytcheck", "'" .. #toDownload .. " downloads queued for " .. name .. "'") | |
-- Fetch! | |
for _, fetch in next, toDownload do | |
logger("-t ytcheck", "'Downloading audio from YT #" .. fetch .. "'") | |
ytdl( | |
"--no-playlist", | |
"--retries 1", | |
"--abort-on-unavailable-fragment", | |
"--no-overwrites", | |
"--no-progress", | |
"--extract-audio", | |
fetch | |
) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment