diff --git a/konata.sh b/konata.sh index 9ff6769..398ffd8 100755 --- a/konata.sh +++ b/konata.sh @@ -256,16 +256,18 @@ konachan(){ 4c(){ args tmp="/tmp/4chan" - json="$tmp/${args[0]}.json" + threads="$tmp/${args[0]}.json" [ ! -d $tmp ] && mkdir $tmp - [ $(find $json -mmin +60) ] && rm $json - [ ! -f $json ] && wget -O $json https://a.4cdn.org/${args[0]}/threads.json - thread=$(jq -r '.[].threads[].no' < $json | shuf | head -n1) - img=$(curl -s https://a.4cdn.org/${args[0]}/thread/$thread.json | jq -r '.posts[].tim' | shuf | head -n1) - url="https://i.4cdn.org/${args[0]}/$img" - # TODO: Try to determine from JSON output whether image is PNG or JPG. - sendpic "$url.jpg" - sendpic "$url.png" + [ $(find $threads -mmin +60) ] && rm $threads + [ ! -f $threads ] && wget -O $threads https://a.4cdn.org/${args[0]}/threads.json + thread=$(jq -r '.[].threads[].no' < $threads | shuf | head -n1) + posts=$(curl -s https://a.4cdn.org/${args[0]}/thread/$thread.json) + imgs=$(jq -r ".posts[] | select(.ext==\".jpg\"), select(.ext==\".png\")" <<< $posts) + no=$(jq -r ".no" <<< $imgs | shuf | head -n1) + img=$(jq -r "select(.no==$no) | .tim" <<< $imgs) + ext=$(jq -r "select(.no==$no) | .ext" <<< $imgs) + url="https://i.4cdn.org/${args[0]}/${img}${ext}" + sendpic "$url" "sauce" } lfy(){