알송의 가사 가져오는 코드

Programming/Visual Basic 2017. 5. 13. 20:31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
Imports System.Text
Imports System.Net
Imports System.IO
Imports System.Xml
Imports System.Security
 
Module ParseAlSong
    Private Const URL As String = "http://lyrics.alsong.co.kr/alsongwebservice/service1.asmx"
 
    Private Const SendXML As String =
        "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbCrLf &
        "<SOAP-ENV:Envelope " &
            "xmlns:SOAP-ENV=""http://www.w3.org/2003/05/soap-envelope"" " &
            "xmlns:SOAP-ENC=""http://www.w3.org/2003/05/soap-encoding"" " &
            "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " &
            "xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" " &
            "xmlns:ns2=""ALSongWebServer/Service1Soap"" " &
            "xmlns:ns1=""ALSongWebServer"" " &
            "xmlns:ns3=""ALSongWebServer/Service1Soap12"">" & vbCrLf &
        "  <SOAP-ENV:Body>" & vbCrLf &
        "    <ns1:GetLyric5><ns1:stQuery>" & vbCrLf &
        "    <ns1:strChecksum>{0}</ns1:strChecksum>" & vbCrLf &
        "    <ns1:strVersion></ns1:strVersion>" & vbCrLf &
        "    <ns1:strMACAddress></ns1:strMACAddress>" & vbCrLf &
        "    <ns1:strIPAddress></ns1:strIPAddress>" & vbCrLf &
        "    </ns1:stQuery></ns1:GetLyric5>" & vbCrLf &
        "  </SOAP-ENV:Body>" & vbCrLf &
        "</SOAP-ENV:Envelope>"
 
    Public Structure AlSongInfo
        Dim ID As Integer
        Dim RegistDate As Date
        Dim Title As String
        Dim Artist As String
        Dim Album As String
        Dim CGood As Integer
        Dim CBad As Integer
        Dim Lyric As String
        Dim FirstRegister As AlSongRegister
        Dim CurrentRegister As AlSongRegister
    End Structure
 
    Public Structure AlSongRegister
        Dim Name As String
        Dim eMail As String
        Dim URL As String
        Dim Phone As String
        Dim Comment As String
    End Structure
 
    Public Function GetMP3Info(FileName As StringAs AlSongInfo
        Using FS = New FileStream(FileName, FileMode.Open, FileAccess.Read)
            Return GetMP3Info(FS)
        End Using
    End Function
 
    Public Function GetMP3Info(Stream As Stream) As AlSongInfo
        '오디오 프레임에서 163840 바이트를 읽어 해시 값을 구함
        '해당 코드에서는 NuGet의 ID3를 사용함
        Const SampleSize As Integer = 163840
        Dim MD5 = Cryptography.MD5.Create()
        Dim Hash() As Byte
        Using MP3 As New Id3.Mp3Stream(Stream)
            Hash = MD5.ComputeHash(MP3.GetAudioStream(), 0, SampleSize)
        End Using
        Return GetInfo(BitConverter.ToString(Hash).Replace("-""").ToLower())
    End Function
 
    Public Function GetInfo(Checksum As StringAs AlSongInfo
        Dim Request = HttpWebRequest.CreateHttp(URL)
        Request.KeepAlive = False
        Request.Method = "POST"
        Request.UserAgent = "gSOAP/2.7"
        Request.ContentType = "application/soap+xml; charset=utf-8"
        Request.Headers.Add("SOAPAction""""ALSongWebServer/GetLyric5""")
        Using Stream = Request.GetRequestStream()
            WriteStream(Stream, Checksum)
        End Using
        Using Response = Request.GetResponse()
            Using Stream = Response.GetResponseStream()
                Return ParseStream(Stream)
            End Using
        End Using
    End Function
 
 
    Private Sub WriteStream(Stream As Stream, Checksum As String)
        Dim XML = Encoding.UTF8.GetBytes(String.Format(SendXML, Checksum))
        Stream.Write(XML, 0, XML.Length)
    End Sub
 
    Private Function ParseStream(Stream As Stream) As AlSongInfo
        Dim XML As New XmlDocument()
        XML.Load(Stream)
        Dim Node = XML.SelectSingleNode("*/*/*/*")
        If Node Is Nothing Then Return Nothing
        Dim Info As AlSongInfo
        With Info
            .ID = GetNodeValue(Node, "strInfoID")
            .RegistDate = GetNodeValue(Node, "strRegistDate")
            .Title = GetNodeValue(Node, "strTitle")
            .Artist = GetNodeValue(Node, "strArtist")
            .Album = GetNodeValue(Node, "strAlbum")
            .CGood = GetNodeValue(Node, "strCountGood")
            .CBad = GetNodeValue(Node, "strCountBad")
            .Lyric = GetNodeValue(Node, "strLyric").Replace("<br>", vbCrLf)
 
            .FirstRegister.Name = GetNodeValue(Node, "strRegisterFirstName")
            .FirstRegister.eMail = GetNodeValue(Node, "strRegisterFirstEMail")
            .FirstRegister.URL = GetNodeValue(Node, "strRegisterFirstURL")
            .FirstRegister.Phone = GetNodeValue(Node, "strRegisterFirstPhone")
            .FirstRegister.Comment = GetNodeValue(Node, "strRegisterFirstComment")
 
            .CurrentRegister.Name = GetNodeValue(Node, "strRegisterName")
            .CurrentRegister.eMail = GetNodeValue(Node, "strRegisterEMail")
            .CurrentRegister.URL = GetNodeValue(Node, "strRegisterURL")
            .CurrentRegister.Phone = GetNodeValue(Node, "strRegisterPhone")
            .CurrentRegister.Comment = GetNodeValue(Node, "strRegisterComment")
        End With
        Return Info
    End Function
 
    Private Function GetNodeValue(Node As XmlElement, Name As StringAs String
        Dim SubNode = Node.GetElementsByTagName(Name)
        If SubNode.Count = 0 Then Return ""
        Return SubNode(0).InnerText
    End Function
End Module
 
cs


Lyric이 가사임

posted by 자칭 프로그래머ㅡ ModMapper