Module: CinemaInformation

Defined in:
lib/cinema_information.rb,
lib/cinema_information/version.rb

Constant Summary collapse

VERSION =
"0.0.7"

Class Method Summary collapse

Class Method Details

.assemble_prog_with_info(prog, info) ⇒ Object



298
299
300
301
# File 'lib/cinema_information.rb', line 298

def self.assemble_prog_with_info(prog,info)
  prog.store(:result, info)
  prog
end

.configure_api_key(key) ⇒ Object



20
21
22
# File 'lib/cinema_information.rb', line 20

def self.configure_api_key(key)
  @config[:key] = key
end

.configure_file_path(path) ⇒ Object



16
17
18
# File 'lib/cinema_information.rb', line 16

def self.configure_file_path(path)
  @config[:prog] = path
end

.get_id_from_title(title) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/cinema_information.rb', line 236

def self.get_id_from_title(title)
  Tmdb::Api.key(@config[:key])
  Tmdb::Api.language("fr")
  @search = Tmdb::Search.new
  @search.resource('movie')
  @search.query(title)
  @movie = @search.fetch
  if @movie.nil?
    puts title
  else
    @movie[0][:id]
  end
end

.get_info(path, key, size) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/cinema_information.rb', line 303

def self.get_info(path, key, size)
  #Configure file path
  CinemaInformation.configure_file_path(path)
  #Configure api key
  CinemaInformation.configure_api_key(key)
  #Parse the whole ods file
  @programme = CinemaInformation.parse_ods
  #Get the movie list
  movie_list = CinemaInformation.get_movie
  #Get the movie information from the list
  @movie_info = CinemaInformation.get_movie_info_from_list(movie_list, size)
  #Assemble programme with movie information
  @result = CinemaInformation.assemble_prog_with_info(@programme, @movie_info)
  #Return a CinemaObject
  @cinema_object = CinemaObject.new(@result)
  @cinema_object
end

.get_movieObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/cinema_information.rb', line 193

def self.get_movie
  s = Roo::OpenOffice.new(@config[:prog], ods_options: {encoding: Encoding::UTF_8})
  film = {}
  lastrow = s.last_row
  lastcolumn = s.last_column
  y = 2

  while y != lastrow  do
    x = 1
    while x != lastcolumn+1 do
      value_from_cell = s.cell(y,x)

      if x == 2 or x == 5
        unless film.has_key?(value_from_cell)
          unless value_from_cell.nil?
            if value_from_cell.include? ':'
              value_from_cell.sub!(':', ' ')
            end
            if value_from_cell.include? "'"
              value_from_cell.sub!("'", ' ')
            end
            if value_from_cell.include? ","
              value_from_cell.sub!(",", '')
            end
            value = CGI::escape(value_from_cell)
            film.store(value_from_cell,[value])
          end
        end
      end
      x += 1
    end
    y += 1
  end
  film
end

.get_movie_casts(id) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/cinema_information.rb', line 250

def self.get_movie_casts(id)
  Tmdb::Api.key(@config[:key])
  Tmdb::Api.language("fr")
  @casts = Tmdb::Movie.casts(id)
  @casting = {}
  actor = {}
  i = 0
  if @casts.nil?
    actor = 'casting non disponible'
    @casting = actor
  else
    while i < 3
      unless @casts[i].nil?
        actor[:role] = @casts[i]["character"]
        actor[:name] = @casts[i]["name"]
      end
      @casting.store(i, actor)
      actor = {}
      i += 1
    end
  end
@casting
end

.get_movie_details(id) ⇒ Object



229
230
231
232
233
234
# File 'lib/cinema_information.rb', line 229

def self.get_movie_details(id)
  Tmdb::Api.key(@config[:key])
  Tmdb::Api.language("fr")
  movie = Tmdb::Movie.detail(id)
  movie
end

.get_movie_info_from_list(list, size) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/cinema_information.rb', line 274

def self.get_movie_info_from_list(list, size)
  result = {}
  @listresult = {}
  list.each_pair do |k, v|
    id = CinemaInformation.get_id_from_title(v)
    movie = CinemaInformation.get_movie_details(id)
    casting = CinemaInformation.get_movie_casts(id)
    result[:id] = id
    result[:title] = movie.title
    result[:title_list] = k
    result[:synopsis] = movie.overview
    unless movie.genres.nil?
      result[:genre] = movie.genres.collect { |g| g[:name] }
    end
    unless movie.poster_path.nil?
      result[:poster] = 'http://image.tmdb.org/t/p/'+size+'/'+movie.poster_path
    end
    result[:casting] = casting
    @listresult.store(k, result)
    result = {}
  end
  @listresult
end

.parse_odsObject



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cinema_information.rb', line 24

def self.parse_ods
  #open ods file with UTF_8 encoding option
  s = Roo::OpenOffice.new(@config[:prog], ods_options: {encoding: Encoding::UTF_8})

  @result = {}
  @daily_schedule = {}
  day = {}
  lastrow = s.last_row
  lastcolumn = s.last_column
  y = 2

  @result[:mois] = s.cell(1,3)
  @result[:cinema1] = s.cell(1,2)
  @result[:cinema2] = s.cell(1,5)

  while y != lastrow  do
    x = 1

    while x != lastcolumn+1 do

      value = s.cell(y,x)

      if s.cell(y, 3).nil?
        key = s.cell(y-1, 3).gsub(/[^\d]/, '')
        flag = true #second line of a date
      else
        key = s.cell(y, 3).gsub(/[^\d]/, '')
        flag = false #first line of a date
      end

      if x == 1
        if flag == false
          day[:horaire1] = value
        elsif flag == true
          day[:horaire2] = value
        end
      end
      if x == 2
        if flag == false
          day[:film1] = value
        elsif flag == true
          day[:film3] = value
        end
      end
      if x == 3 and flag == false
        day[:date] = value
      end
      if x == 5
        if flag == false
          day[:film2] = value
        elsif flag == true
          day[:film4] = value
        end
      end
      x += 1
    end
    @daily_schedule.store( key, day)
    if flag == true
      day = {}
    end
    y +=1
  end
  @result.store( :daily_schedule, @daily_schedule)
  @result
end

.parse_ods_with_day(date) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/cinema_information.rb', line 90

def self.parse_ods_with_day(date)
  s = Roo::OpenOffice.new(@config[:prog], ods_options: {encoding: Encoding::UTF_8})

  @result = {}
  @daily_schedule = {}
  day = {}
  film = {}
  lastrow = s.last_row
  lastcolumn = s.last_column
  y = 2 # Start on line 2 to avoid header

  @result[:mois] = s.cell(1,3)
  @result[:cinema1] = s.cell(1,2)
  @result[:cinema2] = s.cell(1,5)

  while y != lastrow  do
    x = 1

    while x != lastcolumn+1 do

      value = s.cell(y,x) #used for daily_schedule
      value_from_cell = s.cell(y,x) # used for movie list

      if s.cell(y, 3).nil?
        key = s.cell(y-1, 3).gsub(/[^\d]/, '')

        flagline = true # second line of a date

        if key == date
          flagdate = true # first line of the day researched'
        else
          flagdate = false # first line of a day not researched'
        end

      else
        key = s.cell(y, 3).gsub(/[^\d]/, '')
        flagline = false #first line of a date

        if key == date
          flagdate = true #second line of the day researched'
        else
          flagdate = false #second line of a day not researched'
        end

      end

      if x == 2 or x == 5 and flagdate == true
        unless film.has_key?(value_from_cell)
          unless value_from_cell.nil?
            if value_from_cell.include? ':'
              value_from_cell.sub!(':', ' ')
            end
            if value_from_cell.include? "'"
              value_from_cell.sub!("'", ' ')
            end
            if value_from_cell.include? ","
              value_from_cell.sub!(",", '')
            end
            value_escaped = CGI::escape(value_from_cell)
            film.store(value_from_cell,[value_escaped])
          end
        end
      end

      if x == 1 and flagdate == true
        if flagline == false
          day[:horaire1] = value
        elsif flagline == true
          day[:horaire2] = value
        end
      end
      if x == 2 and flagdate == true
        if flagline == false
          day[:film1] = value
        elsif flagline == true
          day[:film3] = value
        end
      end
      if x == 3 and flagline == false and flagdate == true
        day[:date] = value
      end
      if x == 5 and flagdate == true
        if flagline == false
          day[:film2] = value
        elsif flagline == true
          day[:film4] = value
        end
      end
      x += 1
    end
    unless day[:date].nil?
      @daily_schedule.store( key, day)
    end
    if flagline == true
      day = {}
    end
    y +=1
  end
  @result.store( :daily_schedule, @daily_schedule)
  @result.store( :result, film)
  @result
end

.select_daily_program(path, key, size, day) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/cinema_information.rb', line 321

def self.select_daily_program(path, key, size, day)
  #Configure file path
  CinemaInformation.configure_file_path(path)
  #Configure api key
  CinemaInformation.configure_api_key(key)
  #Parse the ods file in function of the parameter day and nb of day
  @result = CinemaInformation.parse_ods_with_day(day)
  #Get the movie information from the list
  @movie_info = CinemaInformation.get_movie_info_from_list(@result[:result], size)
  #Assemble programme with movie information
  @result = CinemaInformation.assemble_prog_with_info(@result, @movie_info)
  #Return a CinemaObject
  @cinema_object = CinemaObject.new(@result)
  @cinema_object
end