require 'hpricot' require 'open-uri' require 'zip/zip' # http://www.betaseries.com/ class BetaSeries def initialize(inspector) @inspector = inspector episodes = @inspector.episodes.select { |e| e.need_srt?(@inspector.langs) } $stdout.print "------------------------------------------\n" $stdout.print "Start srt searching from www.betaseries.com\n" $stdout.print "------------------------------------------\n" episodes.each do |e| @inspector.langs.each do |l| unless e.srt.include?(l) $stdout.print "#{e.episode_name_with_format} [#{l}]: " if get_srt(e, l) inspector.log.info "#{e.episode_name_with_format} [#{l}] // BetaSeries // #{@srt_name}" $stdout.print "FOUND\n" e.srt << l @inspector.growl_episode(e, l) break else $stdout.print "NOT FOUND\n" end end end end end private def get_srt(episode, lang) found = false agent = WWW::Mechanize.new doc = Hpricot(open(url(episode))) doc.search("div.episode").each do |el| title = el.search("div.titreep").inner_text if good_episode?(episode, title) el.search("ul.srt li a") do |srt_link| title = srt_link.inner_text if !found && good_language?(title, lang) && good_format?(episode, title) && no_tag?(title) Tempfile.open("tv.zip") do |tempfile| tempfile.write(agent.get_file(srt_link[:href])) # download the zip tempfile.close # tempfile need to be closed for unzip it Zip::ZipFile.open(tempfile.path) do |zip_file| if zip_file.size == 1 file = zip_file.first else zip_file.each do |f| if f.size > 5000 && good_episode?(episode, f.name) && good_language?(f.name, lang) && episode.good_format?(f.name) && no_tag?(f.name) file = f end end end if file @srt_name = file.name zip_file.extract(file, episode.srt_filename(lang)) found = true end end end end end end end rescue => e @inspector.log.fatal "BetaSeries // #{episode.episode_name_with_format} [#{lang}] // #{e}" found = false else found end def url(episode) "http://www.betaseries.com/ajax/planning_serie.php?url=#{tv_show(episode)}&saison=#{episode.season}" end def tv_show(episode) case episode.tv_show when "Terminator The Sarah Connor Chronicles" "thesarahconnorchronicles" when "CSI New York" "csiny" when "The IT Crowd" "itcrowd" else episode.tv_show end.downcase.gsub(/\s/, '') end def lang_version(lang) case lang when 'fr' "VF" when 'en' "VO" end end def num(episode) "#{episode.season}#{episode.number < 10 ? "0#{episode.number}" : episode.number}" end def good_episode?(episode, name) num2 = "S#{episode.season < 10 ? "0#{episode.season}" : episode.season}E#{episode.number < 10 ? "0#{episode.number}" : episode.number}" name.include?(num(episode)) || name.include?(num2) end def good_language?(name, lang) case lang when 'fr' name.include?("VF") || name.include?("fr") when 'en' name.include?("VO") || name.include?("en") end end def good_format?(episode, name) if name.include?("VO") || name.include?("VF") true else episode.good_format?(name) end end def no_tag?(filename) case filename when /no.?tag/i true when /tag/i false else true end end end