본문 바로가기

자료

[VB.NET/C#] 아프리카 TV 라이브 방송링크 받아오기

728x90

 

위의 사진처럼 아프리카 TV 라이브 방송의 주소를 알 수 있습니다.

공개방송 , 숨겨진방송 , 잠긴방송의 링크를 얻어올 수 있습니다.

 

19세 이상의 링크의 경우 로그인이 필요합니다.

로그인이 필요하신 경우 전역변수 선언과 로그인 코드를 넣으시면 됩니다.

 

전역변수

Dim logincookie As CookieContainer = Nothing

BJ 정보받기 및 라이브링크 받아오기

Public Async Sub ParseBJInfo(BJID As String)
        Dim postdata As String = "bj=" & BJID & "&station_no=1915896"
        Dim bytes As Byte() = New UTF8Encoding().GetBytes(postdata)

        Dim request As HttpWebRequest = WebRequest.Create("http://api.m.afreecatv.com/station/main/a/getmaindata")
        With request
            .Method = "POST"
            .Host = "api.m.afreecatv.com"
            .KeepAlive = False
            .ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
            .Referer = "http://m.afreecatv.com/" & BJID
            .UserAgent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16"
            .ContentLength = bytes.Length
            Await .GetRequestStream().WriteAsync(bytes, 0, bytes.Length)
        End With

        Dim response As HttpWebResponse = Nothing
        response = DirectCast(request.GetResponse(), HttpWebResponse)

        Dim html As String = Await New StreamReader(response.GetResponseStream()).ReadToEndAsync

        html = Regex.Unescape(html)

        Dim r_Bno As New Regex("broad_no\x22:(.*),\x22is_secret", RegexOptions.IgnoreCase)
        Dim r_LiveTitle As New Regex("broad_title\x22:\x22(.*)\x22,\x22broad_memo", RegexOptions.IgnoreCase)
        Dim r_BJName As New Regex("user_nick\x22:\x22(.*)\x22,\x22view_cnt", RegexOptions.IgnoreCase)

        If r_Bno.IsMatch(html) Then
            Dim r_BnoMatch As MatchCollection = r_Bno.Matches(html)
            Dim r_BJNameMatch As MatchCollection = r_BJName.Matches(html)
            Dim r_LiveTitleMatch As MatchCollection = r_LiveTitle.Matches(html)

            Console.WriteLine("========================================================================")
            Console.WriteLine("BJ 아이디 : " & BJID)
            Console.WriteLine("BJ 닉네임 : " & r_BJNameMatch(0).Groups(1).Value)
            Console.WriteLine("방송 제목 : " & r_LiveTitleMatch(0).Groups(1).Value)
            Console.WriteLine("")

            ParseTKLink(r_BnoMatch(0).Groups(1).Value)
        End If
    End Sub

    Public Async Sub ParseTKLink(bno As String)
        Dim postdata As String = "bno=" & bno
        Dim bytes As Byte() = New UTF8Encoding().GetBytes(postdata)

        Dim request As HttpWebRequest = WebRequest.Create("http://live.afreecatv.com:8057/afreeca/player_live_ticket_api.php")
        With request
            .Method = "POST"
            .Host = "live.afreecatv.com:8057"
            .KeepAlive = False
            .Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
            .ContentType = "application/x-www-form-urlencoded"
            .Referer = "http"
            .UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0"
            .CookieContainer = logincookie
            .ContentLength = bytes.Length
            Await .GetRequestStream().WriteAsync(bytes, 0, bytes.Length) '오류뜨는 부분
        End With

        Dim response As HttpWebResponse = Nothing
        response = DirectCast(request.GetResponse(), HttpWebResponse)

        Dim html As String = Await New StreamReader(response.GetResponseStream()).ReadToEndAsync

        html = Regex.Unescape(html)

        Dim r_TK As New Regex("TK\x22:\x22(.*)\x22}}", RegexOptions.IgnoreCase)

        If r_TK.IsMatch(html) Then
            Dim r_TKMatch As MatchCollection = r_TK.Matches(html)

            txtLiveLink.Text = "http://chromecast.afreeca.gscdn.com/livestream-14/auth_playlist.m3u8?aid=" & r_TKMatch(0).Groups(1).Value

            Console.WriteLine("라이브 링크 : http://chromecast.afreeca.gscdn.com/livestream-14/auth_playlist.m3u8?aid=" & r_TKMatch(0).Groups(1).Value)
            Console.WriteLine("========================================================================")
        End If
    End Sub

로그인

  Public Async Sub Login(ID As String, PW As String)
        Dim tempcookie As New CookieContainer

        Dim request As HttpWebRequest = WebRequest.Create("https://member.afreeca.com:8111/login/LoginAction.php?szWork=login&szUid=" + ID + "&szPassword=" + PW)
        With request
            .Method = "GET"
            .Host = "member.afreecatv.com:8111"
            .KeepAlive = False
            .Referer = "http://login.afreecatv.com:8100/afreeca/login.php?szFrom=full&request_uri=http%3A%2F%2Fafreecatv.com%2F"
            .UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0"
            .CookieContainer = tempcookie
        End With

        Dim response As HttpWebResponse = Nothing
        response = DirectCast(request.GetResponse(), HttpWebResponse)

        tempcookie.Add(response.Cookies)
        logincookie = tempcookie

        Dim html As String = Await New StreamReader(response.GetResponseStream()).ReadToEndAsync

        html = Regex.Unescape(html)

        If html.Contains("""RESULT"":1") Then
            MsgBox("로그인 성공!")
        Else
            MsgBox("로그인 실패")
        End If
    End Sub
728x90