]> git.plutz.net Git - serve0/blob - pages/list.sh
full page list for pagination, playctl in listview
[serve0] / pages / list.sh
1 #!/bin/zsh
2
3 # Copyright 2014 - 2016 Paul Hänsch
4 #
5 # This file is part of Serve0.
6
7 # Serve0 is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # Serve0 is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with Serve0.  If not, see <http://www.gnu.org/licenses/>. 
19
20 . $_EXEC/pages/common.sh
21
22 LF='
23 '
24 filters="${_GET[f]}"
25 debug "FILTERS: $filters"
26
27 present_filter(){
28   printf %s\\n "$*" \
29   | sed -r 's;(^|!|\^|\|)([^!\^\|:]+:);\1;g;s;\^;\n;g' \
30   | sort -r \
31   | sed -r '$q;s;$; \&amp\; ;g'
32 }
33
34 if [ -n "$search" ]; then
35   TITLE="$search by $order"
36 elif [ -n $filter ]; then
37   TITLE="$(present_filter "$filter") by $order"
38 else
39   TITLE="List by $order"
40 fi
41
42 quicklinks(){
43   output=''
44   tac ${_DATA}/meta/recent \
45   | while [ "$(printf %s "$output" |wc -l)" -lt 10 ] && read line; do
46     printf %s "$output" |grep -qF "$line" || output="$output$line$LF"
47   done 
48   printf %s "$output" |while read line; do
49     linef=$(printf %s "$line" |sed -r 's;^.*(\?|&)f=([^&]+)&?.*$;\2;g')
50     lineo=$(printf %s "$line" |sed -r 's;^.*(\?|&)o=([^&]+)&?.*$;\2;g')
51     printf %s "<a href="$line">by ${lineo}: $(present_filter "$linef")</a>" 
52   done
53 }
54
55 category_selected(){
56   cat="${1}:"
57   filter="$2"
58   [ "$cat" = "none:" ] && cat=''
59
60   printf %s "$taglist" \
61   | sed -r 's;^('"$filter"')$;1 &;;t;s;^;0 ;' \
62   | sed -rn 's;^(0|1) '"$cat"'([^:]+)$;\1 \2;p' \
63   | sed 's;^0 ;<option>;;s;^1 ;<option selected>;;s;$;</option>;'
64 }
65
66 _printVideo(){
67   info="$1"
68   cache="${_DATA}/cache/$info.cache"
69   meta="${_DATA}/meta/$info.meta"
70
71   if [ "$cache" -nt "$meta" ]; then
72     cat "$cache"
73   else
74     videofile="${_DATA}/videos/$info"
75     thumb="${_DATA}/thumbs/${info}.jpg"
76     [ -r "$thumb" ] || genthumb "$videofile" "$thumb"
77     [ -r "$meta"  ] || genmeta "$videofile" "$meta"
78
79     head -n1 "$meta" |read length width height filename
80     length=$(validate "$length" '[0-9]+' 0)
81     width=$(validate "$width" '[0-9]+' 0)
82     height=$(validate "$height" '[0-9]+' 0)
83
84     tags="$(sed -n 2p "$meta")"
85     video="$(urlsafe "videos/$info")"
86     thumb="$(urlsafe "thumbs/$info.jpg")"
87     linkinfo="$(urlsafe "$info")"
88     htmlinfo="$(htmlsafe "$info")"
89
90     [ "$(($length % 60))" -lt 10 ] && minutes="$(($length / 60)):0$(($length % 60))" \
91                                    || minutes="$(($length / 60)):$(($length % 60))"
92
93     tee "$cache" <<VIDEOend
94       <li class="thumb">
95         <a class="watchlink" name="${linkinfo}" href="?action=watch&i=${linkinfo}">
96           <img src="$thumb" alt="Preview not yet available">
97         </a>
98         <h2>${htmlinfo}</h2>
99         
100         <input type="checkbox" name="tagsel" value="${htmlinfo}">
101         <span class="info property">${minutes}min</span>
102         <span class="info property">${width}x${height}</span>
103         $(printf %s "$tags" |sed -r 's:\|*([^|]+)\|*: <span class="info tag">\1</span>:g')
104       </li>
105 VIDEOend
106   fi
107 }
108
109 genlist(){
110   case "$order" in
111     Date)   ls -c "${_DATA}"/videos/ |egrep -i "$file_pattern";;
112     Length) sed -sn 1p "${_DATA}"/meta/*.meta |sort -n |sed -r 's;^[0-9\t]+\t;;';;
113     Name)   printf '%s\n' "${_DATA}"/videos/* |sed -r 's;^.*/;;;';;
114   esac
115 }
116
117 selectionlist() {
118   cachebase="?o=${order}&s=${search//\//}&f=${filter//\//}"
119   cache="${_DATA}/cache/${cachebase}"
120   if [ "$cache" -nt ${_DATA}/videos -a "$cache" -nt ${_DATA}/meta ]; then
121     cat "$cache"
122   else
123     filterex="s;^([0-9]+\t){3}(.+)\n.*$;\2;p"
124     printf '%s\n' "$filter" |tr '^' '\n' \
125     | sed -r 's;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\\\&;g' \
126     | while read each; do
127       [ "${each:0:1}" = '!' ] && filterex="/^[^\n]+\n(.*\|)?(${each#?})(\|.*)?$/d;{${filterex}}" \
128                               || filterex="/^[^\n]+\n(.*\|)?(${each})(\|.*)?$/{${filterex}}"
129     done
130       
131     genlist \
132     | if [ -n "$search" ] ; then
133       debug "Applying search: ${search}"
134       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
135       | xargs -d '\n' egrep -sil "$search" \
136       | sed -r 's;^.*/;;;s;\.meta$;;'
137     elif [ -n "$filter" ]; then
138       debug "Applying filters: ${filter} ++ ${filterex}"
139       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
140       | xargs -d '\n' sed -srn ":a;N;2!ba;{${filterex}}"
141     else
142       debug "No search or filtering"
143       cat
144     fi \
145     | tee "$cache"
146   fi
147 }
148
149 thumblist() {
150   selectionlist \
151   | sed -n "$page,$(($page + $pagesize - 1))p" \
152   | while read line; do
153     _printVideo "$line"
154   done
155 }
156
157 pagecount="$(( $(selectionlist | wc -l) / $pagesize ))"