Querying Last.Fm web services

Download and get help for different MediaMonkey Addons.

Moderators: Peke, Gurus

trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Querying Last.Fm web services

Post by trixmoto » Tue May 01, 2007 4:03 am

Up until now I have be using the "fixurl" function written by psyxonova to encode URL strings before querying Last.Fm. However, I've discovered that any characters not in the basic character set (decimal code larger than 127) need to be UTF-8 encoded into two hex codes. Here is the function I am now using...
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.

trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Post by trixmoto » Tue May 01, 2007 4:04 am

Code: Select all

Function fixurl(sRawURL)
  Const sValidChars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\/&:"
  sRawURL = Replace(sRawURL,"+","%2B")
  
  If Len(sRawURL) > 0 Then
    Dim i : i = 1
    Do While i < Len(sRawURL)+1
      Dim s : s = Mid(sRawURL,i,1)
      If InStr(1,sValidChars,s,0) = 0 Then
        Dim d : d = Asc(s)
        If d = 32 Or d > 2047 Then
          s = "+"
        Else
          If d < 128 Then
            s = DecToHex(d)
          Else
            s = DecToUtf(d)
          End If
        End If
      Else
        Select Case s
          Case "&"
            s = "%2526"
          Case "/"
            s = "%252F"
          Case "\"
            s = "%5C"
          Case ":"
            s = "%3A"
        End Select
      End If
      fixurl = fixurl&s
      i = i + 1
    Loop
  End If
End Function

Function HexToDec(h)
  HexToDec = 0
  Dim i : i = 0
  For i = Len(h) To 1 Step -1
    Dim d : d = Mid(h,i,1)
    d = Instr("0123456789ABCDEF",UCase(d))-1
    If d >= 0 Then
      HexToDec = HexToDec+(d*(16^(Len(h)-i)))
    Else
      HexToDec = 0
      Exit For
    End If
  Next
End Function

Function DecToBin(intDec)
  DecToBin = ""
  Dim d : d = intDec
  Dim e : e = 1024
  While e >= 1
    If d >= e Then
      d = d - e
      DecToBin = DecToBin&"1"
    Else
      DecToBin = DecToBin&"0"
    End If
    e = e / 2
  Wend
End Function

Function BinToHex(strBin)
  Dim d : d = 0
  Dim i : i = 0
  For i = Len(strBin) To 1 Step -1
    Select Case Mid(strBin,i,1)
      Case "0"
        'do nothing
      Case "1"
        d = d + (2^(Len(strBin)-i))
      Case Else
        BinToHex = "00"
        Exit Function
    End Select
  Next
  BinToHex = DecToHex(d)
End Function

Function DecToHex(d)
  If d < 16 Then
    DecToHex = "0"&CStr(Hex(d))
  Else
    DecToHex = CStr(Hex(d))
  End If
End Function

Function DecToUtf(d)
  Dim b : b = DecToBin(d)
  Dim a : a = "110"&Left(b,5)
  b = "10"&Mid(b,6)
  DecToUtf = "%"&BinToHex(a)&"%"&BinToHex(b)
End Function
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.

RedX
Posts: 366
Joined: Wed Dec 27, 2006 10:32 am
Location: Germany

Post by RedX » Tue May 01, 2007 7:13 am

Very nice work!

Thank you very much this makes the life of people that use it much easier :)

Post Reply