require_relative "/opt/ruby/tools/main.rb" require 'nokogiri' =begin #Use like: c = Csfd.new 2595 puts c.all.inspect =end class Csfd def initialize id if id.to_i>0 url = 'https://www.csfd.cz/film/'+id.to_s @id = id else url = id @id = url.sub(/^.*\/film\/([0-9]*).*?$/, '\1').to_i end raise 'Bad URL' unless url w = ::Dms::Wget.new x = w.get url @url = w.last_url @html = Nokogiri::HTML x end def url @url end def id @id end def page_title x = @html.css("title").first return false unless x x.children.to_s.sub(' | ČSFD.cz','') end def not_found return true if page_title =~ /.*Chyba - stránka nenalezena.*/ raise '502 Bad Gateway' if page_title == '502 Bad Gateway' false end def clean_name @html.css("h1").first.children.to_s.gsub(/<.*>/, "").strip end def name @html.css("h1").first.children.to_s.strip end def names ret = [self.clean_name] @html.css("ul.names li").each do |li| ret << li.css('h3').first.text.strip rescue nil end ret.uniq end def year a = @html.css(".origin span").first return false unless a a.children.to_s.strip end def country x = @html.css(".origin").first return false unless x x.children.to_s.gsub(/<.*>/, "").sub(/,.*$/,'').strip end def genre x = @html.css(".genre").first return false unless x x.children.to_s.strip rescue nil end def image a = @html.css("#poster img").first return false unless a a = a.attribute('src').to_s return false if a =~ /poster-free.png$/ 'https://img.csfd.cz' + a.sub('//img.csfd.cz','').sub('?h180','') end def description a = @html.css("#plots .content ul li div").first return false unless a a.text.strip end def rating x = @html.css("#rating meta[itemprop='ratingValue']") return false if !x || x.empty? return x.attribute('content').value.to_i end def get_creator name @people = @html.css(".creators").first unless @people return false unless @people @people.css('div').each do |d| x=d.css('h4').text.sub(':','') next unless x==name r = {} d.css('a').each do |p| id = p.attribute('href').to_s.sub(/^.*tvurce\/([0-9]*)-.*$/,'\1').to_i r[id] = p.text end return r end false end def direction self.get_creator 'Režie' end def scenario self.get_creator 'Scénář' end def camera self.get_creator 'Kamera' end def music self.get_creator 'Hudba' end def cast self.get_creator 'Hrají' end def producers self.get_creator 'Producenti' end def cut self.get_creator 'Střih' end def sound self.get_creator 'Zvuk' end def mask self.get_creator 'Masky' end def costumes self.get_creator 'Kostýmy' end def template self.get_creator 'Předloha' end def all return false if self.not_found x = { :page_title => self.page_title, :name => self.clean_name, :names => self.names } x[:year] = self.year if self.year x[:country] = self.country if self.country x[:genre] = self.genre if self.genre x[:image] = self.image if self.image x[:direction] = self.direction if self.direction x[:scenario] = self.scenario if self.scenario x[:camera] = self.camera if self.camera x[:music] = self.music if self.music x[:cast] = self.cast if self.cast x[:producers] = self.producers if self.producers x[:cut] = self.cut if self.cut x[:sound] = self.sound if self.sound x[:mask] = self.mask if self.mask x[:costumes] = self.costumes if self.costumes x[:description] = self.description if self.description x[:rating] = self.rating if self.rating x end #Search movies by name def self.list_movies name ret = {} url='https://www.csfd.cz/hledat/?q='+URI.escape(name) x = ::Dms::Wget::get url html = Nokogiri::HTML x #Direct redirection one = html.css('.g-plusone') unless one.empty? uri = one.attribute('data-href').value id = uri.sub(/.*\/film\/([0-9]*)-.*$/,'\1').to_i return id end #List lis = (html.css("#search-films li") rescue return false) lis.each do |z| desc = z.css('p').first desc = desc.children.to_s if desc x = z.css("h3 a") next if x.empty? x = x.first uri = x.attribute('href').value id = uri.sub(/\/film\/([0-9]*)-.*$/,'\1').to_i name = x.children.to_s ret[id] = {:name => name, :uri => uri} if desc ret[id][:desc] = desc ret[id][:year] = desc.sub(/^.* ([0-9]{4})$/,'\1') end end ret end end