bilibili_sunday 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/bsserver +2 -0
- data/lib/bilibili_sunday/client.rb +4 -0
- data/lib/bilibili_sunday/downloader.rb +60 -20
- data/lib/bilibili_sunday/server.rb +6 -0
- data/lib/bilibili_sunday/version.rb +1 -1
- metadata +1 -1
data/bin/bsserver
CHANGED
@@ -13,48 +13,64 @@ module BilibiliSunday
|
|
13
13
|
require 'uri'
|
14
14
|
require 'open-uri'
|
15
15
|
require 'logger'
|
16
|
+
require 'thread'
|
16
17
|
|
17
18
|
def initialize(work_path = nil, downloader = nil, logger = nil)
|
18
19
|
@work_path = File.expand_path(work_path || '~/.bilibili_sunday')
|
19
20
|
@downloader = downloader || Aria2::Downloader.new
|
20
21
|
@logger = logger || Logger.new($stdout)
|
21
22
|
@cacher = BilibiliSunday::Cacher.new(cacher_store_path)
|
23
|
+
@mutex = Mutex.new
|
22
24
|
|
23
25
|
FileUtils.mkdir_p(@work_path)
|
24
26
|
end
|
25
27
|
|
26
28
|
def routine_work
|
27
|
-
@
|
29
|
+
@mutex.synchronize do
|
30
|
+
@logger.info 'Carrying out routine work. '
|
28
31
|
|
29
|
-
|
32
|
+
videos = get_all_videos
|
30
33
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
+
videos.each do |cid|
|
35
|
+
update_status(cid)
|
36
|
+
concat(cid) if (cache_completed?(cid) && (!concat_started?(cid)))
|
37
|
+
end
|
34
38
|
end
|
35
39
|
end
|
36
40
|
|
37
41
|
def query_status(cid)
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
42
|
+
@mutex.synchronize do
|
43
|
+
status = :unknown
|
44
|
+
status = :caching if cache_in_progress?(cid)
|
45
|
+
status = :concatenating if concat_in_progress?(cid)
|
46
|
+
status = :complete if concat_completed?(cid)
|
47
|
+
|
48
|
+
{
|
49
|
+
cid: cid,
|
50
|
+
status: status,
|
51
|
+
downloads: load_yaml(status_yaml_path(cid)) || [],
|
52
|
+
path: concat_completed?(cid) ? concat_output_file_path(cid) : nil,
|
53
|
+
comments_path: comments_path(cid)
|
54
|
+
}
|
55
|
+
end
|
50
56
|
end
|
51
57
|
|
52
58
|
def request_cache(cid)
|
53
|
-
|
59
|
+
@mutex.synchronize do
|
60
|
+
cache(cid)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def remove_cache(cid)
|
65
|
+
@mutex.synchronize do
|
66
|
+
remove(cid)
|
67
|
+
end
|
54
68
|
end
|
55
69
|
|
56
70
|
def all_videos
|
57
|
-
|
71
|
+
@mutex.synchronize do
|
72
|
+
get_all_videos
|
73
|
+
end
|
58
74
|
end
|
59
75
|
|
60
76
|
def cid_for_video_url(url)
|
@@ -84,11 +100,17 @@ module BilibiliSunday
|
|
84
100
|
end
|
85
101
|
|
86
102
|
def active_videos
|
87
|
-
|
103
|
+
@mutex.synchronize do
|
104
|
+
all_videos.select { |i| !concat_completed?(i)}
|
105
|
+
end
|
88
106
|
end
|
89
107
|
|
90
108
|
private
|
91
109
|
|
110
|
+
def get_all_videos
|
111
|
+
Dir.glob(File.join(video_store_path, '*')).select {|f| File.directory? f}.map { |f| File.basename(f).to_i }
|
112
|
+
end
|
113
|
+
|
92
114
|
def gzip_inflate(string)
|
93
115
|
# TODO Ugly workaround...
|
94
116
|
begin
|
@@ -312,6 +334,24 @@ module BilibiliSunday
|
|
312
334
|
|
313
335
|
end
|
314
336
|
|
337
|
+
def remove(cid)
|
338
|
+
return true unless cache_started?(cid)
|
339
|
+
return false if concat_in_progress?(cid)
|
340
|
+
|
341
|
+
downloads = load_yaml(downloads_yaml_path(cid))
|
342
|
+
status = load_yaml(status_yaml_path(cid))
|
343
|
+
|
344
|
+
downloads.each_with_index do |download, order|
|
345
|
+
if !status || status[order][:status]['status'] == 'active'
|
346
|
+
@downloader.remove(download[:download_id])
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
FileUtils.rm_rf(video_path(cid))
|
351
|
+
|
352
|
+
true
|
353
|
+
end
|
354
|
+
|
315
355
|
end
|
316
356
|
|
317
357
|
end
|
@@ -32,6 +32,8 @@ module BilibiliSunday
|
|
32
32
|
handle_all_videos
|
33
33
|
elsif method == 'active_videos'
|
34
34
|
handle_active_videos
|
35
|
+
elsif method == 'remove_cache'
|
36
|
+
handle_remove_cache(params[0].to_i)
|
35
37
|
else
|
36
38
|
handle_error(1, 'No matching method. ')
|
37
39
|
end
|
@@ -64,6 +66,10 @@ module BilibiliSunday
|
|
64
66
|
return 200, {result: @downloader.active_videos}
|
65
67
|
end
|
66
68
|
|
69
|
+
def handle_remove_cache(cid)
|
70
|
+
return 200, {result: @downloader.remove_cache(cid)}
|
71
|
+
end
|
72
|
+
|
67
73
|
def handle_error(error_code, error_message)
|
68
74
|
return 500, {error: {code: error_code, message: error_message}}
|
69
75
|
end
|