Class: BilibiliSunday::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/bilibili_sunday/downloader.rb

Instance Method Summary collapse

Constructor Details

#initialize(work_path = nil, downloader = nil, logger = nil) ⇒ Downloader

Returns a new instance of Downloader.



18
19
20
21
22
23
24
25
26
# File 'lib/bilibili_sunday/downloader.rb', line 18

def initialize(work_path = nil, downloader = nil, logger = nil)
	@work_path = File.expand_path(work_path || '~/.bilibili_sunday')
	@downloader = downloader || Aria2::Downloader.new
	@logger = logger || Logger.new($stdout)
	@cacher = BilibiliSunday::Cacher.new(cacher_store_path)
	@mutex = Mutex.new

	FileUtils.mkdir_p(@work_path)
end

Instance Method Details

#active_videosObject



99
100
101
102
103
# File 'lib/bilibili_sunday/downloader.rb', line 99

def active_videos
	@mutex.synchronize do
		all_videos.select { |i| !concat_completed?(i)}
	end
end

#all_videosObject



71
72
73
74
75
# File 'lib/bilibili_sunday/downloader.rb', line 71

def all_videos
	@mutex.synchronize do
		get_all_videos
	end
end

#cid_for_video_url(url) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bilibili_sunday/downloader.rb', line 77

def cid_for_video_url(url)
	doc = Nokogiri::HTML(gzip_inflate(@cacher.read_url(url)))

	res = /secure,cid=([0-9]*)/.match(doc.to_s)

	if res && res[1]
		return res[1].to_i
	else
		raise "Not a valid Bilibili video page URL. "
	end
end

#query_status(cid) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bilibili_sunday/downloader.rb', line 42

def query_status(cid)
	@mutex.synchronize do
		status = :unknown
		status = :caching if cache_in_progress?(cid)
		status = :concatenating if concat_in_progress?(cid)
		status = :complete if concat_completed?(cid)

		{
			cid: cid,
			status: status, 
			downloads: load_yaml(status_yaml_path(cid)) || [], 
			path: concat_completed?(cid) ? concat_output_file_path(cid) : nil,
			comments_path: comments_path(cid)
		}
	end
end

#remove_cache(cid) ⇒ Object



65
66
67
68
69
# File 'lib/bilibili_sunday/downloader.rb', line 65

def remove_cache(cid)
	@mutex.synchronize do
		remove(cid)
	end
end

#request_cache(cid) ⇒ Object



59
60
61
62
63
# File 'lib/bilibili_sunday/downloader.rb', line 59

def request_cache(cid)
	@mutex.synchronize do
		cache(cid)
	end
end

#routine_workObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bilibili_sunday/downloader.rb', line 28

def routine_work
	@mutex.synchronize do
		@logger.info 'Carrying out routine work. '

		videos = get_all_videos

		videos.each do |cid|
			update_status(cid)
			cleanup(cid)
			concat(cid) if (cache_completed?(cid) && (!concat_started?(cid)))
		end
	end
end

#title_for_video_url(url) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/bilibili_sunday/downloader.rb', line 89

def title_for_video_url(url)
	doc = Nokogiri::HTML(gzip_inflate(@cacher.read_url(url)))

	doc.css('meta').each do |i|
		return i['content'] if i['name'] == 'title'
	end

	raise "Not a valid Bilibili video page URL. "
end