~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
#!/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 anime
#
#Básicamente queremos utilizar la base de datos de momento como 
# elemento adicional de dxtotal y mt o mtf y quizá 
# en el futuro, se use con exclusividad, sustituyendo al método 
# de la carpeta de anime.
#
#Se deben poder añadir anime a seguir y ANIMEs conforme se vayan descargando.
#Así mismo servirá para llevar la cuenta de los vistos en "ver".

declare -A ANIME
ANIME=( ["id"]=""  ["titulo"]="" ["url"]="" ["alias"]="" ["episodio"]="" )

function anime.info() {
    echo "anime - Módulo para seguimiento de anime"
    echo
    echo "Maneja anime que se ve online."
}

function anime.add() {
    [[ $# != 2 ]] && ( die $(iargs 2 "título y url") )
    
    local titulo="$1"
    local url="$2"

    $DEBUG && echo "> anime.add" > /dev/tty
    db.executeUpdate "insert into anime (titulo, url, alias, episodio) values (\"$titulo\",\"$url\",\"\",1)"	
    $DEBUG && echo "< anime.add" > /dev/tty
    echo "$ROWID"
}

function anime.add.info() {
    echo "anime.add - Añade una serie anime a la base de datos"
    echo
    echo -e "\tArgumentos: titulo, url"
    echo -e "\tDevuelve el id del anime en la base de datos"
    echo
    echo "La URL debe indicarse sin la parte http:// o https://"
}

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

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

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

    $DEBUG && echo "> anime.getByTitulo" > /dev/tty
    local res=$(db.executeQuery "select id from anime where lower(titulo) = \"${titulo,,}\"")
    $DEBUG && echo "< anime.getByTitulo" > /dev/tty
    echo "$res"
}

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

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

function anime.find() {
    local busqueda="$@"

    local res=$(db.executeQuery "select * from anime where titulo like \"%${busqueda}%\"")
    echo "$res"	
}

function anime.list() {
    $DEBUG && echo "> anime.getById" > /dev/tty
    local res=$(db.executeQuery "select * from anime")
    $DEBUG && echo "< anime.getById" > /dev/tty
    echo "$res"
}

function anime.delete() {
    [[ $# != 1 ]] && ( die $(iargs 1 "id") )
    local id="$1"

    $DEBUG && echo "> anime.delete" > /dev/tty
    $(db.executeUpdate "delete from anime where id = $id")
    $DEBUG && echo "< anime.delete" > /dev/tty
    echo "$ROWID"
}

function anime.setAlias() {
    
    [[ $# != 2 ]] && ( die $(iargs 2 "id y alias") )
    local id="$1"
    local alias="$2"

    $DEBUG && echo "> anime.setAlias" > /dev/tty
    $(db.executeUpdate "update anime set alias=\"$alias\" where id = $id")
    $DEBUG && echo "< anime.setAlias" > /dev/tty
    echo "$ROWID"	
}

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

    local id=$(db.executeQuery "select id from anime where titulo = \"$titulo\"")
    echo $id
}

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

    db.executeUpdate "delete from anime where id = $id"
}

function anime.updateTitulo() {
    
    [[ $# != 2 ]] && ( die $(iargs 2 "id y título") )
    local id="$1"
    local titulo="$2"

    db.executeUpdate "update anime set titulo=\"$titulo\" where id = $id"
}

function anime.updateURL() {
    
    [[ $# != 2 ]] && ( die $(iargs 2 "id y url") )
    local id="$1"
    local url="$2"

    db.executeUpdate "update anime set url=\"$url\" where id = $id"
}

function anime.updateEpisodio() {
    
    [[ $# != 2 ]] && ( die $(iargs 2 "id y episodio") )
    local id="$1"
    local episodio="$2"

    db.executeUpdate "update anime set episodio=$episodio where id = $id"
}

function anime.next() {
    [[ $# != 1 ]] && ( die $(iargs 1 "id") )
    local id="$1"

    db.executeUpdate "update anime set episodio=episodio+1 where id = $id"
}

function anime.previous() {
    [[ $# != 1 ]] && ( die $(iargs 1 "id") )
    local id="$1"

    db.executeUpdate "update anime set episodio=episodio-1 where id = $id"
}

function anime.parse() {
    [[ $# != 1 ]] && ( die $(iargs 1 "registro (datos)") )
    local datos="$1"

    $DEBUG && echo ">Entra en anime.parse" > /dev/tty
    IFS='|' read -a datosa <<< "$datos"; IFS=" "
    ANIME[id]="${datosa[0]}"
    ANIME[titulo]="${datosa[1]}"
    ANIME[url]="${datosa[2]}"
    ANIME[alias]="${datosa[3]}"
    ANIME[episodio]="${datosa[4]}"
    #$DEBUG && echo "<Sale en anime.parse" > /dev/tty
}

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

    anime.parse "$datos"

    echo > /tmp/ANIME
    for key in ${!ANIME[@]}; do
	echo "$key ${ANIME[$key]}" >> /tmp/ANIME
    done
}

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

    let i=0
    for key in "${!ANIME[@]}"; 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 < ${#ANIME[@]} ]]; then
	local res=$(grep "^$field" /tmp/ANIME | awk '{ print $2 };')
	echo "$res"
    else
	echo "-1"
    fi
}

function anime.screenAnime() {
    echo "${ANIME[id]} ${ANIME[titulo]} ${ANIME[url]} ${ANIME[alias]} ${ANIME[episodio]}" > /dev/tty
}

function anime.watch() {
    [[ $# != 1 ]] && ( die $(iargs 1 "id") )
    local id="$1"

    $DEBUG && echo "> anime.watch" > /dev/tty
    local res=$(db.executeQuery "select * from anime where id = $id")
    anime.parse "$res"

    anime.screenAnime

    x-www-browser http://${ANIME[url]}$(trim ${ANIME[episodio]})
    anime.next ${ANIME[id]}

    $DEBUG && echo "< anime.watch" > /dev/tty
}