Class: DouyinDownload::Parser
- Inherits:
-
Object
- Object
- DouyinDownload::Parser
show all
- Defined in:
- lib/douyin_download/parser.rb
Defined Under Namespace
Classes: InvalidDouyinUrlError
Constant Summary
collapse
- MOBILE_USER_AGENT =
"Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"
Instance Method Summary
collapse
Constructor Details
#initialize(url) ⇒ Parser
Returns a new instance of Parser.
6
7
8
9
10
11
12
|
# File 'lib/douyin_download/parser.rb', line 6
def initialize(url)
@url = url
regexp = /v\.douyin\.com\/[\w\d]+/
unless @url =~ regexp
raise InvalidDouyinUrlError.new("Invalid douyin url: #{@url}")
end
end
|
Instance Method Details
#api_play_url ⇒ Object
36
37
38
|
# File 'lib/douyin_download/parser.rb', line 36
def api_play_url
info.dig('item_list',0,'video','play_addr','url_list',0)
end
|
#description ⇒ Object
32
33
34
|
# File 'lib/douyin_download/parser.rb', line 32
def description
info.dig('item_list',0,'desc')
end
|
#douyin_id ⇒ Object
14
15
16
17
|
# File 'lib/douyin_download/parser.rb', line 14
def douyin_id
url = @url.gsub /https?:\/\/v\.douyin\.com\//,''
url.gsub '/',''
end
|
#download(filename: nil) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/douyin_download/parser.rb', line 77
def download(filename: nil)
begin
body = Faraday.get(play_url, {}, 'User-Agent': MOBILE_USER_AGENT).body
filename ||= douyin_id + ".mp4"
File.open(filename,"wb+"){|f|
f.write body
}
filename
rescue Exception
end
end
|
#download_url ⇒ Object
28
29
30
|
# File 'lib/douyin_download/parser.rb', line 28
def download_url
info.dig('item_list',0,'video','download_addr','url_list',0)
end
|
#info ⇒ Object
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
|
# File 'lib/douyin_download/parser.rb', line 47
def info
@info ||= begin
result = {}
ua = MOBILE_USER_AGENT
body = Faraday.get(long_url, {}, 'User-Agent': ua).body
regexp = /\.init\(\{(.+)\}\)\;\s+\}\)\;/m
api_url = nil
if body =~ regexp
json = "{#{$1}}"
data = parse_json json
item_ids = data[:itemId]
dytk = data[:dytk]
api_url = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=#{item_ids}&dytk=#{dytk}"
else
end
if api_url
body = Faraday.get(api_url).body
result = JSON.parse body
end
result
end
end
|
#long_url ⇒ Object
19
20
21
22
23
24
25
26
|
# File 'lib/douyin_download/parser.rb', line 19
def long_url
@long_url ||= begin
= Faraday.get(@url).
['location']
end
end
|
#play_url ⇒ Object
40
41
42
43
44
45
|
# File 'lib/douyin_download/parser.rb', line 40
def play_url
= Faraday.get(api_play_url,{},'User-Agent': MOBILE_USER_AGENT).
['location']
end
|