~mhysterio/sintel-media/trunk

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
#!/bin/bash
# This file is part of Sintel.
#
# Sintel is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Sintel is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Sintel.  If not, see <https://www.gnu.org/licenses/>.
#
#
#Gestión de películas de sintel

declare -A PELICULA


PELICULA=( ["id"]="" ["titulo"]="" ["fichero"]="" ["torrent"]="" ["esserie"]="" ["esvisto"]="" ["esdescargado"]="" )

function peliculas.info() {
    echo "peliculas - Gestión de películas"
    echo
    echo "Contiene todo lo necesario para añadir, modificar y ver"
    echo "medios de películas."
    echo
    echo "Puesto que peliculas es un nombre largo, se ha optado por"
    echo "la palabra inglesa film para el prefijo de este módulo."
    echo
    echo "Registro:"
    echo
    echo "id | titulo | fichero | torrent | esserie | esvisto | esdescargado"
}

#@ film.add titulo fichero torrent
function film.add() {
    [[ $# != 3 ]] && ( die $(iargs 3 "título, ficheroy torrent") )
    
	local titulo="$1"
	local fichero="$2"
	local torrent="$3"

	$DEBUG && echo "> film.add" > /dev/tty
	db.executeUpdate "insert into medio (titulo, fichero, torrent, esserie, esvisto) values (\"$titulo\",\"$fichero\",$torrent,0,0)"
	$DEBUG && echo "< film.add" > /dev/tty
	echo "$ROWID"
}

function film.add.info() {
    echo "film.add - Añadir película (titulo fichero torrent)"
    echo
    echo -e "\tArgumentos: titulo, fichero, torrent"
    echo
    echo "Añade una película nueva a la base de datos."
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -n film.add:\"Indiana Jones y la última cruzada|/home/mhyst/Peliculas/IndianaJones3.avi|182\""
}

#@ film.add2 registro (Formato: titulo|fichero|torrent)
function film.add2() {
    [[ $# != 1 ]] && ( die $(iargs 1 "registro") )
	local datos="$1"
	film.parse "$datos"
	local titulo="${PELICULA[titulo]}"
	local fichero="${PELICULA[fichero]}"
	local torrent="${PELICULA[torrent]}"

	db.executeUpdate "insert into medio (titulo, fichero, torrent, esserie, esvisto) values (\"$titulo\",\"$fichero\",$torrent,0,0,0)"
	echo "$ROWID"
}

function film.add2.info() {
    echo "film.add2 - Añadir película (un solo argumento)"
    echo
    echo -e "\tArgumentos: registro"
    echo
    echo "Esta función es prácticamente idéntica a film.add. la direfencia"
    echo "estriba en que la primera requiere tres argumentos separados"
    echo "mientras que esta solo requiere uno que luego divide en los tres"
    echo "datos que le hacen falta: título, fichero y torrent."
    echo
    echo "Mirando los ejemplos se puede ver que las llamadas son casi idénticas"
    echo "a excepción de que film.add utiliza la opción -n y esta -f. Véase la"
    echo "documentación de las funciones callFuncion y callFuncion2 para saber"
    echo "más."
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f film.add2:\"North By Northwest|/home/mhyst/Peliculas/Nbnw.mp4|305\""
}

#@ film-getById id
function film.getById() {
    [[ $# != 1 ]] && ( die $(iargs 1 "id") )
	local id="$1"

	$DEBUG && echo "> film.getById" > /dev/tty
	local res=$(db.executeQuery "select * from medio where id = $id")
	$DEBUG && echo "< film.getById" > /dev/tty
	echo "$res"
}

function film.getById.info() {
    echo "film.getById - Obtener película por id"
    echo
    echo -e "\tArgumentos: id"
    echo
    echo "Esta función devuelve el registro de la película tal como fue"
    echo "almacenado en la base de datos. Cada campo separado por '|'."
    echo
    echo "id se corresponde con el id de la tabla medio donde se almacenan"
    echo "tanto películas como series, por lo que si se proporciona el id"
    echo "de una serie, se dará el registro de esta."
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f film.getById:871"
}

#@ film.getIdByTitulo titulo
function film.getIdByTitulo() {
    [[ $# != 1 ]] && ( die $(iargs 1 "título") )
	local titulo="$1"

	$DEBUG && echo "> film.getByTitulo" > /dev/tty
	local res=$(db.executeQuery "select id from medio where esserie=0 and titulo = \"$titulo\"")
	$DEBUG && echo "< film.getByTitulo" > /dev/tty
	echo "$res"
}

function film.getIdByTitulo.info() {
    echo "film.getIdByTitulo - Obtener id de película por título"
    echo
    echo -e "\tArgumentos: título"
    echo
    echo "Devolverá todos los id de las películas cuyo  título sea"
    echo "exactamente igual a la cadena proporcionada como argumento."
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f film.getIdByTitulo:\"El puente sobre el río Kwai\""
}

#@ film.getByTitulo titulo
function film.getByTitulo() {
    [[ $# != 1 ]] && ( die $(iargs 1 "título") )
	local titulo="$1"

	$DEBUG && echo "> film.getByTitulo" > /dev/tty
	local res=$(db.executeQuery "select * from medio where esserie=0 and titulo = \"$titulo\"")
	$DEBUG && echo "< film.getByTitulo" > /dev/tty
	echo "$res"
}

function film.getByTitulo.info() {
    echo "film.getByTitulo - Obtener registro por título."
    echo
    echo -e "\tArgumentos: ritulo"
    echo
    echo "Devuelve todos los registros de la tabla medio cuyo título"
    echo "sea igual al suministrado."
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f getByTitulo:Vértigo"
}

#@  film-getByFichero fichero
function film.getByFichero() {
    [[ $# != 1 ]] && ( die $(iargs 1 "fichero") )
	local fichero="$1"

	local res=$(db.executeQuery "select * from medio where esserie = 0 and fichero = \"$fichero\"")
	echo "$res"
}

function film.getByFichero.info() {
    echo "film.getByFichero - Película por nombre de fichero."
    echo
    echo -e "\tArgumentos: ruta de fichero"
    echo
    echo "Devuelve el registro completo de cada película cuyo fichero"
    echo "con ruta completa coincida con el suministrado."
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f film.getByFicheri:\"/home/mhyst/Peliculas/Nbnw.mjv\""
}

#@ film-find busqueda
function film.find() {
	local busqueda="$@"

	echo "Busqueda: $busqueda" > /dev/tty
	local res=$(db.executeQuery "select * from medio where esserie = 0 and titulo like \"%${busqueda}%\"")
	echo "$res"	
}

function film.find.info() {
    echo "film.find - Buscar películas"
    echo
    echo -e "\tArgumentos: cadena de búsqueda"
    echo
    echo "Devuelve el registro de todas las películas cuyo título"
    echo "contenga la cadena suministrada."
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f film.find:kwai"
}

#@ film-getId titulo (sinonimo de getIdByTitulo)
function film.getId() {
    [[ $# != 1 ]] && ( die $(iargs 1 "título") )
	local titulo="$1"

	local id=$(db.executeQuery "select id from medio where esserie = 0 and titulo = \"$titulo\"")
}

function film.getId.info() {
    echo "film.getId - Igual que getIdByTitulo"
    echo
    echo -e "\tArgumentos: titulo"
    echo
    echo "Devolverá todos los id de las películas cuyo  título sea"
    echo "exactamente igual a la cadena proporcionada como argumento."
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f film.getId:\"El puente sobre el río Kwai\""
}

#@ film.remove id
function film.remove() {
    [[ $# != 1 ]] && ( die $(iargs 1 "id") )
	local id="$1"

	db.executeUpdate "delete from medio where esserie = 0 and id = $id"
}

function film.remove.info() {
    echo "film.remove - Eliminar película por id"
    echo
    echo -e "\tArgumentos: id"
    echo
    echo "Elimina la película con el id suministrado."
    echo "El id es único, por lo que no se borrará más de una."
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f film.remove:871"
}

#@ film-updateFichero id fichero
function film.updateFichero() {
    [[ $# != 2 ]] && ( die $(iargs 2 "id y fichero") )

    
	local id="$1"
	local fichero="$2"

	db.executeUpdate "update medio set fichero=\"$fichero\" where id = $id"
}

function film.updateFichero.info() {
    echo "film.updateFichero - Actualizar fichero"
    echo
    echo -e "\tArgumentos: id, ruta fichero"
    echo
    echo "Actualiza el fichero del medio cuyo id se ha suministrado."
    echo "No distingue entre series y películas."
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -n film.updateFichero:\"2|/mnt/Peliculas/Nbnw.mp4\""
}

#@ film.updateTorrent id torrent
function film.updateTorrent() {
    [[ $# != 2 ]] && ( die $(iargs 2 "id y torrent") )
    
	local id="$1"
	local torrent="$2"

	db.executeUpdate "update medio set torrent=$torrent where id = $id"
}

function film.updateTorrent.info() {
    echo "film.updateTorrent - Actualizar torrent por id"
    echo
    echo -e "\tArgumentos: id, torrent"
    echo
    echo "Actualiza el torrent de la película cuyo id se ha suministrado."
    echo "Cabe notar que en este caso el torrent no es más que el id"
    echo "del torrent asignado por transmission. Con este dato, y la"
    echo "ayuda del módulo torrents, se puede obtener información del"
    echo "torrent en transmission. Esto permite saber, entre otras cosas"
    echo "si el torrent ya ha terminado de descargarse."
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -n film.updateTorrent:\"2|296\""
}

#@ film.setVista id
function film.setVista() {
    [[ $# != 1 ]] && ( die $(iargs 1 "id") )
	local id="$1"

	db.executeUpdate "update medio set esvisto=1 where id = $id"
}

function film.setVista.info() {
    echo "film.setVista - Establece una película como vista."
    echo
    echo -e "\tArgumentos: id"
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f film.setVista:2"
}

#@ film.setNoVista id
function film.setNoVista() {
    [[ $# != 1 ]] && ( die $(iargs 1 "id") )
	local id="$1"

	db.executeUpdate "update medio set esdescargado=0 where id = $id"
}

function film.setNoVista.info() {
    echo "film.setNoVista - Establece una película como no vista."
    echo
    echo -e "\tArgumentos: id"
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f film.setNoVista:2"
}

#@ film.setDescargada id
function film.setDescargada() {
    [[ $# != 1 ]] && ( die $(iargs 1 "id") )
	local id="$1"

	db.executeUpdate "update medio set esdescargado=1 where id = $id"
}

function film.setDescargada.info() {
    echo "film.setDescargada - Establece una película como descargada."
    echo
    echo -e "\tArgumentos: id"
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f film.setDescargada:1"
}

#@ film.SetNoDescargada id
function film.setNoDescargada() {
    [[ $# != 1 ]] && ( die $(iargs 1 "id") )
	local id="$1"

	db.executeUpdate "update medio set esvisto=0 where id = $id"
}

function film.setNoDescargada.info() {
    echo "film.setNoDescargada - Establece una película como no descargada."
    echo
    echo -e "\tArgumentos: id"
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f film.setNoDescargada:1"
}

#@ film.parse datos (Formato: (id|titulo|fichero|torrent|esserie|esvisto|esdescargado))
function film.parse() {
    [[ $# != 1 ]] && ( die $(iargs 1 "registro") )
	local datos="$1"

	IFS='|' read -a datosa <<< "$datos"; IFS=" "
	PELICULA[id]="${datosa[0]}"
	PELICULA[titulo]="${datosa[1]}"
	PELICULA[fichero]="${datosa[2]}"
	PELICULA[torrent]="${datosa[3]}"
	PELICULA[esserie]="${datosa[4]}"
	PELICULA[esvisto]="${datosa[5]}"
	PELICULA[esdescargado]="{datosa[6]}"
}

function film.parse.info() {
    echo "film.parse - Procesa registro de película."
    echo
    echo -e "\tArgumentos: registro"
    echo
    echo "Recibe un registro con el formato:"
    echo "campo1|campo2|campo3|etc"
    echo "y separa cada campo en una celda del array"
    echo "PELICULA."
}

#@ film.parseToFile datos
function film.parseToFile() {
    [[ $# != 1 ]] && ( die $(iargs 1 "registro") )
	local datos="$1"

	film.parse "$datos"

	echo > /tmp/PELICULA
	let i=0
	for key in ${!PELICULA[@]}; do
		echo "$key ${PELICULA[$key]}" >> /tmp/PELICULA
	done
}

function film.parseToFile.info() {
    echo "film.parseToFile - Procesa registro y lo copia al archivo /tmp/PELICULA"
    echo
    echo -e "\tArgumentos: registro"
    echo
    echo "Esta función permite compartir datos de una película con"
    echo "otros scripts o programas."
}

#@ film.getField field
function film.getField() {
    [[ $# != 1 ]] && ( die $(iargs 1 "campo") )
	local field="$1"

	let i=0
	for key in "${!PELICULA[@]}"; do
		if [[ "$key" == "$field" ]]; then
			$DEBUG && echo "Coincide con $key. Salimos" > /dev/tty
			break
		fi
		(( i++ ))
	done
	$DEBUG && echo "El valor de i es $i" > /dev/tty
	if [[ $i < ${#PELICULA[@]} ]]; then
		local res=$(grep "^$field" /tmp/PELICULA | awk '{ print $2 };')
		echo "$res"
	else
		echo "-1"
	fi
}

function film.getField.info() {
    echo "film.getField - Obtiene un campo de un registro"
    echo
    echo -e "\tArgumentos: nombre del campo"
    echo
    echo "Antes de poder llamar a esta función debe de haberse"
    echo "incovado la función film.parse."
    echo
    echo "Ejemplos:"
    echo -e "\tsintel -f film.getField:titulo"
}