Class: Shin::Data::Sfi

Inherits:
Object
  • Object
show all
Defined in:
lib/shin/data/sfi.rb

Defined Under Namespace

Classes: HTTPError, MissingArgument, NotValid

Instance Method Summary collapse

Instance Method Details

#info(params = {}) ⇒ Object

Info

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/shin/data/sfi.rb', line 58

def info(params={})
  raise MissingArgument, "You are missing the argument 'id' which is required to use this source." unless params[:id] != ""
  
  # Response
  response = Base.get('http://www.sfi.se/sv/svensk-filmdatabas/Item/?type=MOVIE&itemid=' + params[:id].to_s)
  raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200
  
  # Nokogiri parse
  @main_noko = Nokogiri::HTML response.body rescue nil
  
  # Can't be nil
  if @main_noko != nil
    # Data
    @swedish_title = @main_noko.css('td#ctl00_Full_Main_ctl00_SwedishTitleCell').text.gsub(/Mer$/, "").strip rescue nil
    @description = @main_noko.css('div#ctl00_Full_Main_ctl00_StorySummary').text.strip rescue nil
    @swedish_premiere = @main_noko.css('td#ctl00_Full_Main_ctl00_SwedishPremiereCell').text rescue nil
    @age_limit = @main_noko.css('span#ctl00_Full_Main_ctl00_AgeLimitContent').text.strip rescue nil
    
    {id: params[:id].to_i, swedish_title: @swedish_title, swedish_premiere: @swedish_premiere, description: @description, age_limit: @age_limit}.to_hashugar
  else
    raise NotValid, "Nokogiri failed to parse the HTML."
  end
end

#newObject



8
9
10
# File 'lib/shin/data/sfi.rb', line 8

def new
  self
end

#search(params = {}) ⇒ Object

Search (on title)

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/shin/data/sfi.rb', line 13

def search(params={})
  raise MissingArgument, "You are missing the argument 'title' which is required to use this source." unless params[:title] != ""
  
  params[:type] ||= "MovieTitle"
  params[:match] ||= "Begin"
  params[:year] ||= nil

  # Response
  response = Base.get('http://www.sfi.se/sv/svensk-filmdatabas/sokresultat/?searchword=' + URI.encode(params[:title]) + '&type=' + params[:type] + '&match=' + params[:match] + '&prom=False')
  raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200
  
  # Nokogiri parse
  @main_noko = Nokogiri::HTML response.body rescue nil
  
  # Can't be nil
  if @main_noko != nil
    @array = []
    
    @main_noko.css('div.search_result > div.search_heading').map do |e|
      @id = e.css('a')[0]['href'][/itemid\=(\d+)/, 1].to_i rescue nil
      
      @title = e.css('a').text.strip rescue nil
      @year = e.text[/\((\d+)/, 1].to_i rescue nil
      @country = e.text[/\((\d+), (.*)\)/, 2].to_s rescue nil
      
      # Find one or get all? (Allow years to be one less or one more aswell)
      if params[:year] == nil
        @array << {id: @id.to_i, title: @title, year: @year, country: @country}
      elsif @year == params[:year] or (@year - 1) == params[:year] or (@year + 1) == params[:year]
        # It should only return a single one if you have specified a year
        return {id: @id.to_i, title: @title, year: @year, country: @country}.to_hashugar
      end

    end
    
    # Return
    @array.to_hashugar
  else
    raise NotValid, "Nokogiri failed to parse the HTML."
  end
  
  
end

#titles(params = {}) ⇒ Object

Titles

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/shin/data/sfi.rb', line 83

def titles(params={})
  raise MissingArgument, "You are missing the argument 'id' which is required to use this source." unless params[:id] != ""
  
  # Response
  response = Base.get('http://www.sfi.se/en-GB/svensk-filmdatabas/Item/?type=MOVIE&iv=Titles&itemid=' + params[:id].to_s)
  raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200
  
  # Nokogiri parse
  @main_noko = Nokogiri::HTML response.body rescue nil
  
  # Can't be nil
  if @main_noko != nil
    @array = []
    
    @main_noko.css('table#ctl00_Full_Main_ctl00_TitlesTable > tr').map do |e|
      @type = e.css('th').text.strip.gsub(/\:$/, "") rescue nil
      @title = e.css('td').text.strip rescue nil
          
      @array << {type: @type, title: @title}
    end
    
    @array.to_hashugar
  else
    raise NotValid, "Nokogiri failed to parse the HTML."
  end
end