-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylistGenerator.lua
135 lines (118 loc) · 3.54 KB
/
playlistGenerator.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
local lfs = require("lfs")
local function printHelp()
print("Generate a .cctpl playlist")
print("Usage: lua playlistGenerator.lua new_playlist.cctpl [options]")
print("Options:")
print(" -b, --base-url <url> Specify the base URL for the playlist.")
print(" -d, --directory <path> Specify the directory containing the dfpwm files.")
print(" -t, --type <type> Specify the type of the playlist (e.g., nextcloud).")
print(" -h, --help Show this help message.")
print("\nExample:")
print(
" lua playlistGenerator.lua -b \"https://nextcloud.example.com/s/MyMinecraftSongs\" -d \"/path/to/files\" -t \"nextcloud\"")
end
local function parseArgs()
local args = {} -- Table to hold parsed arguments
local i = 1 -- Index for the global arg table
while i <= #arg do
local key = arg[i]
if key == "-b" or key == "--base-url" then
i = i + 1
if arg[i] then
args.baseUrl = arg[i]
else
error("Error: Base URL not provided after " .. key)
end
elseif key == "-d" or key == "--directory" then
i = i + 1
if arg[i] then
args.directory = arg[i]
else
error("Error: Directory not provided after " .. key)
end
elseif key == "-t" or key == "--type" then
i = i + 1
if arg[i] then
args.type = arg[i]
else
error("Error: Type not provided after " .. key)
end
elseif key == "-h" or key == "--help" then
printHelp()
os.exit(0)
else
if not args.targetFile then
if arg[i]:match("^.+(%..+)$") ~= ".cctpl" then
print("Wrong playlist file extension: " .. arg[i]:match("^.+(%..+)$"))
print()
printHelp()
os.exit(1)
end
args.targetFile = arg[i]
else
print("Error: Unknown argument " .. key)
print()
printHelp()
os.exit(1)
end
end
i = i + 1 -- Move to the next argument
end
if not args.baseUrl then
print("BaseUrl wasn't specified!")
print()
printHelp()
os.exit(1)
elseif not args.directory then
print("No directory was specified!")
print()
printHelp()
os.exit(1)
elseif not args.type then
print("A playlist type wasn't specified!")
print()
printHelp()
os.exit(1)
elseif not args.targetFile then
print("A target playlist file wasn't specified!")
print()
printHelp()
os.exit(1)
end
return args
end
local function getFiles(directory)
local files = {}
-- Check if the directory exists
local attr = lfs.attributes(directory)
if not attr or attr.mode ~= "directory" then
return nil, "Directory does not exist"
end
-- Iterate through the directory
for file in lfs.dir(directory) do
if file:match("%.dfpwm$") then -- Check for .dfpwm extension
table.insert(files, file)
end
end
return files
end
local function writePlaylist(filename, type, baseUrl, files)
local file, err = io.open(filename, "w") -- Open file for writing
if not file then
return nil, "Error opening file: " .. err
end
-- Write the @type and @baseUrl tags
file:write("@type " .. type .. "\n")
file:write("@baseUrl " .. baseUrl .. "\n")
-- Write each file in the files table
for _, fileName in ipairs(files) do
file:write(fileName .. "\n")
end
file:close() -- Close the file
end
local function Main()
local parsedArgs = parseArgs()
local files = getFiles(parsedArgs.directory)
writePlaylist(parsedArgs.targetFile, parsedArgs.type, parsedArgs.baseUrl, files)
end
Main()