|
@ -13,7 +13,7 @@ import Data.Aeson (decode) |
|
|
import qualified Data.ByteString.Lazy.Char8 as BL |
|
|
import qualified Data.ByteString.Lazy.Char8 as BL |
|
|
import qualified Data.Text as T |
|
|
import qualified Data.Text as T |
|
|
import Control.Exception |
|
|
import Control.Exception |
|
|
import Network.HTTP.Base (urlEncode) |
|
|
import Network.HTTP.Base (urlEncode, urlDecode) |
|
|
import Types |
|
|
import Types |
|
|
|
|
|
|
|
|
makeURL :: T.Text -> T.Text |
|
|
makeURL :: T.Text -> T.Text |
|
@ -32,7 +32,7 @@ getItems str = |
|
|
|
|
|
|
|
|
cid = "571126085022-7ash7a48cdao0tesqe66ghtime34cfvo.apps.googleusercontent.com" |
|
|
cid = "571126085022-7ash7a48cdao0tesqe66ghtime34cfvo.apps.googleusercontent.com" |
|
|
secret = "FjgeOtSZoJgU87FbuwJf2vwj" |
|
|
secret = "FjgeOtSZoJgU87FbuwJf2vwj" |
|
|
file = "./tokens.txt" |
|
|
file = "/home/wes/.config/youtube-tokens.txt" |
|
|
baseURI = "https://www.googleapis.com/youtube/v3/" |
|
|
baseURI = "https://www.googleapis.com/youtube/v3/" |
|
|
|
|
|
|
|
|
searchRequest :: String -> String -> String |
|
|
searchRequest :: String -> String -> String |
|
@ -43,6 +43,22 @@ searchRequest keyword accessTok = |
|
|
"&type=video&access_token=" ++ |
|
|
"&type=video&access_token=" ++ |
|
|
accessTok |
|
|
accessTok |
|
|
|
|
|
|
|
|
|
|
|
getNewTokens :: OAuth2Client -> IO OAuth2Tokens |
|
|
|
|
|
getNewTokens client = do |
|
|
|
|
|
tokens <- read <$> readFile file |
|
|
|
|
|
newTokens <- refreshTokens client tokens |
|
|
|
|
|
writeFile file (show newTokens) |
|
|
|
|
|
return newTokens |
|
|
|
|
|
|
|
|
|
|
|
findTracks :: OAuth2Client -> String -> String -> IO [SearchResult] |
|
|
|
|
|
findTracks client accessTok term = do |
|
|
|
|
|
response <- (try $ simpleHttp $ searchRequest term accessTok) :: IO (Either HttpException BL.ByteString) |
|
|
|
|
|
case response of |
|
|
|
|
|
(Left _) -> do |
|
|
|
|
|
tokens <- getNewTokens client |
|
|
|
|
|
findTracks client (accessToken tokens) term |
|
|
|
|
|
(Right resp) -> return $ getItems resp |
|
|
|
|
|
|
|
|
search :: String -> IO [SearchResult] |
|
|
search :: String -> IO [SearchResult] |
|
|
search term = do |
|
|
search term = do |
|
|
let client = OAuth2Client { clientId = cid, clientSecret = secret } |
|
|
let client = OAuth2Client { clientId = cid, clientSecret = secret } |
|
@ -57,15 +73,3 @@ search term = do |
|
|
writeFile file (show tokens) |
|
|
writeFile file (show tokens) |
|
|
accessTok <- fmap (accessToken . read) (readFile file) |
|
|
accessTok <- fmap (accessToken . read) (readFile file) |
|
|
findTracks client accessTok (urlEncode term) |
|
|
findTracks client accessTok (urlEncode term) |
|
|
|
|
|
|
|
|
getNewTokens :: OAuth2Client -> IO () |
|
|
|
|
|
getNewTokens client = do |
|
|
|
|
|
tokens <- read <$> readFile file |
|
|
|
|
|
newTokens <- refreshTokens client tokens |
|
|
|
|
|
writeFile file (show newTokens) |
|
|
|
|
|
|
|
|
|
|
|
findTracks client accessTok term = do |
|
|
|
|
|
response <- (try $ simpleHttp $ searchRequest term accessTok) :: IO (Either HttpException BL.ByteString) |
|
|
|
|
|
case response of |
|
|
|
|
|
(Left _) -> getNewTokens client >> findTracks client accessTok term |
|
|
|
|
|
(Right resp) -> return $ getItems resp |
|
|
|
|
|