2. June 2013 04:43
/
Administrator
/
/
Comments (12)
Few weeks ago I decided to add a channel to my Roku box to play live videos from a Free-To-Air Satellite TV channel. This specific channel has a live video stream from their website which requires authentication. My challenge was to build a Roku app using provided SDK to simulate the authentication process of the asp.net website so I can grab the URL of the live stream. (URL has a token attached to it which requires authentication).
To accomplish this I had to make multiple HTTPs calls to the site from my Roku app, and on each call grab some piece of response and use for the next call. Following code snippet shows how URL of the stream is grabbed from a web page after logging in to the site. (As you can see there are some repeated code which you can move to a separate function, and I leave that to you ):
Function GetLiveFeedUrl() as Dynamic
' make sure we have the credentials
sec = CreateObject("roRegistrySection", "credentials")
username = sec.Read("username")
password = sec.Read("password")
if username = "" or password = ""
if NOT EnterCredentials()
return false
end if
end if
result = ""
timeout = 10000
ut = CreateObject("roURLTransfer")
ut.SetPort(CreateObject("roMessagePort"))
' setup the transfer for SSL
ut.SetCertificatesFile("common:/certs/ca-bundle.crt")
ut.InitClientCertificates()
ut.AddHeader("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31")
ut.SetURL("https://www.example.com/live")
if ut.AsyncGetToString()
event = wait(timeout, ut.GetPort())
if type(event) = "roUrlEvent"
print event.GetResponseCode()
result = event.GetString()
else if event = invalid
ut.AsyncCancel()
else
print "roUrlTransfer::AsyncGetToString(): unknown event"
endif
end if
' get my IP cookie from JavaScript source
r = CreateObject("roRegex", "setCookie\(\'YPF8827340282Jdskjhfiw_928937459182JAX666\', \'(.+?)\',", "im")
MyIP = r.Match(result)[1]
' send credentials to login page
LoginData = "UserName=" + ut.Escape(username) + "&Password=" + ut.Escape(password) + "&btnLogin=login"
cookie = "YPF8827340282Jdskjhfiw_928937459182JAX666=" + MyIP + "; DOAReferrer="
ut.AddHeader("Cookie", cookie)
ut.SetURL("https://www.example.com/User/Home/Login")
if ut.AsyncPostFromString(LoginData)
event = wait(timeout, ut.GetPort())
if type(event) = "roUrlEvent"
print event.GetResponseCode()
result = event.GetString()
else if event = invalid
ut.AsyncCancel()
else
print "roUrlTransfer::AsyncGetToString(): unknown event"
endif
end if
' add auth cookies
headers = event.GetResponseHeadersArray()
For Each header in headers
If header[ "Set-Cookie" ] <> invalid Then 'And header[ "Set-Cookie" ].InStr( cookieName + "=" ) = 0 Then
cookie = cookie + "; " + header[ "Set-Cookie" ]
End If
NEXT
ut.AddHeader("Cookie", cookie)
'print cookie
' now try to find the video URL
ut.SetURL("http://www.example.com/live")
if ut.AsyncGetToString()
event = wait(timeout, ut.GetPort())
if type(event) = "roUrlEvent"
print event.GetResponseCode()
result = event.GetString()
else if event = invalid
ut.AsyncCancel()
else
print "roUrlTransfer::AsyncGetToString(): unknown event"
endif
end if
q = chr(34)
r = CreateObject("roRegex", "(http://.+\.m3u8\?token=[^" +q+ "]+)", "im")
VideoURL = r.Match(result)[1]
print VideoURL
return VideoURL
End Function
Note: This was part of manoto Roku app I created to allow playing the live feed of manoto1 TV on my Roku box.
7ca45a7a-fe73-439c-b0fe-5340418e912e|3|5.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
Tags :