-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlyrics.lua
executable file
·193 lines (169 loc) · 5.46 KB
/
lyrics.lua
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
--[[
Get lyrics about a song from chartlyrics.com
Copyright © 2011 Scott Moak ([email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
--]]
-- Lua modules
require "simplexml"
-- Some global variables: widgets
dlg = nil -- dialog
okay = nil
lyrics_txtbox = nil
artist_label = nil
song_title_label = nil
lyrics_url = "http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist="
-- Script descriptor, called when the extensions are scanned
function descriptor()
return { title = "Lyrics" ;
version = "0.1" ;
author = "Scott Moak" ;
url = '';
shortdesc = "Fetches Song Lyrics";
description = "<center><b>Fetch Song Lyrics</b></center><br />"
.. "Get lyrics about songs from "
.. "chartlyrics.<br />This Extension will show "
.. "you the lyrics of the current song";
capabilities = { "input-listener" } }
end
function reset_global_variables()
okay = nil
lyrics_txtbox = nil
artist_label = nil
song_title_label = nil
end
-- Remove trailing & leading spaces
function trim(str)
if not str then return "" end
return string.gsub(str, "^%s*(.*)+%s$", "%1")
end
-- First function to be called when the extension is activated
function activate()
vlc.msg.dbg("Activating lyrics dialog...")
create_dialog()
end
-- This function is called when the extension is disabled
function deactivate()
vlc.msg.dbg("Deactivating lyrics dialog...")
end
-- This function is called when the dialog is closed
function close()
vlc.msg.dbg("Lyrics Dialog closed")
reset_global_variables()
if dlg ~= nil then dlg:delete() end
dlg = nil
vlc.deactivate()
end
function close_clicked()
close()
end
function input_changed()
local item = vlc.input.item()
if item == nil then return false end
local title = item:name() -- It return the internal title or the filename if the first is missing
local metas = item:metas()
local artist = ""
if metas and metas["artist"] then
artist = metas["artist"]
end
if title ~= nil then
title = string.gsub(title, "(.*)%.%w+$", "%1") -- Removes file extension
if title ~= "" then
song_title_label:set_text("<b>Title:</b> " .. title)
artist_label:set_text("<b>Artist:</b> " .. artist)
dlg:update()
end
end
local lyrics = get_lyrics()
if lyrics then
--lyrics_txtbox:set_text(lyrics)
end
return true
end
function sleep(sec)
local t = vlc.misc.mdate()
vlc.misc.mwait(t + sec*1000*1000)
end
-- Get clean title from filename
function get_title(str)
local item = vlc.item or vlc.input.item()
if not item then
return ""
end
local metas = item:metas()
if metas and metas["title"] then
return metas["title"]
else
local filename = string.gsub(item:name(), "^(.+)%.%w+$", "%1")
return trim(filename or item:name())
end
end
function get_lyrics()
local title = get_title()
local artist = get_artist()
if title == nil or title == "" or artist == nil or artist == "" then
return ""
end
vlc.msg.dbg("Artist: " .. artist)
vlc.msg.dbg("Title: " .. title)
lyrics_url = lyrics_url .. vlc.strings.encode_uri_component(artist) .. "&song=" .. vlc.strings.encode_uri_component(title)
vlc.msg.dbg("Fetching from " .. lyrics_url)
local s, msg = vlc.stream(lyrics_url)
if not s then
vlc.msg.warn("Weird: " .. msg)
return msg
end
local data = s:read( 65535 )
s = nil
if data then
vlc.msg.dbg(data)
local xml = simplexml.parse_string(data)
data = nil
if xml then
for _, v in ipairs(xml.children) do
if v.name == "Lyric" and type(v.children[1]) == "string" then
return htmlize(v.children[1])
end
end
end
end
return "Could not find lyrics"
end
-- Replaces newlines with <br /> so the text looks pretty :)
function htmlize(str)
return string.gsub(str, "\n", "<br />")
end
function get_artist(str)
local item = vlc.item or vlc.input.item()
if not item then
return ""
end
local metas = item:metas()
if metas and metas["artist"] then
return metas["artist"]
else
local filename = string.gsub(item:name(), "^(.+)%.%w+$", "%1")
return trim(filename or item:name())
end
end
-- Create the main dialog
function create_dialog()
vlc.msg.dbg("Creating lyrics dialog...")
dlg = vlc.dialog("Lyrics")
-- col, row, row_span, col_span
artist_label = dlg:add_label("<b>Artist:</b> " .. get_artist(), 1, 1, 1, 1)
song_title_label = dlg:add_label("<b>Title:</b> " .. get_title(), 1, 2, 1, 1)
lyrics_txtbox = dlg:add_html(get_lyrics(), 1, 3, 1, 1)
okay = dlg:add_button("Close", close_clicked, 1, 4, 1, 1)
-- Show, if not already visible
dlg:show()
end