Skip to content

Commit

Permalink
feat: implement a youtube client in order to fix #24
Browse files Browse the repository at this point in the history
  • Loading branch information
erdnaxeli committed Dec 9, 2022
1 parent 7138673 commit f01d8d8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/blocker.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class Castblock::Blocker

@devices = Hash(Chromecast::Device, Channel(Nil)).new

def initialize(@chromecast : Chromecast, @sponsorblock : Sponsorblock, @seek_to_offset : Int32, @mute_ads : Bool, @skip_ads : Bool, @merge_threshold : Float64)
def initialize(@chromecast : Chromecast, @sponsorblock : Sponsorblock,
@youtube : Youtube, @seek_to_offset : Int32, @mute_ads : Bool,
@skip_ads : Bool, @merge_threshold : Float64)
end

def run : Nil
Expand Down
14 changes: 13 additions & 1 deletion src/castblock.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require "clip"
require "./blocker"
require "./chromecast"
require "./sponsorblock"
require "./youtube"

module Castblock
VERSION = "0.1.0"
Expand Down Expand Up @@ -40,6 +41,12 @@ module Castblock
"Adjust this value to skip multiple adjacent segments that don't overlap.")]
@merge_threshold = 0.0

@[Clip::Option("--youtube-api-key")]
@[Clip::Doc("An API key that will be used to search for the video is the " \
"chromecast does not send its id. If not API key is provided, " \
"it fallbacks on a scrapping way.")]
@youtube_api_key : String? = nil

def read_env
# If a config option equals its default value, we try to read it from the env.
# This is a temporary hack while waiting for Clip to handle it in a better way.
Expand All @@ -49,6 +56,10 @@ module Castblock
@categories = read_env_str_array(@categories, ["sponsor"], "CATEGORIES")
@mute_ads = read_env_bool(@mute_ads, false, "MUTE_ADS")
@merge_threshold = read_env_float(@merge_threshold, 0.0, "MERGE_THRESHOLD")

if @youtube_api_key.nil? && (var = ENV["YOUTUBE_API_KEY"]?)
@youtube_api_key = var
end
end

def read_env_bool(value : Bool?, default : Bool?, name : String) : Bool?
Expand Down Expand Up @@ -117,7 +128,8 @@ module Castblock
end

chromecast = Chromecast.new
blocker = Blocker.new(chromecast, sponsorblock, @seek_to_offset, @mute_ads, @skip_ads, @merge_threshold)
youtube = Youtube.new(@youtube_api_key)
blocker = Blocker.new(chromecast, sponsorblock, youtube, @seek_to_offset, @mute_ads, @skip_ads, @merge_threshold)

blocker.run
end
Expand Down
19 changes: 19 additions & 0 deletions src/youtube.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Castblock::Youtube
def initialize(@api_key : String? = nil)
end

# Get the id of a video given its author name and title.
def get_video_id(author : String, title : String) : String?
if !@api_key.nil?
get_video_id_from_api(author, title)
else
get_video_id_from_scrapping(author, title)
end
end

private def get_video_id_from_api(author : String, title : String) : String?
end

private def get_video_id_from_scrapping(author : String, title : String) : String?
end
end

0 comments on commit f01d8d8

Please sign in to comment.