Skip to content

Instantly share code, notes, and snippets.

@PofMagicfingers
Created June 30, 2019 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PofMagicfingers/8bdcff7d68daa57aaef5c5007befa980 to your computer and use it in GitHub Desktop.
Save PofMagicfingers/8bdcff7d68daa57aaef5c5007befa980 to your computer and use it in GitHub Desktop.
Season, episode and episode type inferred from title
def infer_type_season_and_episode!
# try to detect episode number
tome_chapter_matcher = /(?>\bT(?>ome)?[\s\.]*(?<tome>[0-9]+)\b)?[\s\.]*\bCh(?>apitre|apter)?[\s\.]*(?<chapter>[0-9]+)\b/i
season_episode_matcher = /\b(S(?>eason|aison)?[\s\.]*(?<season>[0-9]+)[\s.]*)?(E|É)(?>pisode|p)?[\s\.]*(?<episode>[0-9]+)\b/i
simple_episode_matcher = /(?>#{::Regexp.escape(self.feed.try(:title))}\s*\#?|\#\s*|°\s*|(É|E)(?>pisode|p)[\s\.]*)(?<episode>[0-9]+)\b(?>$|\.?\s+|\:)/i
_tome = _chapter = _season = _episode = 0
tome_chapter_match = tome_chapter_matcher.match(self.title)
if tome_chapter_match
Rails.logger.debug("Result from regexp : #{tome_chapter_match.inspect}")
_tome = tome_chapter_match[:tome].to_i
_chapter = tome_chapter_match[:chapter].to_i
end
Rails.logger.debug("Found tome #{_tome} and chapter #{_chapter}")
season_episode_match = season_episode_matcher.match(self.title)
if season_episode_match
Rails.logger.debug("Result from regexp : #{season_episode_match.inspect}")
_season = season_episode_match[:season].to_i
_episode = season_episode_match[:episode].to_i
end
Rails.logger.debug("Found season #{_season} and episode #{_episode}")
unless _tome + _chapter + _season + _episode > 0
simple_episode_match = simple_episode_matcher.match(self.title)
Rails.logger.debug("Result from regexp : #{simple_episode_match.inspect}")
if simple_episode_match
_episode = simple_episode_match[:episode].to_i
Rails.logger.debug("Found episode #{_episode}")
end
end
_bonus = self.title.match(/bonus/i).present?
_trailer = self.title.match(/trailer|bande[\-\ ]annonce|teaser|teasing/i).present?
Rails.logger.debug("bonus #{_bonus.to_json} _trailer : #{_trailer.to_json}")
Rails.logger.debug(((_season+_tome+_episode+_chapter > 0) or _bonus or _trailer).to_json)
self.inferred_type_season_and_episode = ((_season+_tome+_episode+_chapter > 0) or _bonus or _trailer)
if self.inferred_type_season_and_episode
self.episode_type = _bonus ? "bonus" : _trailer ? "trailer" : "full"
if _season > 0
if _tome > 0
self.season = 100*_tome+_season
else
self.season = _season
end
elsif _tome > 0
self.season = _tome
end
if _episode > 0
if _chapter > 0
self.episode = 100*_chapter+_episode
else
self.episode = _episode
end
elsif _chapter > 0
self.episode = _chapter
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment