VERSION 1.0 CLASS BEGIN MultiUse = -1 'True Persistable = 0 'NotPersistable DataBindingBehavior = 0 'vbNone DataSourceBehavior = 0 'vbNone MTSTransactionMode = 0 'NotAnMTSObject END Attribute VB_Name = "cLyrikSite" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = True Attribute VB_PredeclaredId = False Attribute VB_Exposed = False Option Explicit ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Objeto responsável por pegar letras do site oficial ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Guarda o nome do objeto Private Const ST_MY_NAME As String = "cLyrikSite" ' Endereços genéricos Private Const ST_ADDRESS_GET As String = "http://$S/website/lrk_get_url.php" Private Const ST_ADDRESS_INSERT As String = "http://$S/website/lrk_insert.php" ' Cookie genérico Private Const ST_COOKIE As String = ST_INVALID_VALUE ' Strings de POST Private Const ST_POST_ARTIST As String = "art=" Private Const ST_POST_ALBUM As String = "alb=" Private Const ST_POST_TITLE As String = "tit=" Private Const ST_POST_DURATION As String = "dur=" Private Const ST_POST_SIZE As String = "siz=" Private Const ST_POST_VERSION As String = "vrs=" Private Const ST_POST_MUTATION As String = "mut=" Private Const ST_POST_LYRIK As String = "lrk=" Private Const ST_POST_LYRIC As String = "lrc=" ' Início de GET Private Const ST_GET_INIT As String = "?" ' Separador de strings de POST Private Const ST_POST_SEP As String = "&" ' Final do post (enviar) Private Const ST_POST_GET_END As String = "get=Get" Private Const ST_POST_INSERT_END As String = "add=Add" ' Indicador de informação Private Const BT_RESULT_INFO As Byte = 58 ' Tipos de informação Private Const ST_RESULT_NOTFOUND As String = ":NOTFOUND" Private Const ST_RESULT_NOUPDATES As String = ":NOUPDATES" Private Const ST_RESULT_SUCCESS As String = ":SUCCESS" Private Const ST_RESULT_SQLCONNERROR As String = ":SQL_CONNECTION_ERROR" Private Const ST_RESULT_SQLERROR As String = ":SQL_ERROR" Private Const ST_RESULT_UPDATESERVER As String = ":UPDATESERVER:" ' Tamanho máximo dos campos Private Const LN_POST_ARTIST As Long = 60 Private Const LN_POST_ALBUM As Long = 60 Private Const LN_POST_TITLE As Long = 160 ' Objeto de acesso à internet Private WithEvents iNet As cInternet Attribute iNet.VB_VarHelpID = -1 ' Armazena a letra Private st_Lyrik As String ' Armazena se é somente update Private lu_Update As LYRIK_UPDATES ' Evento de resposta Public Event Loaded(Status As INTERNET_LYRIK, ID As Long, Update As LYRIK_UPDATES) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Ao iniciar/terminar classe ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Class_Initialize() On Error Resume Next ' Limpa memória st_Lyrik = ST_INVALID_VALUE ' Cria novo acesso à internet Set iNet = New cInternet End Sub Private Sub Class_Terminate() On Error Resume Next ' Libera acesso à internet Set iNet = Nothing ' Limpa memória st_Lyrik = ST_INVALID_VALUE End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Propriedade do socket ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Property Let Socket(wInput As Winsock) ' Guarda iNet.Socket = wInput End Property Public Property Get Socket() As Winsock ' Retorna Socket = iNet.Socket End Property ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Propriedade do timer ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Property Let Time(tInput As Timer) ' Seta iNet.Time = tInput End Property Public Property Get Time() As Timer ' Retorna Time = iNet.Time End Property ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Tenta encontrar uma letra ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Sub Load(cmMusic As cMusic, ID As Long, Optional lnMutation As Long = LN_NO_ERROR) Dim stAux As String Dim stAddress As String Dim stPost As String On Error GoTo ErrTreat: ' Inicia variável stPost = ST_GET_INIT ' Concatena artista stPost = stPost + ST_POST_ARTIST + wstringTOutf8uhtml(cmMusic.Artist, LN_POST_ARTIST) + ST_POST_SEP ' Concatena album stPost = stPost + ST_POST_ALBUM + wstringTOutf8uhtml(cmMusic.Album, LN_POST_ALBUM) + ST_POST_SEP ' Concatena faixa stPost = stPost + ST_POST_TITLE + wstringTOutf8uhtml(cmMusic.Title, LN_POST_TITLE) + ST_POST_SEP ' Concatena duração stPost = stPost + ST_POST_DURATION + CStr(cmMusic.Duration) + ST_POST_SEP ' Concatena tamamnho do arquivo stPost = stPost + ST_POST_SIZE + CStr(cmMusic.FileSize) + ST_POST_SEP ' Concatena mutação stPost = stPost + ST_POST_MUTATION + CStr(lnMutation) + ST_POST_SEP ' Concatena versão do lyrik stPost = stPost + ST_POST_VERSION + CStr(LN_LYRIK_VERSION) + ST_POST_SEP + ST_POST_GET_END ' Monta endereço stAddress = Replace(ST_ADDRESS_GET, ST_SERVER_ADDRESS, st_LyrikServer) + stPost ' Envia requisição para internet iNet.getPage stAddress, ID, ST_COOKIE, True ' Verifica a mutação Select Case lnMutation ' Se não houver Case LN_NO_ERROR ' Não é update lu_Update = lu_NoUpdate ' Se for inválido Case LN_INVALID_VALUE ' É update de Lyric lu_Update = lu_FromLyric ' Outros Case Else ' É update de Lyrik lu_Update = lu_FromLyrik End Select Exit Sub ErrTreat: LogErrMessage Err.Description, ST_MY_NAME + ".Load" End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Envia uma letra para internet ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Sub Save(cmMusic As cMusic, clLyrik As cLyric, blLyrik As Boolean) Dim stAux As String Dim stPost As String Dim stAddress As String On Error GoTo ErrTreat: ' Inicia variável stPost = ST_INVALID_VALUE ' Concatena artista stPost = stPost + ST_POST_ARTIST + wstringTOutf8uhtml(cmMusic.Artist, LN_POST_ARTIST) + ST_POST_SEP ' Concatena album stPost = stPost + ST_POST_ALBUM + wstringTOutf8uhtml(cmMusic.Album, LN_POST_ALBUM) + ST_POST_SEP ' Concatena faixa stPost = stPost + ST_POST_TITLE + wstringTOutf8uhtml(cmMusic.Title, LN_POST_TITLE) + ST_POST_SEP ' Concatena duração stPost = stPost + ST_POST_DURATION + CStr(cmMusic.Duration) + ST_POST_SEP ' Concatena tamanho do arquivo stPost = stPost + ST_POST_SIZE + CStr(cmMusic.FileSize) + ST_POST_SEP ' Concatena mutação stPost = stPost + ST_POST_MUTATION + CStr(clLyrik.Mutation) + ST_POST_SEP ' Concatena versão do lyrik stPost = stPost + ST_POST_VERSION + CStr(LN_LYRIK_VERSION) + ST_POST_SEP ' Se letra for um Lyrik If blLyrik Then ' Concatena arquivo com letra & tempos stPost = stPost + ST_POST_LYRIK + wstringTOutf8html(clLyrik.Properties + clLyrik.Load) + ST_POST_SEP + ST_POST_INSERT_END Else ' Concatena arquivo com letra pura stPost = stPost + ST_POST_LYRIC + wstringTOutf8html(clLyrik.Properties + clLyrik.TextRebuild) + ST_POST_SEP + ST_POST_INSERT_END End If ' Monta endereço stAddress = Replace(ST_ADDRESS_INSERT, ST_SERVER_ADDRESS, st_LyrikServer) ' Envia requisição para internet iNet.postData stAddress, LN_INVALID_VALUE, stPost, ST_COOKIE Exit Sub ErrTreat: LogErrMessage Err.Description, ST_MY_NAME + ".Save" End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Ao chegar evento da internet ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub iNet_ieEvent(ID As INTERNET_EVENT) Dim stAux As String On Error GoTo ErrTreat: ' Se ocorreu com sucesso If (ID = ieDataArrival) Then ' Recebe e processa dados stAux = GetLyrik(iNet.Data) ' Se houve retorno If (Len(stAux) > 0) Then ' Se o retorno for do tipo informação If (Asc(Mid(stAux, 1, 1)) = BT_RESULT_INFO) Then ' Verifica qual é a informação Select Case stAux ' Não encontrado Case ST_RESULT_NOTFOUND RaiseEvent Loaded(ilNotFound, iNet.ID, lu_Update) ' Não houve atualizações Case ST_RESULT_NOUPDATES RaiseEvent Loaded(ilNoUpdate, iNet.ID, lu_Update) ' Enviou com sucesso Case ST_RESULT_SUCCESS RaiseEvent Loaded(ilSent, iNet.ID, lu_Update) ' Erro com o SQL/Conexão SQL Case ST_RESULT_SQLCONNERROR, ST_RESULT_SQLERROR RaiseEvent Loaded(ilSqlError, iNet.ID, lu_Update) ' Erro não padronizado Case Else ' Se for info de novo servidor If updateServer(stAux) Then ' Letra não encontrada nesse servidor RaiseEvent Loaded(ilNotFound, iNet.ID, lu_Update) Else ' Erro desconhecido RaiseEvent Loaded(ilConnError, iNet.ID, lu_Update) End If End Select ' Termina procedimento Exit Sub Else ' Armazena a lyrik no buff st_Lyrik = utf8TOwstring(stAux) ' Informa que conseguiu carregar a letra RaiseEvent Loaded(ilFound, iNet.ID, lu_Update) ' Termina procedimento Exit Sub End If End If End If ' Esvazia buff st_Lyrik = ST_INVALID_VALUE ' Informa que não conseguiu carregar a letra RaiseEvent Loaded(ilConnError, iNet.ID, lu_Update) Exit Sub ErrTreat: LogErrMessage Err.Description, ST_MY_NAME + ".iNet_ieEvent" End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Atualiza servidor (se for info de atualização) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Function updateServer(stInput As String) As Boolean ' Inicia retorno updateServer = False ' Verifica se a entrada é válida If (Len(stInput) > Len(ST_RESULT_UPDATESERVER)) Then ' Se a entrada for realmente de atualização If (Mid(stInput, 1, Len(ST_RESULT_UPDATESERVER)) = ST_RESULT_UPDATESERVER) Then ' Seta novo servidor st_LyrikServer = Mid(stInput, Len(ST_RESULT_UPDATESERVER) + 1) ' Salva configurações cc_Configuration.Save False ' Seta que config era de info de novo servidor updateServer = True End If End If End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Retira a Lyrik do corpo da HTML ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Function GetLyrik(stHTML As String) As String Dim stAux As String Dim lnAuxStart As Long Dim lnAuxEnd As Long On Error GoTo ErrTreat: ' Inicia retorno GetLyrik = ST_INVALID_VALUE ' Encontra início da lyrik lnAuxStart = InStr(1, stHTML, ST_LYRIK_RESULT_SEP_BEGIN) ' Verifica se encontrou If (lnAuxStart > 0) Then ' Seta inicio real lnAuxStart = lnAuxStart + Len(ST_LYRIK_RESULT_SEP_BEGIN) ' Encontra o fim da lyrik lnAuxEnd = InStr(lnAuxStart, stHTML, ST_LYRIK_RESULT_SEP_END) ' Verifica se encontrou If (lnAuxEnd > 0) Then ' Seta lyrik GetLyrik = Mid(stHTML, lnAuxStart, lnAuxEnd - lnAuxStart) Else ' Loga erro #If httperr Then LogErrMessage "CORRUPT LYRIK: No end tag", ST_MY_NAME + ".GetLyrik" #End If End If Else ' Loga erro #If httperr Then LogErrMessage "NO LYRIK IN RESPONSE: " + CRLFtoCR(stHTML), ST_MY_NAME + ".GetLyrik" #End If End If ' Encontra início de versão lnAuxStart = InStr(1, stHTML, ST_LYRIK_VERSION_RESULT_SEP_BEGIN) ' Se encontrou If (lnAuxStart > 0) Then ' Seta inicio real lnAuxStart = lnAuxStart + Len(ST_LYRIK_VERSION_RESULT_SEP_BEGIN) ' Encontra o fim da versão lnAuxEnd = InStr(lnAuxStart, stHTML, ST_LYRIK_VERSION_RESULT_SEP_END) ' Se encontrou If (lnAuxEnd > 0) Then ' Recupera texto de versão stAux = Mid(stHTML, lnAuxStart, lnAuxEnd - lnAuxStart) ' Se for um número válido If IsNumeric(stAux) Then ' Guarda número ln_LastVersion = CLng(stAux) End If End If End If Exit Function ErrTreat: LogErrMessage Err.Description, ST_MY_NAME + ".GetLyrik" End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Retorna a Lyrik armazenada e limpa memória ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Property Get Lyrik() As String ' Retorna Lyrik = st_Lyrik ' Limpa local st_Lyrik = ST_INVALID_VALUE End Property