• http.Fetch Loop Help
    6 replies, posted
Hey guys sorry for bothering again with my stupid problems So i am trying to make for some days the YoutubeAPI to work and i am stuck on 1 part again so here is the code [CODE]concommand.Add( "TestYoutubeAPI", function() FullYoutubeURL = "https://www.youtube.com/watch?v=qpgTC9MDx1o" VideoID = string.Replace(FullYoutubeURL,"https://www.youtube.com/watch?v=","") http.Fetch("http://serve01.mp3skull.onl/get?id="..VideoID, function(body, len, headers, code) print ("-----------------------") PrintTable(headers) print ("-----------------------") print (len) print ("-----------------------") print (code) print ("-----------------------") print (body) print ("-----------------------") sound.PlayURL("http://serve01.mp3skull.onl/get?id=" ..VideoID, "", function(AudioURL) if IsValid(AudioURL) then LocalPlayer():ChatPrint("Valid") else LocalPlayer():ChatPrint("Invalid") end end) end) end)[/CODE] Here is what i get from headers, len, code, body before it finish converting [CODE]----------------------- Access-Control-Allow-Origin = * Cache-Control = public, max-age=0 Connection = keep-alive Content-Encoding = gzip Content-Type = text/html; charset=UTF-8 Date = Thu, 22 Sep 2016 18:32:21 GMT ETag = W/"e51-1571ec72f58" Last-Modified = Mon, 12 Sep 2016 14:22:32 GMT Server = nginx Transfer-Encoding = chunked Vary = Accept-Encoding ----------------------- 3665 ----------------------- 200 ----------------------- <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"> <link rel="icon" href="https://mp3skull.onl/frontend/templates_bootstrap_ajax/img/mp3skull.ico" type="image/x-icon"> <link rel="stylesheet" href="bower/bootstrap/dist/css/bootstrap.min.css" /> <script src="bower/angularjs/angular.min.js"></script> <script src="bower/underscore/underscore-min.js"></script> <script src="/bower/jquery/dist/jquery.min.js"></script> <script type="text/javascript"> var config = {root: '/api/youtube'}; </script> <script src="js/referrer.js"></script> <script src="js/app.js"></script> <script src="js/config/ban.js"></script> <script src="js/directives/converter/directive.js"></script> <script src="js/directives/converter-btn/directive.js"></script> <script src="js/directives/converter/controller.js"></script> <script src="js/directives/converter-btn/controller.js"></script> <meta name=viewport content="width=device-width, initial-scale=1.0, maximum-scale=1.0"> <title>MP3Skull - YouTube to MP3</title> <style> .visible-150{display:none;} @media (min-width: 1200px) { body{padding:20px 50px;} } @media (max-width: 150px) { .hidden-150{display:none;} .visible-150{display:block;} } .overlay-anchor{ position:absolute; top:0px; left:0px; width:100%; height:100%; background-color:rgba(255,255,255,0.0); cursor:default; } .overlay-anchor:hover{ background-color:rgba(255,255,255,0.2); } </style> </head> <body ng-app="converter"> <div class="row visible-lg-block hidden-150"> <div class="col-md-12 text-center"> <h1 class="text-center">MP3Skull</h1> <ul class="breadcrumb text-center" vocab="http://schema.org/" typeof="BreadcrumbList"> <li property="itemListElement" typeof="ListItem"> <a href="/" property="item" typeof="WebPage"> <span property="name">MP3Skull</span> </a> <meta property="position" content="1"> </li> <li class="active" property="itemListElement" typeof="ListItem"> <a href="/en/home/" property="item" typeof="WebPage"> <span property="name">Free MP3 Download &amp; YouTube Converter</span> </a> <meta property="position" content="2"> </li> </ul> </div> <div class="col-md-12"> <p class="alert alert-info"><i class="fa fa-download"></i> <b>MP3Skull</b> is now downloading and converting your wanted music - please wait a few seconds</p> </div> </div> <div style="position:relative;" class="hidden-150"> <converter-frame></converter-frame> </div> <div class="container visible-lg-block hidden-150"> <br><br> <div class="row text-center"> <div class="col-md-12"> <a href="https://mp3skull.onl/">Visit MP3Skull.onl for more downloads!</a> </div> </div> </div> <div class="visible-150"> <converter-btn></converter-btn> </div> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-58847518-32', 'auto'); ga('send', 'pageview'); </script> </body> </html> ----------------------- Invalid[/CODE] And here is what i get when its done converting and i can use it to play the music, After the fetch runs and sound.PlayURL fails i need to wait a couple seconds to finish converting and than run the script again so the music can start playing [CODE]----------------------- Access-Control-Allow-Origin = * Connection = keep-alive Content-Length = 4481068 Content-Type = audio/mpeg Date = Thu, 22 Sep 2016 18:32:56 GMT Server = nginx/1.2.1 ----------------------- 4481068 ----------------------- 200 ----------------------- ID3 ----------------------- Valid [/CODE] So what i wanna do is "if body is ID3 do sound.PlayURL... else fetch http again" like a loop is it possible to do this ?. Or maybe i do something wrong ? i don't know i am really noob if you have any ideas please let me know Thanks and sorry :ohno:
[CODE]local function thing(url) http.Fetch( url, function(body) print(body) end, function(err) print(err) thing(url) end, ) end [/CODE]
Why are you nesting two HTTP requests? PlayURL already fetches the URL so you're basically doing the same twice. Try this: [code] function fetchAudioURL(AudioURL) --recursive function sound.PlayURL("http://serve01.mp3skull.onl/get?id=" ..VideoID, "", function(AudioURL) --try to play the URL if IsValid(AudioURL) then LocalPlayer():ChatPrint("Valid") --if valid then we're done (base case) else timer.Simple( 1, fetchAudioURL(AudioURL) ) --otherwise, wait a sensible amount of time and try again (n case) end end) end [/code] You might want to add some check to exit in case the URL is always invalid (the site is 404, for example) as otherwise you will get a stack overflow at some point.
[CODE] function FetchAudio( AudioURL ) sound.PlayURL( AudioURL, "mono", function( station, errorid, errorstr ) if IsValid( station ) then station:Play() else print( "Error: "..errorid..": "..errorstr.." with following url: "..AudioURL ) end end) end [/CODE] I wouldn't make a loop because most likely any error is going to be its an invalid audio source, this code works as for your mp3 skull stuff you would just concatenate the two strings together and put it in this function or make the function do it for you like he is doing it.
[QUOTE=chikkenslayer;51094614][CODE] function FetchAudio( AudioURL ) sound.PlayURL( AudioURL, "mono", function( station, errorid, errorstr ) if IsValid( station ) then station:Play() else print( "Error: "..errorid..": "..errorstr.." with following url: "..AudioURL ) end end) end [/CODE] I wouldn't make a loop because most likely any error is going to be its an invalid audio source, this code works as for your mp3 skull stuff you would just concatenate the two strings together and put it in this function or make the function do it for you like he is doing it.[/QUOTE] Read the OP again, he is using a website that takes a while to process the video into audio, and the only way to get progress OR the mp3 is to fetch the same URL over and over until you get the audio back instead of HTML.
[lua] concommand.Add( "TestYoutubeAPI", GetYoutube ) function GetYoutube(ply, cmd, args, str) FullYoutubeURL = "https://www.youtube.com/watch?v=qpgTC9MDx1o" VideoID = string.Replace(FullYoutubeURL,"https://www.youtube.com/watch?v=","") http.Fetch("http://serve01.mp3skull.onl/get?id="..VideoID, function(body, len, headers, code) print ("-----------------------") PrintTable(headers) print ("-----------------------") print (len) print ("-----------------------") print (code) print ("-----------------------") print (body) print ("-----------------------") sound.PlayURL("http://serve01.mp3skull.onl/get?id=" ..VideoID, "", function(AudioURL) if IsValid(AudioURL) then LocalPlayer():ChatPrint("Valid") else LocalPlayer():ChatPrint("Invalid") GetYoutube(ply, cmd, args, str) end end) end) end [/lua] [editline]23rd September 2016[/editline] Sorry for he awful formatting, there's only so much I can do on a phone
[QUOTE=meharryp;51095945][lua] concommand.Add( "TestYoutubeAPI", GetYoutube ) function GetYoutube(ply, cmd, args, str) FullYoutubeURL = "https://www.youtube.com/watch?v=qpgTC9MDx1o" VideoID = string.Replace(FullYoutubeURL,"https://www.youtube.com/watch?v=","") http.Fetch("http://serve01.mp3skull.onl/get?id="..VideoID, function(body, len, headers, code) print ("-----------------------") PrintTable(headers) print ("-----------------------") print (len) print ("-----------------------") print (code) print ("-----------------------") print (body) print ("-----------------------") sound.PlayURL("http://serve01.mp3skull.onl/get?id=" ..VideoID, "", function(AudioURL) if IsValid(AudioURL) then LocalPlayer():ChatPrint("Valid") else LocalPlayer():ChatPrint("Invalid") GetYoutube(ply, cmd, args, str) end end) end) end [/lua] [editline]23rd September 2016[/editline] Sorry for he awful formatting, there's only so much I can do on a phone[/QUOTE] If you actually typed part of that code on a phone you deserve a fucking medal.
Sorry, you need to Log In to post a reply to this thread.