Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/lib
/build
*.dwarf
/Gemfile.lock
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'openssl'
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ all: teletube

teletube: main.cr src/**/*.cr
shards
ruby scripts/options.rb
bundle
bundle exec ruby scripts/options.rb
crystal build --error-trace -o teletube main.cr
@strip teletube
@du -sh teletube

release: main.cr src/**/*.cr
shards
ruby scripts/options.rb
bundle
bundle exec ruby scripts/options.rb
crystal build main.cr --release -o teletube
@strip teletube
@du -sh teletube
ruby scripts/build.rb
bundle exec ruby scripts/build.rb

clean:
rm -rf .crystal teletube .deps .shards libs lib *.dwarf build
Expand All @@ -22,4 +24,4 @@ PREFIX ?= /usr/local

install: teletube
install -d $(PREFIX)/bin
install teletube $(PREFIX)/bin
install teletube $(PREFIX)/bin
7 changes: 6 additions & 1 deletion scripts/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
BASE_URI = 'https://tube.switch.ch'
RESOURCES = {
'channel' => '/schema/v1/channel.json',
'video' => '/schema/v1/video.json'
'video' => '/schema/v1/video.json',
'collection' => '/schema/v1/collection.json',
'create_collection_item' => '/schema/v1/create_collection_item.json',
'update_collection_item' => '/schema/v1/update_collection_item.json',
'create_collaborator' => '/schema/v1/create_collaborator.json',
'create_collaborator_invitation' => '/schema/v1/create_collaborator_invitation.json',
}.freeze

class Property
Expand Down
44 changes: 44 additions & 0 deletions src/teletube/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,48 @@ module Teletube
else
@client.get_channels
end
when "collaborators"
case context.command
when "create"
@client.create_collaborator
when "destroy"
@client.destroy_collaborator
else
@client.get_collaborators
end
when "collaborator-invitations"
case context.command
when "create"
@client.create_collaborator_invitation
when "destroy"
@client.destroy_collaborator_invitation
else
@client.get_collaborator_invitations
end
when "collection-items"
case context.command
when "create"
@client.create_collection_item
when "update"
@client.update_collection_item
when "destroy"
@client.destroy_collection_item
else
@client.get_collection_items
end
when "collections"
case context.command
when "create"
@client.create_collection
when "show"
@client.get_collection
when "update"
@client.update_collection
when "destroy"
@client.destroy_collection
else
@client.get_collections
end
when "documents"
case context.command
when "create"
Expand Down Expand Up @@ -109,6 +151,8 @@ module Teletube
else
@client.get_profiles_me
end
when "positions"
@client.update_positions
when "progress"
@client.get_video_progress
when "restorations"
Expand Down
117 changes: 117 additions & 0 deletions src/teletube/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,109 @@ module Teletube
)
end

def get_collaborators
handle_response(
@http.get(
path:
"/api/v1/#{@context.params["type"]}/#{@context.params["id"]}" \
"/collaborators"
)
)
end

def create_collaborator
handle_response(
@http.post(
path:
"/api/v1/#{@context.params["type"]}/#{@context.params["id"]}" \
"/collaborators",
params: @context.params
)
)
end

def destroy_collaborator
handle_response(@http.delete(path: "/api/v1/collaborators/#{@context.params["id"]}"))
end

def get_collaborator_invitations
handle_response(
@http.get(
path:
"/api/v1/#{@context.params["type"]}/#{@context.params["id"]}" \
"/collaborator_invitations"
)
)
end

def create_collaborator_invitation
handle_response(
@http.post(
path:
"/api/v1/#{@context.params["type"]}/#{@context.params["id"]}" \
"/collaborator_invitations",
params: @context.params
)
)
end

def destroy_collaborator_invitation
handle_response(
@http.delete(path: "/api/v1/collaborator_invitations/#{@context.params["id"]}")
)
end

def get_collection_items
handle_response(
@http.get(path: "/api/v1/collections/#{@context.params["collection_id"]}/collection_items")
)
end

def create_collection_item
handle_response(
@http.post(
path: "/api/v1/collections/#{@context.params["collection_id"]}/collection_items",
params: @context.params
)
)
end

def update_collection_item
handle_response(
@http.patch(
path: "/api/v1/collection_items/#{@context.params["id"]}", params: @context.params
)
)
end

def destroy_collection_item
handle_response(@http.delete(path: "/api/v1/collection_items/#{@context.params["id"]}"))
end

def get_collections
handle_response(@http.get(path: "/api/v1/collections"))
end

def create_collection
handle_response(@http.post(path: "/api/v1/collections", params: @context.params))
end

def get_collection
handle_response(@http.get(path: "/api/v1/collections/#{@context.params["id"]}"))
end

def update_collection
handle_response(
@http.patch(path: "/api/v1/collections/#{@context.params["id"]}", params: @context.params)
)
end

def destroy_collection
handle_response(
@http.delete(path: "/api/v1/collections/#{@context.params["id"]}")
)
end

class Upload
# 50 megabytes
SEGMENT_SIZE = 52428800
Expand Down Expand Up @@ -205,6 +308,8 @@ module Teletube
handle_response(@http.get(path: "/api/v1/videos/#{@context.params["video_id"]}/documents"))
elsif @context.params.has_key?("channel_id")
handle_response(@http.get(path: "/api/v1/channels/#{@context.params["channel_id"]}/documents"))
else
handle_response(@http.get(path: "/api/v1/documents"))
end
end

Expand Down Expand Up @@ -238,6 +343,18 @@ module Teletube
handle_response(@http.get(path: "/api/v1/profiles/me"))
end

def update_positions
positions = @context.params["positions"].as_s.split(",").map do |position|
position.to_i
end
handle_response(
@http.patch(
path: "/api/v1/#{@context.params["type"]}/#{@context.params["id"]}/positions",
params: positions
),
)
end

def get_video_progress
handle_response(@http.get(path: "/api/v1/videos/#{@context.params["video_id"]}/progress"))
end
Expand Down
2 changes: 1 addition & 1 deletion src/teletube/http.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module Teletube
@http.post(path: path, headers: all, body: body)
end

def patch(path : String, params : Hash(String, JSON::Any))
def patch(path : String, params : Array | Hash(String, JSON::Any))
@http.patch(path: path, headers: @headers, body: params.to_json)
end

Expand Down
Loading