Module: MoviesClient

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

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.assemble_prog_with_info(prog, info) ⇒ Object



364
365
366
367
# File 'lib/movies_client.rb', line 364

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

.configure_api_key(key) ⇒ Object



21
22
23
# File 'lib/movies_client.rb', line 21

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

.configure_file_path(path) ⇒ Object



17
18
19
# File 'lib/movies_client.rb', line 17

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

.get_id_from_title(title) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/movies_client.rb', line 298

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



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/movies_client.rb', line 369

def self.get_info(path, key, size)
  #Configure file path

  MoviesClient.configure_file_path(path)
  #Configure api key

  MoviesClient.configure_api_key(key)
  #Parse the whole ods file

  @programme = MoviesClient.parse_ods
  #Get the movie information from the list

  @movie_info = MoviesClient.get_movie_info_from_list(@programme[:result], size)
  #Assemble programme with movie information

  @result = MoviesClient.assemble_prog_with_info(@programme, @movie_info)
  #Return a CinemaObject

  @cinema_object = MoviesObject.new(@result)
  @cinema_object
end

.get_movie_casts(id) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/movies_client.rb', line 312

def self.get_movie_casts(id)
  Tmdb::Api.key(@config[:key])
  Tmdb::Api.language("fr")
  @casts = Tmdb::Movie.casts(id)
  @credits = Tmdb::Movie.credits(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



291
292
293
294
295
296
# File 'lib/movies_client.rb', line 291

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



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/movies_client.rb', line 337

def self.get_movie_info_from_list(list, size)
  result = {}
  @listresult = {}
  list.each_pair do |k, v|
    id = MoviesClient.get_id_from_title(v)
    movie = MoviesClient.get_movie_details(id)
    casting = MoviesClient.get_movie_casts(id)
    result[:id] = id
    result[:title] = movie.title
    result[:title_list] = k
    result[:synopsis] = movie.overview
    unless movie.tagline == ''
      result[:tagline] = movie.tagline
    end
    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



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
89
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
# File 'lib/movies_client.rb', line 25

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 = {}
  film = {}
  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]/, '').to_s
        flagline = true # second line of a date

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

      end

      case x
        when 1
          if flagline == false
            day[:horaire1] = value
          elsif flagline == true
            day[:horaire2] = value
          end
        when 2
          if flagline == false
            day[:film1] = value
            unless film.has_key?(value)
              unless value.nil?
                if value.include? ':'
                  value.sub!(':', ' ')
                end
                if value.include? "'"
                  value.sub!("'", ' ')
                end
                if value.include? ","
                  value.sub!(",", '')
                end
                value_escaped = CGI::escape(value)
                film.store(value,[value_escaped])
              end
            end
          elsif flagline == true
            day[:film3] = value
            unless film.has_key?(value)
              unless value.nil?
                if value.include? ':'
                  value.sub!(':', ' ')
                end
                if value.include? "'"
                  value.sub!("'", ' ')
                end
                if value.include? ","
                  value.sub!(",", '')
                end
                value_escaped = CGI::escape(value)
                film.store(value,[value_escaped])
              end
            end
          end
        when 3
          unless flagline == true
            day[:date] = value
          end
        when 5
          if flagline == false
            day[:film2] = value
            unless film.has_key?(value)
              unless value.nil?
                if value.include? ':'
                  value.sub!(':', ' ')
                end
                if value.include? "'"
                  value.sub!("'", ' ')
                end
                if value.include? ","
                  value.sub!(",", '')
                end
                value_escaped = CGI::escape(value)
                film.store(value,[value_escaped])
              end
            end
          elsif flagline == true
            day[:film4] = value
            unless film.has_key?(value)
              unless value.nil?
                if value.include? ':'
                  value.sub!(':', ' ')
                end
                if value.include? "'"
                  value.sub!("'", ' ')
                end
                if value.include? ","
                  value.sub!(",", '')
                end
                value_escaped = CGI::escape(value)
                film.store(value,[value_escaped])
              end
            end
          end
        else
          # cellule non traité

      end
      x += 1
    end
    @daily_schedule.store( key, day)
    if flagline == true
      day = {}
    end
    y +=1
  end
  @result.store( :daily_schedule, @daily_schedule)
  @result.store( :result, film)
  @result
end

.parse_ods_with_day(date) ⇒ Object



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
192
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/movies_client.rb', line 155

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)

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

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

      end

      case key
        when date
          case x
            when 1
              if flagline == false
                day[:horaire1] = value
              elsif flagline == true
                day[:horaire2] = value
              end
            when 2
              if flagline == false
                day[:film1] = value
                unless film.has_key?(value)
                  unless value.nil?
                    if value.include? ':'
                      value.sub!(':', ' ')
                    end
                    if value.include? "'"
                      value.sub!("'", ' ')
                    end
                    if value.include? ","
                      value.sub!(",", '')
                    end
                    value_escaped = CGI::escape(value)
                    film.store(value,[value_escaped])
                  end
                end
              elsif flagline == true
                day[:film3] = value
                unless film.has_key?(value)
                  unless value.nil?
                    if value.include? ':'
                      value.sub!(':', ' ')
                    end
                    if value.include? "'"
                      value.sub!("'", ' ')
                    end
                    if value.include? ","
                      value.sub!(",", '')
                    end
                    value_escaped = CGI::escape(value)
                    film.store(value,[value_escaped])
                  end
                end
              end
            when 3
              unless flagline == true
                day[:date] = value
              end
            when 5
              if flagline == false
                day[:film2] = value
                unless film.has_key?(value)
                  unless value.nil?
                    if value.include? ':'
                      value.sub!(':', ' ')
                    end
                    if value.include? "'"
                      value.sub!("'", ' ')
                    end
                    if value.include? ","
                      value.sub!(",", '')
                    end
                    value_escaped = CGI::escape(value)
                    film.store(value,[value_escaped])
                  end
                end
              elsif flagline == true
                day[:film4] = value
                unless film.has_key?(value)
                  unless value.nil?
                    if value.include? ':'
                      value.sub!(':', ' ')
                    end
                    if value.include? "'"
                      value.sub!("'", ' ')
                    end
                    if value.include? ","
                      value.sub!(",", '')
                    end
                    value_escaped = CGI::escape(value)
                    film.store(value,[value_escaped])
                  end
                end
              end
            else
              # cellule non traité

          end
        else
          # date non traité

      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, date) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/movies_client.rb', line 385

def self.select_daily_program(path, key, size, date)
  #Configure file path

  MoviesClient.configure_file_path(path)
  #Configure api key

  MoviesClient.configure_api_key(key)
  #Parse the ods file in function of the parameter day and nb of day

  @result = MoviesClient.parse_ods_with_day(date)
  #Get the movie information from the list

  @movie_info = MoviesClient.get_movie_info_from_list(@result[:result], size)
  #Assemble programme with movie information

  @result = MoviesClient.assemble_prog_with_info(@result, @movie_info)
  #Return a CinemaObject

  @cinema_object = MoviesObject.new(@result)
  @cinema_object
end