Advertisement
justync7

getLyrics

Oct 14th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.04 KB | None | 0 0
  1. function split(str, pat)
  2.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  3.    local fpat = "(.-)" .. pat
  4.    local last_end = 1
  5.    local s, e, cap = str:find(fpat, 1)
  6.    while s do
  7.       if s ~= 1 or cap ~= "" then
  8.      table.insert(t,cap)
  9.       end
  10.       last_end = e+1
  11.       s, e, cap = str:find(fpat, last_end)
  12.    end
  13.    if last_end <= #str then
  14.       cap = str:sub(last_end)
  15.       table.insert(t, cap)
  16.    end
  17.    return t
  18. end
  19.  
  20. api="http://lyrics.wikia.com/api.php?artist=ARTISTHERE&song=SONGHERE"
  21. tArgs={ ... }
  22. function printUsage()
  23. rp=shell.getRunningProgram()
  24. print("Usage: "..rp.." <artist> <song>")
  25. end
  26. if #tArgs < 2 then
  27. printUsage()
  28. error()
  29. end
  30. api="http://lyrics.wikia.com/api.php?artist="..tArgs[1]:gsub("_","+").."&song="..tArgs[2]:gsub("_","+")
  31. site=http.get(api)
  32. if site then
  33. src=site.readAll()
  34. site.close()
  35. split1=split(src,"<pre>")
  36. split2=split(split1[2],"</pre>")
  37. lyrics=split2[1]
  38. lyrics=lyrics:gsub("&#039;","'")
  39. term.clear()
  40. term.setCursorPos(1,1)
  41. print(tArgs[1].." - "..tArgs[2])
  42. textutils.slowPrint(lyrics)
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement