]> git.plutz.net Git - serve0/blob - pages/list.sh
Page titles based on content, improved display of quicklinks
[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(){ printf %s "$*" |sed -r 's;(^|!|\^|\|)([^!\^\|:]+:);\1;g;s;\^; \&amp\; ;g' }
28
29 if [ -n "$search" ]; then
30   TITLE="$search by $order"
31 elif [ -n $filter ]; then
32   TITLE="$(present_filter "$filter") by $order"
33 else
34   TITLE="List by $order"
35 fi
36
37 quicklinks(){
38   output=''
39   tac ${_DATA}/meta/recent \
40   | while [ "$(printf %s "$output" |wc -l)" -lt 10 ] && read line; do
41     printf %s "$output" |grep -qF "$line" || output="$output$line$LF"
42   done 
43   printf %s "$output" |while read line; do
44     linef=$(printf %s "$line" |sed -r 's;^.*(\?|&)f=([^&]+)&?.*$;\2;g')
45     lineo=$(printf %s "$line" |sed -r 's;^.*(\?|&)o=([^&]+)&?.*$;\2;g')
46     printf %s "<a href="$line">by ${lineo}: $(present_filter "$linef")</a>" 
47   done
48 }
49
50 category_selected(){
51   cat="${1}:"
52   filter="$2"
53   [ "$cat" = "none:" ] && cat=''
54
55   printf %s "$taglist" \
56   | sed -r 's;^('"$filter"')$;1 &;;t;s;^;0 ;' \
57   | sed -rn 's;^(0|1) '"$cat"'([^:]+)$;\1 \2;p' \
58   | sed 's;^0 ;<option>;;s;^1 ;<option selected>;;s;$;</option>;'
59 }
60
61 _printVideo(){
62   info="$1"
63   cache="${_DATA}/cache/$info.cache"
64   meta="${_DATA}/meta/$info.meta"
65
66   if [ "$cache" -nt "$meta" ]; then
67     cat "$cache"
68   else
69     videofile="${_DATA}/videos/$info"
70     thumb="${_DATA}/thumbs/${info}.jpg"
71     [ -r "$thumb" ] || genthumb "$videofile" "$thumb"
72     [ -r "$meta"  ] || genmeta "$videofile" "$meta"
73
74     head -n1 "$meta" |read length width height filename
75     length=$(validate "$length" '[0-9]+' 0)
76     width=$(validate "$width" '[0-9]+' 0)
77     height=$(validate "$height" '[0-9]+' 0)
78
79     tags="$(sed -n 2p "$meta")"
80     video="$(urlsafe "videos/$info")"
81     thumb="$(urlsafe "thumbs/$info.jpg")"
82     linkinfo="$(urlsafe "$info")"
83     htmlinfo="$(htmlsafe "$info")"
84
85     [ "$(($length % 60))" -lt 10 ] && minutes="$(($length / 60)):0$(($length % 60))" \
86                                    || minutes="$(($length / 60)):$(($length % 60))"
87
88     tee "$cache" <<VIDEOend
89       <li class="thumb">
90         <a class="watchlink" name="${linkinfo}" href="?action=watch&i=${linkinfo}">
91           <img src="$thumb" alt="Preview not yet available">
92         </a>
93         <h2>${htmlinfo}</h2>
94         
95         <input type="checkbox" name="tagsel" value="${htmlinfo}">
96         <span class="info property">${minutes}min</span>
97         <span class="info property">${width}x${height}</span>
98         $(printf %s "$tags" |sed -r 's:\|*([^|]+)\|*: <span class="info tag">\1</span>:g')
99       </li>
100 VIDEOend
101   fi
102 }
103
104 genlist(){
105   case "$order" in
106     Date)   ls -c "${_DATA}"/videos/ |egrep -i "$file_pattern";;
107     Length) sed -sn 1p "${_DATA}"/meta/*.meta |sort -n |sed -r 's;^[0-9\t]+\t;;';;
108     Name)   printf '%s\n' "${_DATA}"/videos/* |sed -r 's;^.*/;;;';;
109   esac
110 }
111
112 thumblist() {
113   cache="${_DATA}/cache/${pagesize}?o=${order}&s=${search}&f=${filter}&pn=${page}"
114   if [ "$cache" -nt ${_DATA}/videos -a "$cache" -nt ${_DATA}/meta ]; then
115     cat "$cache"
116   else
117     filterex="s;^([0-9]+\t){3}(.+)\n.*$;\2;p"
118     printf '%s\n' "$filter" |tr '^' '\n' \
119     | while read each; do
120       [ "${each:0:1}" = '!' ] && filterex="/^[^\n]+\n(.*\|)?(${each#?})(\|.*)?$/d;{${filterex}}" \
121                               || filterex="/^[^\n]+\n(.*\|)?(${each})(\|.*)?$/{${filterex}}"
122     done
123       
124     genlist \
125     | if [ -n "$search" ] ; then
126       debug "Applying search: ${search}"
127       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
128       | xargs -d '\n' egrep -sil "$search" \
129       | sed -r 's;^.*/;;;s;\.meta$;;'
130     elif [ -n "$filter" ]; then
131       debug "Applying filters: ${filter} ++ ${filterex}"
132       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
133       | xargs -d '\n' sed -srn ":a;N;2!ba;{${filterex}}"
134     else
135       debug "No search or filtering"
136       cat
137     fi \
138     | sed -n "$page,$(($page + $pagesize - 1))p" \
139     | tee "$cache"
140   fi \
141   | while read line; do
142     _printVideo "$line"
143   done
144 }