Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienpoly committed Oct 20, 2023
1 parent 5c976d0 commit ab56b20
Show file tree
Hide file tree
Showing 16 changed files with 1,694 additions and 2,572 deletions.
2 changes: 2 additions & 0 deletions app/clients/youtube/playlists.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Youtube
class Playlists < Youtube::Client
DEFAULT_METADATA_PARSER = "Youtube::VideoMetadata"
def all(channel_id:, title_matcher: nil)
items = all_items("/playlists", query: {channelId: channel_id, part: "snippet,contentDetails"}).map do |metadata|
OpenStruct.new({
Expand All @@ -10,6 +11,7 @@ def all(channel_id:, title_matcher: nil)
channel_id: metadata.snippet.channelId,
year: metadata.snippet.title.match(/\d{4}/).to_s.presence || DateTime.parse(metadata.snippet.publishedAt).year,
videos_count: metadata.contentDetails.itemCount,
metadata_parser: DEFAULT_METADATA_PARSER,
slug: metadata.snippet.title.parameterize
})
end
Expand Down
92 changes: 92 additions & 0 deletions app/models/youtube/video_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# require "active_support/core_ext/hash/keys"

# This class is used to extract the metadata from a youtube video
# it will try to:
# - extract the speakers from the title
# - remove the event_name from the title to make less redondant
# - remove leading separators from the title
module Youtube
class VideoMetadata
SPEAKERS_SECTION_SEPARATOR = " by "
SEPARATOR_IN_BETWEEN_SPEAKERS = / & |, | and /

def initialize(metadata:, event_name:, options: {})
@metadata = metadata
@event_name = event_name
end

def cleaned
OpenStruct.new(
{
title: title,
raw_title: raw_title,
speakers: speakers,
event_name: @event_name,
published_at: @metadata.published_at,
description: description,
video_id: @metadata.video_id
}
)
end

def keynote?
title_without_event_name.match(/keynote/i)
end

def lighting?
title_without_event_name.match(/lightning talks/i)
end

private

def extract_info_from_title
title_parts = title_without_event_name.split(SPEAKERS_SECTION_SEPARATOR)
speakers = title_parts.last.split(SEPARATOR_IN_BETWEEN_SPEAKERS).map(&:strip)
title = title_parts[0..-2].join(SPEAKERS_SECTION_SEPARATOR).gsub(/^\s*-/, "").strip

{
title: keynote? ? remove_leading_and_trailing_separators_from(title_without_event_name) : remove_leading_and_trailing_separators_from(title),
speakers: speakers
}
end

def speakers
return [] if lighting?

title_parts = title_without_event_name.split(SPEAKERS_SECTION_SEPARATOR)
title_parts.last.split(SEPARATOR_IN_BETWEEN_SPEAKERS).map(&:strip)
end

def raw_title
@metadata.title
end

def title_without_event_name
# RubyConf AU 2013: From Stubbies to Longnecks by Geoffrey Giesemann
# will return "From Stubbies to Longnecks by Geoffrey Giesemann"
remove_leading_and_trailing_separators_from(raw_title.gsub(@event_name, "").gsub(/\s+/, " "))
end

## remove : - and other separators from the title
def remove_leading_and_trailing_separators_from(title)
title.gsub(/^[-:]?/, "").strip.then do |title|
title.gsub(/[-:,]$/, "").strip
end
end

def title
if keynote? || lighting?
# when it is a keynote or lighting, usually we want to keep the full title without the event name
remove_leading_and_trailing_separators_from(title_without_event_name)
else
title_parts = title_without_event_name.split(SPEAKERS_SECTION_SEPARATOR)
title = title_parts[0..-2].join(SPEAKERS_SECTION_SEPARATOR).gsub(/^\s*-/, "").strip
remove_leading_and_trailing_separators_from(title)
end
end

def description
@metadata.description
end
end
end
84 changes: 84 additions & 0 deletions app/models/youtube/video_metadata_rails_world.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# require "active_support/core_ext/hash/keys"

# This class is used to extract the metadata from a youtube video
# it will try to:
# - extract the speakers from the title
# - remove the event_name from the title to make less redondant
# - remove leading separators from the title
module Youtube
class VideoMetadataRailsWorld
SPEAKERS_SECTION_SEPARATOR = " - "
SEPARATOR_IN_BETWEEN_SPEAKERS = / & |, | and /

def initialize(metadata:, event_name:, options: {})
@metadata = metadata
@event_name = event_name
end

def cleaned
OpenStruct.new(
{
title: title,
raw_title: raw_title,
speakers: speakers,
event_name: @event_name,
published_at: @metadata.published_at,
description: description,
video_id: @metadata.video_id
}
)
end

def keynote?
title_without_event_name.match(/keynote/i)
end

private

def extract_info_from_title
title_parts = title_without_event_name.split(SPEAKERS_SECTION_SEPARATOR)
speakers = title_parts.last.split(SEPARATOR_IN_BETWEEN_SPEAKERS).map(&:strip)
title = title_parts[0..-2].join(SPEAKERS_SECTION_SEPARATOR).gsub(/^\s*-/, "").strip

{
title: keynote? ? remove_leading_and_trailing_separators_from(title_without_event_name) : remove_leading_and_trailing_separators_from(title),
speakers: speakers
}
end

def speakers
raw_title_parts.first.split(SEPARATOR_IN_BETWEEN_SPEAKERS).map(&:strip)
end

def raw_title
@metadata.title
end

def title_without_event_name
# RubyConf AU 2013: From Stubbies to Longnecks by Geoffrey Giesemann
# will return "From Stubbies to Longnecks by Geoffrey Giesemann"
remove_leading_and_trailing_separators_from(raw_title.gsub(@event_name, "").gsub(/\s+/, " "))
end

## remove : - and other separators from the title
def remove_leading_and_trailing_separators_from(title)
return title if title.blank?

title.gsub(/^[-:]?/, "").strip.then do |title|
title.gsub(/[-:,]$/, "").strip
end
end

def title
remove_leading_and_trailing_separators_from(raw_title_parts[1])
end

def description
@metadata.description
end

def raw_title_parts
title_without_event_name.split(SPEAKERS_SECTION_SEPARATOR)
end
end
end
1 change: 1 addition & 0 deletions config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uAKB/HPZ9hm7lyAIpWgUe+24TG/N8bi2smtvEa7aD0nZNIU1sb0ctVEYNcSQbKJjHWSED68aF9/O7iYBt1QYU93in3J14rDvj0bHJ8GCWNEQFI6QekRPcOVrX4qehGn4Y29ikhsrc1/th6V0EHrkP4q3d+AtK0zazPLpLQgMFSMX5dYjck2vT8HfK3nOvX77+RUPXhyyO9roIBDBpSf8kEayzUi0q/h/k8cGsqbM3aL2InfXNM8gaQgJ7atEAzJsKoyt5yGoFxjWTDoOFEqnIIhXu1uRmW+S5aKUC/h0Kil/nEgsvufZGuPiDQgHvXZmO/3W3MmaCLaO2LHcp4qMySaH5SDBOXdzAmGuEl0SMhdyvtuZBCw60WmWRhh9GnhwBwJs/FdlaGnAGLZyB+lQhYYNTOww--9Tq1/zY6N+kujjV9--iOafU6Imt5fluW+yk+MPWQ==
Loading

0 comments on commit ab56b20

Please sign in to comment.