Automate downloading of video entries
authorCameron Ball <cameron@moodle.com>
Thu, 13 Jun 2019 07:54:10 +0000 (15:54 +0800)
committerCameron Ball <cameron@moodle.com>
Thu, 13 Jun 2019 07:54:10 +0000 (15:54 +0800)
app/Main.hs

index d68cc4c..4d1f781 100644 (file)
@@ -10,7 +10,7 @@ import Prelude hiding (init)
 import Data.List (intercalate)
 import Text.JSON
 import Text.JSON.Generic
-import Data.Text hiding (intercalate, unlines, init, length)
+import Data.Text hiding (intercalate, unlines, init, length, splitAt)
 import Data.Time.Clock.POSIX
 import System.File.Tree hiding (mapM, mapM_)
 import System.Directory (doesFileExist)
@@ -61,12 +61,20 @@ entry :: IO ()
 entry = do
   putStrLn "Enter a name for this entry:"
   entryName <- getLine
+  putStrLn "YouTube video ID?"
+  ytUrl <- getLine
+  ytFilename <- if (ytUrl /= "")
+    then putStrLn "Video filename?" >> getLine >>= return . ("::" ++) . (++ ".webm")
+    else return ""
+
   entryNum <- fmap ((+1) . length) $ allFilesIn "entries"
   stamp <- fmap round getPOSIXTime
   let filename = "entries/" ++ (show entryNum) ++ "-" ++ (unpack . (toLower. replace " " "-") $ pack entryName) ++ ".txt"
-  writeFile filename (show stamp ++ "::" ++ entryName ++ "\n")
+  writeFile filename (show stamp ++ "::" ++ entryName ++ ytFilename ++ "\nEntry goes here!\nᚳᚱᛒ\n<a href=\"https://www.youtube.com/watch?v=" ++ ytUrl ++ "\">https://www.youtube.com/watch?v=" ++ ytUrl ++ "</a>")
   putStrLn $ "Created " ++ filename
-  callCommand $ "emacsclient " ++ filename ++ " &"
+  callCommand $ "nano " ++ filename
+  callCommand $ "youtube-dl -f 'webm[height<1080]+bestaudio' \"https://www.youtube.com/watch?v=" ++ ytUrl ++ "\" -o 'build/videos/" ++ (snd . splitAt 2 $ ytFilename)   ++ "'"
+  build
 
 usage :: IO ()
 usage =  putStr . ununlines $ [
@@ -78,9 +86,9 @@ usage =  putStr . ununlines $ [
                        ]
   ]
 
-parse ["init"] = init >> exit
-parse ["build"] = continueIfValidProject build >> exit
-parse ["entry"] = continueIfValidProject entry >> exit
+parse ["init"] = init >> exitSuccess
+parse ["build"] = continueIfValidProject build >> exitSuccess
+parse ["entry"] = continueIfValidProject entry >> exitSuccess
 parse [_] = usage >> exit
 parse [] = usage >> exit