Skip to content

Commit 2ff329d

Browse files
committed
Allow download of original file for video.
1 parent e8a96a6 commit 2ff329d

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/teletube/cli.cr

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ module Teletube
7171
@client.create_file
7272
when "upload"
7373
@client.upload_file
74+
when "download"
75+
@client.download_file
7476
else
7577
@client.get_files
7678
end

src/teletube/client.cr

+18
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,24 @@ module Teletube
154154
handle_response(@http.get(path: "/api/v1/videos/#{@context.params["video_id"]}/files"))
155155
end
156156

157+
def download_file
158+
response = @http.get(path: "/api/v1/videos/#{@context.params["video_id"]}/files")
159+
STDERR.puts "⚡️ #{response.status} (#{response.status_code})"
160+
if response.status_code == 200
161+
files = JSON.parse(response.body).as_a
162+
if files.empty?
163+
puts "🙁 The video does not have any files."
164+
else
165+
filename = files[0]["filename"] ? files[0]["filename"].as_s : "original.mov"
166+
File.open(filename, "w") do |file|
167+
HTTP::Client.get(files[0]["url"].as_s) do |response|
168+
IO.copy(response.body_io, file)
169+
end
170+
end
171+
end
172+
end
173+
end
174+
157175
def get_videos
158176
handle_response(@http.get(path: "/api/v1/channels/#{@context.params["channel_id"]}/videos"))
159177
end

src/teletube/option_parser.cr

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ module Teletube
9494
parser.on("upload", "Uploads a file.") do
9595
context.command = "upload"
9696
end
97+
parser.on("download", "Download the latest upload.") do
98+
context.command = "download"
99+
parser.on("--video-id ID", "The identifier of the video.") do |video_id|
100+
context.params["video_id"] = JSON::Any.new(video_id)
101+
end
102+
end
97103
end
98104

99105
parser.on("videos", "Interact with videos.") do

0 commit comments

Comments
 (0)