-
Notifications
You must be signed in to change notification settings - Fork 3
/
BiliPlaylist.rb
197 lines (141 loc) · 3.61 KB
/
BiliPlaylist.rb
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
194
195
196
197
module BiliPlaylist
require 'fileutils'
require_relative 'BiliConfig'
class BiliPlaylist
def initialize(videos="")
@videos = videos
@hash = {}
@historyfile = File.join($configPath,"biligui.history")
@lastfile = File.join($configPath,"biligui.last")
split
end
def split
unless @videos.empty? then
@videos.split(/\n/).each do |video|
if video.index(",") || video.index(";") then
if video.index(",") then
separator = ","
else
separator = ";"
end
video.split(separator).each do |nest|
if nest.index("http://") then
key = "av" + nest.gsub(/^.*\/av/,'').gsub(/\//,'')
@hash[key] = nest
elsif nest.index("av") then
value = "http://www.bilibili.com/video/" + nest + "/"
@hash[nest] = value
else
next
end
end
else
if video.index("http://") then
key = "av" + video.gsub(/^.*\/av/,'').gsub(/\//,'')
@hash[key] = video
elsif video.index("av") then
value = "http://www.bilibili.com/video/" + video + "/"
@hash[video] = value
else
next
end
end
end
end
end
def hash
return @hash
end
def save(filename="")
playlist = filename
default_playlist = "#{$configPath}/playlist/biliplaylist.m3u8"
if playlist.empty? then
playlist = default_playlist
end
if File.exist?(playlist) then
old_playlist = playlist + ".old"
FileUtils.mv playlist, old_playlist
end
io = open(playlist,"w")
io.puts "#EXTM3U"
@hash.to_a.each do |video|
io.puts "#EXTINF:#{video[0]}"
io.puts video[1]
end
io.close
end
def load(playlist="")
hash = {}
default_playlist = "#{$configPath}/playlist/biliplaylist.m3u8"
if playlist.empty? && File.exist?(default_playlist) then
playlist = default_playlist
end
if playlist.empty? then
p "[ERR] No playlist available!"
else
io = open(playlist, 'r')
array = []
i = 0
io.each_line do |line|
line.chomp!
i += 1
unless line.index("http://") then
next
end
unless line.index("bilibili") then
p "#{line} doesn't seem to be a Bilibili URL!"
else
value = line
key = "av" + value.gsub(/^.*\/av/,'').gsub(/\//,'')
hash[key] = value
array[i] = line
end
end
if array.empty? then
p "This playlist has no URL we support!"
end
io.close
end
return hash
end
def history
io = open(@lastfile, 'w')
@hash.each_value { |item| io.puts item }
io.close
open(@lastfile,'r') do |f|
open(@historyfile, 'a') do |f1|
f.each_line do |line|
f1.puts line
end
end
end
duplicate([@lastfile,@historyfile])
end
def duplicate(files)
files.each do |file|
array = []
i = 0
open(file,'r') do |f|
f.each_line do |line|
line.chomp!
array[i] = line
i+=1
end
end
new = array.uniq
io = open(file, 'w')
new.each {|value| io.puts value}
io.close
end
end
def last
str = ""
open(@lastfile,'r') do |f|
f.each_line do |line|
str += line
end
end
return str
end
end
end