-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpfdb.rb
763 lines (703 loc) · 30.9 KB
/
pfdb.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
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
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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
require 'csv'
require 'fileutils'
require 'json'
require 'terminal-table'
require 'text-table'
require_relative 'modules'
# Auxiliary methods
def time
Time.now.strftime("%Y%m%d%H%M%S") # %N for nanoseconds
end
def short_time
Time.now.strftime("%Y%m%d")
end
def format_time
Time.now.strftime("[%Y-%m-%d %H:%M:%S] ")
end
def filename
CONFIG['json'].remove(".json").concat(".json")
end
def backup_filename
filename.remove(".json").concat("_%s.json" % [time])
end
def autobackup_filename
filename.remove(".json").concat("_%s.json" % [short_time])
end
def message(type, text)
format_time + type.upcase + ": " + text + "\n"
end
# File manipulation methods
def backup(auto = false)
begin
old = (auto ? autobackup_filename : backup_filename)
if File.file?(filename) then FileUtils.cp(filename, old) end
rescue
log("ERROR", "Realizando la copia de seguridad de la base de datos.")
ensure
return old
end
end
def autobackup
backup(true) if CONFIG['autobackup'] && !File.file?(autobackup_filename)
rescue
log("ERROR", "Realizando la copia de seguridad automática.")
else
log("INFO", "Copia de seguridad automática realizada.")
end
def save
backup_name = backup
begin
File.delete(filename) if File.file?(filename)
File.open(filename, "w") do |f|
f.write($movies.to_json)
end
rescue
File.delete(filename) if File.file?(filename)
File.rename(backup_name, filename) if File.file?(backup_name)
log("ERROR", "Guardando la base de datos.")
else
File.delete(backup_name) if File.file?(backup_name)
end
end
def access
File.file?(filename) ? $movies = JSON.parse(File.read(filename), :symbolize_names => true) : $movies = JSON.parse("[]")
rescue
log("ERROR", "Cargando la base de datos.")
else
log("INFO", "Base de datos cargada, hay %i películas." % [$movies.size])
end
def emdb(emdb_filename = "emdb.dat")
backup_name = backup
$films = File.file?(emdb_filename) ? File.binread(emdb_filename).encode("utf-8", "utf-16le").split("\x1D") : []
if $films.blank?
log("ERROR", "Cargando la base de datos desde EMBD, el fichero no existe.")
else
$names = $films.last.split("]")[1..-2].map{ |s| s.split("[")[0].split("\r\n").delete_if{ |r| r.blank? } }
$names = {actors: $names[0], directors: $names[1], writers: $names[2], composers: $names[3]}
$films = $films[1..-2].map{ |s| s.split("\x1E") }
$films = $films.map{ |s|
{
title: s[0].to_s.squish,
original_title: s[1].to_s,
companies: s[2].to_s.split(","),
year: s[4].split("\r\n")[1].to_i,
directors: $names[:directors][s[5].to_i].to_s.split(","),
duration: s[6].to_i,
countries: s[7].to_s.split(","),
vista: s[13].to_i % 2 == 1 ? true : false,
deseada: s[13].to_i % 4 == 2 ? true : false,
propia: s[13].to_i % 8 == 4 ? true : false,
favorita: s[13].to_i % 16 == 8 ? true : false,
color: [s[15].to_s],
emdb_id: s[18].to_s,
writers: $names[:writers][s[19].to_i].to_s.split(","),
genres: s[20].split("\r\n")[1].scan(/./).map{ |g|
case g
when "A" then "Action"
when "C" then "Animation"
when "D" then "Drama"
when "F" then "Fantasy"
when "f" then "Film-Noir"
when "G" then "History"
when "g" then "Game-Show"
when "H" then "Horror"
when "I" then "Family"
when "K" then "Comedy"
when "M" then "Music"
when "m" then "Musical"
when "N" then "News"
when "O" then "War"
when "P" then "Sport"
when "R" then "Romance"
when "r" then "Reality-TV"
when "S" then "Sci-Fi"
when "s" then "Short"
when "T" then "Thriller"
when "t" then "Talk-Show"
when "U" then "Documentary"
when "V" then "Adventures"
when "W" then "Western"
when "X" then "Adult"
when "Y" then "Mistery"
when "!" then "Crime"
when "@" then "Biography"
else "Other"
end
},
imdb_id: s[21].to_s,
date_added: s[22][0..3].to_s + "-" + s[22][4..5].to_s + "-" + s[22][6..7].to_s,
certification: s[23].to_s.to_sym,
trailer_url: s[27].to_s,
date_viewed: s[28].to_s.split("\r\n")[0],
filmaffinity_synopsis: s[32].split("\r\n")[1].to_s,
imdb_votes: s[39].to_i,
imported: false,
updated: false
}
}
$films.each{ |f|
matches = $movies.each_with_index.select{ |s, i| s[:id][:imdb] == f[:imdb_id] }
if matches.count == 0
search(f[:title], "filmaffinity", output: false)
id = $search[0..-2].select{ |s| s[2] == f[:year].to_s }
if !id.blank? && !id[0].blank? && !id[0][0].blank?
id = id[0][0].to_s
matches = $movies.each_with_index.select{ |s, i| s[:id][:filmaffinity] == id }
if matches.count == 0
add_movie(idImdb: f[:imdb_id], idFilmAffinity: id, dual: true)
$movies.last[:dates][:added] = f[:date_added] # TODO: poner la funcion "modify" cuando este hecha (en vez de acceder a $movies directamente)
if f[:vista] then $movies.last[:dates][:viewed] << f[:date_viewed][0..3] + "-" + f[:date_viewed][4..5] + "-" + f[:date_viewed][6..7] end
f[:imported] = true
f[:updated] = true
else
log("INFO", "EMDB - La película %s ya está en la base de datos (Índices %s)" % [matches[0][0][:title], matches.map{ |s, i| i }.join(", ")])
f[:imported] = true
end
else
log("ERROR", "EMDB - Problema cargando película %s (Índice %i)" % [f[:title], f[:emdb_id]])
end
else
log("INFO", "EMDB - La película %s ya está en la base de datos (Índices %s)" % [matches[0][0][:title], matches.map{ |s, i| i }.join(", ")])
f[:imported] = true
end
}
end
rescue
log("ERROR", "Cargando la base de datos desde EMDB.")
else
File.delete(backup_name) if File.file?(backup_name)
save
log("INFO", "Base de datos de EMDB cargada, hay %i películas." % [$films.size])
end
def log(type, text, output = true)
print(message(type, text)) if output
if CONFIG['autolog'] || CONFIG['autolog'] == nil
begin
File.file?("log.txt") ? FileUtils.cp("log.txt", "log_backup.txt") : new_log = true
File.open("log.txt", "a") do |f|
f.write(message("info", "Creado nuevo fichero de log de PFDB, versión v1.0.\n\n")) if new_log
f.write(message(type, text))
end
rescue
if File.file?("log.txt") && File.file?("log_backup.txt")
File.delete("log.txt")
File.rename("log_backup.txt", "log.txt")
end
print(message("error", "Actualizando el log."))
else
File.delete("log_backup.txt") if File.file?("log_backup.txt")
end
end
end
# TODO: Completar añadiendo mas formatos, quiza añadir separaciones no printables para mayor seguridad, quiza añadir mas estilos (tipo tabla, ascii art, etc)
# TODO: Refactor by making each case add on to the previous one instead of repeating
def export(preset: :normal, output: :csv, style: :text)
preset = preset.to_sym
output = output.to_sym
result = []
case preset
when :minimal
header = ["IMDb ID", "FilmAffinity ID"]
result = $movies.map{ |s| [s[:id][:imdb], s[:id][:filmaffinity]] }
when :simple
header = ["IMDb ID", "FilmAffinity ID", "Title", "Year", "Duration", "Directors", "Dates viewed"]
result = $movies.map{ |s|
[
s[:id][:imdb].to_s,
s[:id][:filmaffinity].to_s,
s[:title].to_s,
s[:year].to_s,
s[:duration].to_s,
s[:directors].join(","),
s[:dates][:viewed].join(",")
]
}
when :normal
header = ["IMDb ID", "FilmAffinity ID", "Title", "Original title", "Year", "Duration", "Genres", "Countries", "Directors", "Cast", "Synopsis", "Dates viewed"]
result = $movies.map{ |s|
[
s[:id][:imdb].to_s,
s[:id][:filmaffinity].to_s,
s[:title].to_s,
s[:original_title].to_s,
s[:year].to_s,
s[:duration].to_s,
s[:genres].join(","),
s[:countries].join(","),
s[:directors].join(","),
s[:cast].map{ |k, v| k }.join(","),
s[:synopsis][:filmaffinity].to_s.remove("\n", "*"),
s[:dates][:viewed].join(",")
]
}
when :complete
header = ["IMDb ID", "FilmAffinity ID", "Title", "Original title", "Year", "Duration", "Genres", "Countries", "Directors", "Cast", "Synopsis", "Dates viewed"]
result = $movies.map{ |s|
[
s[:id][:imdb].to_s,
s[:id][:filmaffinity].to_s,
s[:title].to_s,
s[:original_title].to_s,
s[:year].to_s,
s[:duration].to_s,
s[:genres].join(","),
s[:countries].join(","),
s[:directors].join(","),
s[:cast].map{ |k, v| k }.join(","),
s[:synopsis][:filmaffinity].to_s.remove("\n", "*"),
s[:dates][:viewed].join(",")
]
}
else
log("ERROR", "Exportando la base de datos. El preset debe ser 'minimal', 'simple', 'normal' o 'complete'.")
return
end
result.insert(0,header)
case output
when :csv
result = result.map{ |s| s.join("*") }.join("\n")
File.open(filename.remove(".json") + "_export_" + time + ".csv", "w") do |f|
f.write(result)
end
when :txt
result = result.map{ |s| s.join("\n") }.join("\n\n")
File.open(filename.remove(".json") + "_export_" + time + ".txt", "w") do |f|
f.write(result)
end
else
log("ERROR", "Exportando la base de datos. El formato debe ser 'csv' o 'txt'.")
end
end
# TODO: Implementar esta funcion
def import
end
# Methods for manipulating the database
def modify(index, field, value)
bool = index < $movies.size && $movies[index].key?(field) # TODO: Comprobar también que el tipo/clase de value es correcto.
bool ? $movies[index][field] = value : raise("La película o el campo introducido no existe.")
rescue
log("ERROR", "Modificando el campo \"%s\" de la película \"%s\"." % [field, $movies[index][:title]])
end
def add_movie(id: "0068646", web: :imdb, dual: false, idImdb: "0068646", idFilmAffinity: "809297")
if web.is_a?(String) then web = web.to_sym end
movie = !dual ? Movie.new(id: id, web: web) : Movie.new(idImdb: idImdb, idFilmAffinity: idFilmAffinity, dual: true)
$movies << movie.to_hash if !movie.nil?
save if CONFIG['autosave']
id_string = !dual ? "ID %s en %s" % [id, web.to_s] : "ID %s en IMDb, ID %s en FilmAffinity" % [idImdb, idFilmAffinity]
log("info", "Añadida película \"%s\" (%s) a la base de datos." % [movie.to_hash[:title], id_string])
rescue
log("ERROR", "Añadiendo película a la base de datos.")
end
def delete_by_index(index, indexb = nil)
if index.is_a?(Integer) && index > -1 && index < $movies.size
if indexb == nil
name = $movies[index][:title]
$movies.delete_at(index)
log("INFO", "Eliminada película %s de la base de datos." % [name])
else
if indexb.is_a?(Integer) && indexb > -1 && indexb < $movies.size
if index <= indexb
(1..(indexb-index+1)).each{ |s| $movies.delete_at(index) }
log("INFO", "Eliminadas películas %s a %s de la base de datos." % [index.to_s, indexb.to_s])
else
(1..(index-indexb+1)).each{ |s| $movies.delete_at(indexb) }
log("INFO", "Eliminadas películas %s a %s de la base de datos." % [indexb.to_s, index.to_s])
end
else
print("La película introducida no existe.")
end
end
else
print("La película introducida no existe.")
end
end
def delete_by_title(title)
index = $movies.find_index{ |m| m[:title].downcase == title.downcase }
!index.nil? ? delete_by_index(index) : print("Película no encontrada en la base de datos.")
end
def delete(thing, thing2 = nil)
thing.is_a?(Integer) ? delete_by_index(thing, thing2) : (thing.is_a?(String) ? delete_by_title(thing) : raise)
save if CONFIG['autosave']
rescue
log("ERROR", "Eliminando película de la base de datos.")
end
def modify_movie(index, field, value)
modify(index, field, value)
save if CONFIG['autosave']
log("INFO", "Modificada película %s de la base de datos. Actualizado campo %s a valor %s" % [$movies[index][:title], field.to_s, value.to_s])
end
def search_from_web(query, web = :imdb, output = true)
search = []
case web
when :imdb
search = Imdb::Search.new(query)
when :filmaffinity
search = FilmAffinity::Search.new(query)
else
print("ERROR: Web incorrecta.")
return
end
$search = search.result + [web]
$searches[web] = search.result
search.show_result if output
rescue
log("ERROR", "Realizando una búsqueda en la web %s." % [web.to_s.capitalize])
end
def search_from_database(query, order: nil, reverse: false, web: :imdb)
movie = $movies.each_with_index.select{ |m, i| m[:title] =~ /#{query}/i }
!movie.blank? ? (movie.count > 1 ? list(set: movie, order: order, reverse: reverse, web: web) : view_movie(movie[0][0])) : print("No se encontró ningún resultado.")
rescue
log("ERROR", "Realizando una búsqueda en la base de datos.")
end
def search_dual(query, output = true) # TODO: Añadir comprobacion de ID's para evitar repetidos, tener en cuenta que quiero poder usarlo de forma desatendida (no lanzar una excepcion, solo un mensaje)
$searches[:imdb] = Imdb::Search.new(query).result
$searches[:filmaffinity] = FilmAffinity::Search.new(query).result
rows = []
rows << [{value: "IMDb", colspan: 3, alignment: :center}, {value: "FilmAffinity", colspan: 3, alignment: :center}]
rows << :separator
rows << ["ID", "Título", "Año"] + ["ID", "Título", "Año"]
rows << :separator
(0..(CONFIG['search_limit'].to_i - 1)).each{ |s|
if !$searches[:imdb][s].nil? || !$searches[:filmaffinity][s].nil?
imdb = !$searches[:imdb][s].nil? ? [s.to_s, $searches[:imdb][s][1].truncate(50), $searches[:imdb][s][2]] : ["", "", ""]
fa = !$searches[:filmaffinity][s].nil? ? [s.to_s, $searches[:filmaffinity][s][1].truncate(50), $searches[:filmaffinity][s][2]] : ["", "", ""]
rows << imdb + fa
end
}
table = Terminal::Table.new(title: "Resultado de la búsqueda \"#{query}\".", rows: rows)
print(table)
print("\n")
rescue
log("ERROR", "Realizando una búsqueda dual en internet.")
end
def search(query, web = nil, order: nil, reverse: false, output: true)
!web.nil? ? (!!web[/dual/i] ? search_dual(query, output) : search_from_web(query, web.to_sym, output)) : search_from_database(query, order: order, reverse: reverse, web: web)
end
def add_single(index)
if $search.blank?
print("ERROR: Aún no has hecho ninguna búsqueda.")
else
if index.is_a?(Integer) && index > -1 && index < $search.size - 1 # careful, the last element is the web
id = $search[index][0]
web = $search.last
!$movies.map{ |s| s[:id][web.to_sym] }.include?(id) ? add_movie(id: id, web: web) : print("Esta película ya está en la lista.")
else
print("ERROR: ID incorrecta.")
end
end
end
def add_dual(index1, index2) # TODO: Proporcionar opcion configurable para decidir que hacer en caso de que la peli ya este en la base de datos (ignorar, sustituir, etc)
if $searches[:imdb].blank? || $searches[:filmaffinity].blank?
print("ERROR: Aún no has hecho ninguna búsqueda.")
else
if index1.is_a?(Integer) && index1 > -1 && index1 < $searches[:imdb].size && index2.is_a?(Integer) && index2 > -1 && index2 < $searches[:filmaffinity].size
idImdb = $searches[:imdb][index1][0]
idFilmAffinity = $searches[:filmaffinity][index2][0]
name = $searches[:imdb][index1][1]
imdbMatches = $movies.each_with_index.select{ |s, i| s[:id][:imdb] == idImdb }
filmaffinityMatches = $movies.each_with_index.select{ |s, i| s[:id][:filmaffinity] == idFilmAffinity }
matches = imdbMatches + filmaffinityMatches
if matches.count == 0
add_movie(idImdb: idImdb, idFilmAffinity: idFilmAffinity, dual: true)
else
log("INFO", "La película %s ya está en la lista (Índices %s)." % [name, matches.map{ |s, i| i }.uniq.join(", ")])
end
else
print("ERROR: Alguna ID es incorrecta.")
end
end
end
def add(index1, index2 = nil)
index2.nil? ? add_single(index1) : add_dual(index1, index2)
end
def clean
$movies.delete_if{ |m| m[:title].blank? && m[:year] == 0 && m[:duration] == 0 && m[:cast].blank? && m[:genres].blank? }
$movies = $movies.select{ |m| m[:id][:imdb].to_s.blank? } + $movies.select{|m| !m[:id][:imdb].to_s.blank? }.uniq{ |m| m[:id][:imdb].to_s }
$movies = $movies.select{ |m| m[:id][:filmaffinity].to_s.blank? } + $movies.select{|m| !m[:id][:filmaffinity].to_s.blank? }.uniq{ |m| m[:id][:filmaffinity].to_s }
save if CONFIG['autosave']
log("INFO", "Base de datos limpiada de entradas vacías y duplicados.")
end
def autoclean
clean if CONFIG['autoclean']
end
# TODO: Complete this
def rate
end
# Methods for viewing the database
def multirow(text: "", cols: 1, length: 10)
text.scan(/.{1,#{length}}/).map{ |t| [{colspan: cols, value: t}] }
end
def delimitate(num)
num.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end
# TODO: añadir prevencion de blanks a todos, los dos ultimos no funcionan, añadir el resto de atributos
def order_list(list: nil, order: nil, reverse: false, web: :imdb)
if order.is_a?(String) then order = order.to_sym end
case order
when :title, :original_title, :duration, :year, :budget, :gross
list.sort!{ |a, b| !a[0][order].blank? && !b[0][order].blank? ? a[0][order] <=> b[0][order] : !a[0][order].blank? ? -1 : 1 }
when :countries, :genres, :directors, :writers, :producers, :composers, :cinematographers, :editors, :castings, :languages, :color
list.sort!{ |a, b| a[0][order].join <=> b[0][order].join }
when :id, :rating, :votes, :ranking
list.sort!{ |a, b| a[0][order][web] <=> b[0][order][web] }
else
print("ERROR: Clave introducida incorrecta.")
end
reverse ? list.reverse : list
end
def list(show: CONFIG['list_limit'].to_i, set: nil, order: nil, reverse: false, web: :imdb)
if order.is_a?(String) then order = order.to_sym end
header = ["Id", "Año", "Título", "Dur", "Director/es", "País/es", "Vista"]
films = (set.nil? ? $movies.each_with_index.take(show) : set.take(show))
films = !order.nil? ? order_list(list: films, order: order, reverse: reverse, web: web) : films
table = films.map{ |m, i|
[i.to_s,
m[:year].truncate(4),
m[:title].truncate(30),
m[:duration].truncate(3),
m[:directors].join(", ").truncate(30),
m[:countries].join(", ").truncate(30),
m[:dates][:viewed].last == "" ? "No" : m[:dates][:viewed].last
]
}.insert(0, header)
sep = CONFIG['list_separation'].to_i + 1
while sep < table.count - 1
table.insert(sep, :separator)
sep += CONFIG['list_separation'].to_i + 1
end
print(table.to_table(:first_row_is_head => true).align_column(1, :right).align_column(4, :right))
rescue
log("ERROR", "Listando las películas de la base de datos.")
end
def order(show: CONFIG['list_limit'].to_i, set: nil, order: nil, reverse: false, web: :imdb)
list(show: show, set: set, order: order, reverse: reverse, web: web)
end
def sort(show: CONFIG['list_limit'].to_i, set: nil, order: nil, reverse: false, web: :imdb)
list(show: show, set: set, order: order, reverse: reverse, web: web)
end
def view_movie(movie)
width = [CONFIG['movie_card_width'].to_i, 0].max
field_width = [((width - 1) / 5) - 3, 0].max
field_length = [width - field_width - 7, 0].max
limit = [CONFIG['movie_card_field_length_limit'].to_i, 0].max
rows = []
rating = !movie[:id][:imdb].blank? ? movie[:rating][:imdb].to_s : movie[:rating][:filmaffinity].to_s
votes = !movie[:id][:imdb].blank? ? movie[:votes][:imdb].to_s : movie[:votes][:filmaffinity].to_s
rows << [(movie[:year].to_s).truncate(field_width),
(movie[:duration].to_s + " minutos").truncate(field_width),
(rating.to_s + " (" + votes.to_s + " votos)").truncate(field_width),
movie[:genres].join(", ").truncate(field_width),
movie[:countries].join(", ").truncate(field_width)]
rows << :separator
rows << [movie[:color][0].to_s.truncate(field_width),
movie[:languages].join(", ").truncate(field_width),
movie[:companies][0].to_s.truncate(field_width),
(delimitate(movie[:budget]) + "$").truncate(field_width),
(delimitate(movie[:gross]) + "$").truncate(field_width)]
rows << :separator
if !movie[:directors].blank? then rows << ["Dirección"] + [{colspan: 4, value: movie[:directors].join(", ").truncate(field_length)}] end
if !movie[:writers].blank? then rows << ["Guión"] + [{colspan: 4, value: movie[:writers].join(", ").truncate(field_length)}] end
if !movie[:producers].blank? then rows << ["Producción"] + [{colspan: 4, value: movie[:producers].join(", ").truncate(field_length)}] end
if !movie[:cinematographers].blank? then rows << ["Cinematografía"] + [{colspan: 4, value: movie[:cinematographers].join(", ").truncate(field_length)}] end
if !movie[:composers].blank? then rows << ["Música"] + [{colspan: 4, value: movie[:composers].join(", ").truncate(field_length)}] end
if !movie[:editors].blank? then rows << ["Edición"] + [{colspan: 4, value: movie[:editors].join(", ").truncate(field_length)}] end
rows << :separator
text = movie[:cast].map{ |k, v| k.to_s + (!v.blank? ? " (" + v.to_s + ")" : "") }.take(CONFIG['cast_list'].to_i).join(", ").truncate(limit)
multirow(text: text, cols: 4, length: field_length).each_with_index{ |r, i|
if i == 0 then rows << ["Cast"] + r
else rows << [""] + r end
}
rows << :separator
text = !movie[:id][:imdb].blank? ? movie[:synopsis][:imdb] : movie[:synopsis][:filmaffinity]
if !text.blank?
multirow(text: text.truncate(limit), cols: 4, length: field_length).each_with_index{ |r, i|
if i == 0 then rows << ["Sinopsis"] + r
else rows << [""] + r end
}
end
table = Terminal::Table.new(title: movie[:title].truncate(40).upcase + " (" + movie[:original_title].truncate(40).upcase + ")", rows: rows)
print(table)
print("\n")
end
def view(movie, order: nil, reverse: false, web: :imdb)
case movie.class.to_s
when "Integer"
if movie > -1 && movie < $movies.count
view_movie($movies[movie])
else
print("ERROR: ID incorrecta.")
end
when "String"
result = $movies.each_with_index.select{ |m, i| m[:title] =~ /#{movie}/i }
!result.blank? ? (result.count > 1 ? list(set: result, order: order, reverse: reverse, web: web) : view_movie(result[0][0])) : print("No se encontró ningún resultado.")
else
print("ERROR: Debes introducir un entero (índice de la película) o una cadena de texto (título de la película).")
end
rescue
log("ERROR", "Visualizando películas de la base de datos.")
end
def filter(mode: :intersection, order: nil, reverse: false, web: :imdb, **fields) # TODO: do extensive testing, finish it, add remaining fields, maybe more modes and flexibility
if mode.is_a?(String) then mode = mode.to_sym end
bool = false
case mode
when :intersection
result = $movies.each_with_index
fields.each{ |k, v|
if [:title, :original_title].include?(k)
result = result.select{ |m, i| m[k] =~ /#{v}/i }
bool = true
elsif [:countries, :genres, :directors, :writers, :producers, :composers, :cinematographers, :editors, :castings, :companies, :languages, :color].include?(k)
result = result.select{ |m, i| m[k].join("|") =~ /#{v}/i }
bool = true
elsif [:year, :duration, :budget, :gross].include?(k)
result = result.select{ |m, i| m[k] >= v[0] && m[k] <= v[1] }
bool = true
else
print("ERROR: La clave %s no existe.\n" % [k])
end
}
(!result.blank? && bool == true) ? (result.count > 1 ? list(set: result, order: order, reverse: reverse, web: web) : view_movie(result[0][0])) : print("No se encontró ningún resultado.")
when :union
result = []
fields.each{ |k, v|
if [:title, :original_title].include?(k)
result = result | $movies.each_with_index.select{ |m, i| m[k] =~ /#{v}/i }
elsif [:countries, :genres, :directors, :writers, :producers, :composers, :cinematographers, :editors, :castings, :companies, :languages, :color].include?(k)
result = result | $movies.each_with_index.select{ |m, i| m[k].join("|") =~ /#{v}/i }
elsif [:year, :duration, :budget, :gross].include?(k)
result = result | $movies.each_with_index.select{ |m, i| m[k] >= v[0] && m[k] <= v[1] }
else
print("ERROR: La clave %s no existe." % [k])
end
}
!result.blank? ? (result.count > 1 ? list(set: result, order: order, reverse: reverse, web: web) : view_movie(result[0][0])) : print("No se encontró ningún resultado.")
else
print("ERROR: El modo %s no es válido. Los modos válidos son \"intersection\" y \"union\"." % [mode])
end
rescue
log("ERROR", "Filtrando la base de datos.")
end
def awards_movie(movie)
table = Text::Table.new
titulo = movie[:title].truncate(40).upcase + " ("
titulo << [movie[:original_title].truncate(40), movie[:year], movie[:directors].join(", ").truncate(40)].reject{ |s| s.blank? }.join(", ") + ")"
table.rows << [{align: :center, colspan: 2, value: titulo}]
movie[:prizes].select{ |k, v| !v.blank? }.each{ |prize, prizes|
table.rows << :separator
table.rows << [{colspan: 2, value: prize.to_s.titlecase.upcase, align: :center}]
table.rows << :separator
prizes.each{ |outcome, list|
table.rows << [{colspan: 2, align: :center, value: outcome.to_s.capitalize + " (" + list.count.to_s + ")"}]
list.each{ |award, winner|
table.rows << [award.to_s.truncate(60), winner.to_s.truncate(40)]
}
}
}
print(table)
end
def awards(movie)
if movie.is_a?(Integer)
if movie > -1 && movie < $movies.count
awards_movie($movies[movie])
else
print("ERROR: ID incorrecta.")
end
else
if movie.is_a?(String)
movie = $movies.each_with_index.select{ |m, i| m[:title] =~ /#{movie}/i }
!movie.blank? ? (movie.count > 1 ? list(set: movie) : awards_movie(movie[0])) : print("No se encontró ningún resultado.")
else
raise
end
end
rescue
log("ERROR", "Buscando premios de una película en la base de datos.")
end
# TODO: Añadir mas, quiza elaborar graficas
def stats
amount = $movies.size
propias = $movies.select{ |m| !m[:dates][:owned].blank? }.size
vistas = $movies.select{ |m| !m[:dates][:viewed].blank? }.size
title = "PFDB Stats (%s películas, %s propias, %s vistas)" % [amount, propias, vistas]
rows = []
fields = {
"Duración" => $movies.map{ |m| m[:duration] }.reject{ |m| m.blank? || m == 0 },
"Rating IMDb" => $movies.map{ |m| m[:rating][:imdb] }.reject{ |m| m.blank? || m == 0 },
"Rating FA" => $movies.map{ |m| m[:rating][:filmaffinity] }.reject{ |m| m.blank? || m == 0 },
"Votos IMDb" => $movies.map{ |m| m[:votes][:imdb] }.reject{ |m| m.blank? || m == 0 },
"Votos FA" => $movies.map{ |m| m[:votes][:filmaffinity] }.reject{ |m| m.blank? || m == 0 },
"Presupuesto" => $movies.map{ |m| m[:budget] }.reject{ |m| m.blank? || m == 0 },
"Recaudacion" => $movies.map{ |m| m[:gross] }.reject{ |m| m.blank? || m == 0 }
}
uniq_fields = {
"Géneros" => [ $movies.map{ |m| m[:genres].size }, $movies.map{ |m| m[:genres] }.flatten.uniq ],
"Actores" => [ $movies.map{ |m| m[:cast].map{ |k, v| k }.size }, $movies.map{ |m| m[:cast].map{ |k, v| k } }.flatten.uniq ],
"Directores" => [ $movies.map{ |m| m[:directors].size }, $movies.map{ |m| m[:directors] }.flatten.uniq ],
"Guionistas" => [ $movies.map{ |m| m[:writers].size }, $movies.map{ |m| m[:writers] }.flatten.uniq ],
"Productores" => [ $movies.map{ |m| m[:producers].size }, $movies.map{ |m| m[:producers] }.flatten.uniq ],
"Compositores" => [ $movies.map{ |m| m[:composers].size }, $movies.map{ |m| m[:composers] }.flatten.uniq ],
"Cinematógrafos" => [ $movies.map{ |m| m[:cinematographers].size }, $movies.map{ |m| m[:cinematographers] }.flatten.uniq ],
"Editores" => [ $movies.map{ |m| m[:editors].size }, $movies.map{ |m| m[:editors] }.flatten.uniq ],
"Países" => [ $movies.map{ |m| m[:countries].size }, $movies.map{ |m| m[:countries] }.flatten.uniq ],
"Idiomas" => [ $movies.map{ |m| m[:languages].size }, $movies.map{ |m| m[:languages] }.flatten.uniq ]
}
rows << ["Campo", "Min", "Avg", "Max", "Total"]
rows << :separator
fields.each{ |k, v| rows << [ k, {alignment: :right, value: v.min}, {alignment: :right, value: (v.sum.to_f / v.size).round(3)}, {alignment: :right, value: v.max}, {alignment: :right, value: v.sum} ] }
rows << :separator
uniq_fields.each{ |k, v| rows << [ k, {alignment: :right, value: v[0].min}, {alignment: :right, value: (v[0].sum.to_f / v[0].size).round(3)}, {alignment: :right, value: v[0].max}, {alignment: :right, value: v[1].size} ] }
genre_list = $movies.map{ |m| m[:genres].uniq }.flatten
genres = $movies.map{ |m| m[:genres].uniq }.flatten.uniq.map{ |g| [ g, genre_list.count(g) ] }.sort_by{ |s| -s[1] }
country_list = $movies.map{ |m| m[:countries].uniq }.flatten
countries = $movies.map{ |m| m[:countries].uniq }.flatten.uniq.map{ |g| [ g, country_list.count(g) ] }.sort_by{ |s| -s[1] }
rows << :separator
rows << [ "", {colspan: 2, alignment: :center, value: "Desglose de géneros"}, {colspan: 2, alignment: :center, value: "Desglose de paises"} ]
rows << :separator
(1..[genres.size, countries.size].max).each{ |i|
rows << [
"",
i <= genres.size ? genres[i-1][0].to_s : "",
i <= genres.size ? { alignment: :right, value: "%i (%05.2f%%)" % [ genres[i-1][1], 100.0 * genres[i-1][1] / amount ] } : "",
i <= countries.size ? countries[i-1][0].to_s : "",
i <= countries.size ? { alignment: :right, value: "%i (%05.2f%%)" % [ countries[i-1][1], 100.0 * countries[i-1][1] / amount ] } : "",
]
}
table = Terminal::Table.new(title: title, rows: rows)
print(table)
print("\n")
end
# TODO: Reelaborar todo esto del comment.
def add_comment(index, *reasons) # TODO: Terminar y testear este metodo, añadir mas razones
movie = $movies[index]
reasons.each{ |reason|
if reason.is_a?(String)
case reason
when reason =~ /premi/i || reason =~ /oscar/i then movie[:personal_comment] << :premiada
when reason =~ /relevancia/i || reason =~ /hist.ri/i || reason =~ /cultura/i then movie[:personal_comment] << :relevante
when reason =~ /personal/i then movie[:personal_comment] << :personal
else movie[:personal_comment] << reason end
else
print("ERROR: Cada razón debe ser una cadena de texto.")
end
}
end
def comment(index) # TODO: Elaborar este metodo
end
def startup
log("", "----------------------------------------------------------", false)
print("\n")
print(format_time + "x============================x===========================x\n")
print(format_time + "| PFDB v1.0 |\n")
print(format_time + "x============================x===========================x\n")
log("INFO", "Programa ejecutado.")
access
autobackup
autoclean
print(format_time + "x============================x===========================x\n")
end
$movies = []
$search = []
$searches = {}
$names = []
$films = []
startup