#!/bin/bash
basepath1='/zc/vid_4mb'
# \/z4\/sync_mnt-e\/dl\/13b48ab\/test\/ -> '\/z9\/4chan.org\/gif\/test\/' -> \/zc\/vid_4mb\/


if [ $(expr $(date +%s) - $(cat $basepath1/time.txt)) -gt 86400 ]; then


cat $basepath1/torun.txt | xargs -d "\n" sh -c 'for args do sh -c "$args"; done' _ 2>> $basepath1/cronlog2.txt
rm $basepath1/torun.txt

now=$(date +%s); echo $now > $basepath1/time.txt; curl -sL https://a.4cdn.org/gif/catalog.json > $basepath1/threads/catalog.json.$now; cat $basepath1/threads/catalog.json.$now | jq . | grep "^\s\s\s\s\s\s\s\s\"no\": " | sed "s/.* //g" | sed "s/,.*//g" | grep -v "^6226033$" > $basepath1/chkcmd.txt

threadcount=$(wc -l $basepath1/chkcmd.txt | sed "s/ .*//g")

i=1; while [ $i -lt $(expr $threadcount + 1) ]; do

 ii=$(tail -n+$i $basepath1/chkcmd.txt | head -n1)

 echo $i; echo $ii

 curl -sL https://a.4cdn.org/gif/thread/$ii.json > $basepath1/threads/$ii.json.$now

 imgcount=$(expr $(cat $basepath1/threads/$ii.json.$now | jq ".posts[].ext, .posts[].tim, .posts[].md5" | grep -v "^null$" | wc -l) / 3)

 filename=($(cat $basepath1/threads/$ii.json.$now | jq ".posts[].ext, .posts[].tim, .posts[].md5" | grep -v "^null$" | tail -n+$(expr $imgcount + 1) | head -n$imgcount))

 ext=($(cat $basepath1/threads/$ii.json.$now | jq ".posts[].ext, .posts[].tim, .posts[].md5" | grep -v "^null$" | head -n$imgcount | sed "s/\"//g"))

 md5=($(cat $basepath1/threads/$ii.json.$now | jq ".posts[].ext, .posts[].tim, .posts[].md5" | grep -v "^null$" | tail -n$imgcount | sed "s/\"\|=//g" | sed "s/\//_/g" | sed "s/+/-/g"))

 n=0; while [ $n -lt $imgcount ]; do echo TZ=UTC wget -nc https://i.4cdn.org/gif/${filename[$n]}${ext[$n]} -O $basepath1/test/${md5[$n]}; n=$(expr $n + 1); done >> $basepath1/torun.txt

 i=$(expr $i + 1)

done


fi
