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.
@@ -3,5 +3,7 @@
3
3
 
4
4
  require 'bilibili_sunday/server'
5
5
 
6
+ puts 'BilibiliSunday Version v' + BilibiliSunday::VERSION
7
+
6
8
  server = BilibiliSunday::Server.new
7
9
  server.start
@@ -39,6 +39,10 @@ module BilibiliSunday
39
39
  rpc_call('query_status', [cid])
40
40
  end
41
41
 
42
+ def remove_cache(cid)
43
+ rpc_call('remove_cache', [cid])
44
+ end
45
+
42
46
  private
43
47
 
44
48
  def get(url, params = {})
@@ -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
- @logger.info 'Carrying out routine work. '
29
+ @mutex.synchronize do
30
+ @logger.info 'Carrying out routine work. '
28
31
 
29
- videos = all_videos
32
+ videos = get_all_videos
30
33
 
31
- videos.each do |cid|
32
- update_status(cid)
33
- concat(cid) if (cache_completed?(cid) && (!concat_started?(cid)))
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
- status = :unknown
39
- status = :caching if cache_in_progress?(cid)
40
- status = :concatenating if concat_in_progress?(cid)
41
- status = :complete if concat_completed?(cid)
42
-
43
- {
44
- cid: cid,
45
- status: status,
46
- downloads: load_yaml(status_yaml_path(cid)) || [],
47
- path: concat_completed?(cid) ? concat_output_file_path(cid) : nil,
48
- comments_path: comments_path(cid)
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
- cache(cid)
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
- Dir.glob(File.join(video_store_path, '*')).select {|f| File.directory? f}.map { |f| File.basename(f).to_i }
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
- all_videos.select { |i| !concat_completed?(i)}
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
@@ -1,3 +1,3 @@
1
1
  module BilibiliSunday
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bilibili_sunday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: