-
Notifications
You must be signed in to change notification settings - Fork 0
/
movieinfo.sh
executable file
·198 lines (179 loc) · 6.73 KB
/
movieinfo.sh
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
#!/bin/bash
img_viewer=chafa # default
# Text formatting
BOLD='\e[1m'
RED='\e[1;31m'
BLUE='\e[1;34m'
YLLW='\e[1;33m'
NRM='\e[0m' # normal
# Check args
no_img=false
for arg in "$@"; do
case $arg in
--catimg)
img_viewer=catimg
;;
--asciiart)
img_viewer=asciiart
;;
--no-image)
img_viewer=""
no_img=true
;;
*)
printf "${BOLD}Usage${NRM}: movieinfo [flags]\n\n${BOLD}flags${NRM}:\n\
--catimg: image using catimg\n\
--asciiart: ASCII art image using asciiart\n\
--no-image: no image\n"
exit
;;
esac
done
# Warn if image viewer is unavailable
if [ "$no_img" = false ]; then
if [ "$img_viewer" = "chafa" ] && [ -z "$(command -v chafa)" ]; then
printf "${YLLW}WARNING${NRM}: chafa is not installed.\n"
img_viewer=""
no_img=true
elif [ "$img_viewer" = "catimg" ] && [ -z "$(command -v catimg)" ]; then
printf "${YLLW}WARNING${NRM}: catimg is not installed.\n"
img_viewer=""
no_img=true
elif [ "$img_viewer" = "asciiart" ] && [ -z "$(command -v asciiart)" ]; then
printf "${YLLW}WARNING${NRM}: asciiart is not installed.\n"
img_viewer=""
no_img=true
fi
fi
# Read movie and search
printf "${BOLD}Search${NRM}: "
read movie
movie=$(echo $movie | sed -r 's/ /%20/g')
content=$(wget https://www.rottentomatoes.com/search?search=$movie -qO -)
readarray movies -t <<<$(echo $content | grep -oP \
'(?<=<search-page-media-row).*?(?=</search-page-media-row>)')
readarray titleList -t <<<$(echo ${movies[@]} | grep -oP \
'(?<=slot="title"> ).*?(?= </a>)' | sed "s/'/'/g" | sed 's/&/\&/g')
readarray yearList -t <<<$(echo ${movies[@]} | grep -oP \
'(?<=releaseyear=").*?(?=")')
readarray linkList -t <<<$(echo ${movies[@]} | grep -oP \
'(?<= </a> <a href=").*?(?=" class="unset" data-qa="info-name" slot="title">)')
# Movie choice dialog
if [ ${#titleList[$i]} != 1 ]; then
printf "${BOLD}Choose movie${NRM}:\n"
else
printf "${BOLD}No results.${NRM}\n"
exit
fi
moviesNum=${#titleList[@]}
for i in $(seq 0 $((moviesNum < 8 ? moviesNum - 1 : 7))); do
printf " ${BLUE}$i${NRM}. ${titleList[$i]} (${yearList[$i]:=-})" | tr -d '\n'
printf '\n'
done
# Read and check choice
chosen=false
i=$((moviesNum < 8 ? moviesNum - 1 : 7))
while [ "$chosen" = false ]; do
printf "${BOLD}Choice (${BLUE}0${NRM}${BOLD})${NRM}: "
read choice
if [ -z $choice ]; then # default choice
choice='0'
chosen=true
elif [ "$choice" = 'e' ]; then
exit
elif [ "$choice" = 'm' ]; then # more movies
((i++))
if [ ${#titleList[$i]} != 0 ]; then
printf "${BOLD}Printing more...${NRM}\n"
while [ ${#titleList[$i]} != 0 ]; do
printf " ${BLUE}$i${NRM}. ${titleList[$i]} " | tr -d '\n'
printf "(${yearList[$i]:=-})" | tr -d '\n'
printf '\n'
((i++))
done
else
printf "No more results.\n"
fi
((i--))
elif [[ $choice =~ ^[0-9]+$ ]] && [ $choice -le $i ]; then
chosen=true
else
printf "${RED}ERROR${NRM}: Invalid choice. (Type '${BOLD}m${NRM}' "
printf "for more results or '${BOLD}e${NRM}' to exit.)\n"
fi
done
# Retrieve chosen movie info
content=$(wget ${linkList[$choice]} -qO -)
if [ "$no_img" = false ]; then
img=$(echo $content | grep -oP '(?<=<meta property="og:image" content=").*?(?=")' | head -1)
wget -q $img -O /tmp/img.jpg
fi
description=$(echo $content | grep -oP '(?<=<meta name="description" content=").*?(?=")' |
sed 's/"/"/g' | sed "s/'/'/g" | sed 's/&/\&/g' | head -1)
language=$(echo $content | grep -oP \
'(?<=Language</rt-text> </dt> <dd data-qa="item-value-group"> <rt-text data-qa="item-value">).*?(?=</rt-text>)')
director=$(echo $content | grep -oP '(?<="director":\[{"@type":"Person","name":").*?(?=")')
runtime=$(echo $content | grep -oP \
'(?<=Runtime</rt-text> </dt> <dd data-qa="item-value-group"> <rt-text data-qa="item-value">).*?(?=</rt-text>)')
genre=$(echo $content | grep -oP '(?<="metadataGenres":\[").*?(?=")' | head -1)
tomatometer=$(echo $content | grep -oP '(?<=s","scorePercent":").*?(?=%","title":"Tomatometer")' | head -1)
popcornmeter=$(echo $content | grep -oP '(?<="scorePercent":").*?(?=%","title":"Popcornmeter")' | head -1)
# Print chosen movie info
printf "\n${BOLD}Visit${NRM}: ${linkList[choice]}\n"
termwidth=$(tput cols) # terminal width
termheight=$(tput lines) # terminal height
asciiwidth=$((27 * termwidth / 100))
txtwidth=$((6 * termwidth / 10))
if [ "$img_viewer" = "chafa" ]; then
chafa -s "$((termwidth))x$((termheight / 3))" /tmp/img.jpg
printf "\n"
elif [ "$img_viewer" = "catimg" ]; then
catimg -r 2 -w $((2 * $asciiwidth)) /tmp/img.jpg &>>/tmp/img0
if [ "$(cat /tmp/img0 | grep error)" ]; then
# In case of catimg error no image
no_img=true
fi
sed -i '$d' /tmp/img0 # remove last line
# In case title overflows to 2nd line
paste -d '' /tmp/img0 <(printf "\n${BOLD}") >/tmp/img
elif [ "$img_viewer" = "asciiart" ]; then
script -q -c "asciiart -c -i -w $asciiwidth /tmp/img.jpg" -O /dev/null >>/tmp/img0
if [ "$(cat /tmp/img0 | grep asciiart)" ]; then
# In case of asciiart error no image
no_img=true
fi
sed -i 's/\r//g' /tmp/img0 # dos to unix
# In case title overflows to 2nd line
paste -d '' /tmp/img0 <(printf "\n${BOLD}") >/tmp/img
fi
if [ "$no_img" = true ] && [ -n "$img_viewer" ]; then
printf "${RED}ERROR${NRM}: could not process movie image.\n"
fi
printf "${BOLD}${titleList[$choice]} (${yearList[$choice]:=-})${NRM}" | tr -d '\n' >/tmp/mvinfo
printf "\n${NRM}${description:=-}\n" >>/tmp/mvinfo
printf "\n${BOLD}Language${NRM}: ${language:=-}\n" >>/tmp/mvinfo
printf "${BOLD}Director${NRM}: ${director:=-}\n" >>/tmp/mvinfo
printf "${BOLD}Runtime${NRM}: ${runtime:=-}\n" >>/tmp/mvinfo
printf "${BOLD}Genre${NRM}: ${genre:=-}\n\n" >>/tmp/mvinfo
meter=$([ -n "$tomatometer" ] && echo "$tomatometer%%" || echo "-")
printf "${RED}Tomatometer${NRM}: ${meter}\n" >>/tmp/mvinfo
meter=$([ -n "$popcornmeter" ] && echo "$popcornmeter%%" || echo "-")
printf "${BOLD}Popcornmeter${NRM}: ${meter}\n" >>/tmp/mvinfo
fold -s -w $txtwidth /tmp/mvinfo >/tmp/mvinfostd
if [ "$img_viewer" = "chafa" ] || [ "$no_img" = true ]; then
cp /tmp/mvinfostd /tmp/output
else
sed -e 's/$/ /' -i /tmp/img
linesart=$(cat /tmp/img | wc -l)
linestxt=$(cat /tmp/mvinfostd | wc -l)
i=$((linestxt - linesart))
while ((i > 0)); do
printf "%*s \n" $asciiwidth ' ' >>/tmp/img
((i--))
done
paste -d '' /tmp/img /tmp/mvinfostd >/tmp/output
fi
cat /tmp/output
# Clear temporary files
rm -f /tmp/img.jpg /tmp/img /tmp/img0
rm /tmp/mvinfo /tmp/mvinfostd /tmp/output