]> CyberLeo.Net >> Repos - CDN/taggery.git/blob - fetch
Remove lolibooru: site is gone
[CDN/taggery.git] / fetch
1 #!/bin/sh -e
2
3 pebkac() {
4   [ "${*}" ] && printf "%s\n\n" "${*}"
5   cat <<EOF
6 Usage: $(basename "${0}") <id>
7
8 Given an ID number from the site:
9 * downloads and stashes the full-size image in the md5 directory
10 * adds whatever tags are found into the tag area
11
12   -p <poolname>  Associates the downloaded image with a pool
13   -i <indexnum>  Used with -p to indicate the position in the pool
14
15   -t             Refresh the tags, not the picture
16   -f <filename>  If the image is deleted, provide the local filename
17 EOF
18   kill $$
19   exit 1
20 }
21
22 while getopts p:i:tf: OPT
23 do
24   case "${OPT}" in
25   p)  pool="${OPTARG}" ;;
26   i)  indx="${OPTARG}" ;;
27   t)  tags=TAGS        ;;
28   f)  hash="${OPTARG}" ;;
29   ?)  pebkac           ;;
30   esac
31 done
32 shift $(( $OPTIND - 1 ))
33
34 [ "${*}" ] || pebkac
35 id="${1}"
36 [ -z "$(echo "${id}" | tr -d '[0-9]')" ] || pebkac "ID should be an integer"
37
38 taggery_name="$(basename "${0}")"
39 taggery_profile="$(dirname "${0}")/lib/profiles/${taggery_name}"
40 [ -f "${taggery_profile}" ] || pebkac "Unsupported profile: ${taggery_name}"
41 . "${taggery_profile}"
42
43 # Set up temporary area
44 mkdir -p "$(dirname "${0}")/tmp"
45 TMPDIR="$(dirname "${0}")/tmp"
46
47 # Load KVS
48 kvs="${TMPDIR}/.${taggery_name}.kvdb"
49 . "$(dirname "${0}")/lib/kvs.sh"
50
51 # Load Taggery
52 . "$(dirname "${0}")/lib/taggery.sh"
53 image_url() { taggery_image_url "${@}"; }
54 image_tags() { taggery_image_tags "${@}"; }
55
56 cd "$(dirname "${0}")"
57
58 file="$(image_url "${id}")"
59 tags="$(image_tags "${id}")"
60
61 [ "${file}" -o "${hash}" ] || { echo "No download URL (deleted?) and -f not provided" >&2; kill -ABRT $$; exit 1; }
62 [ "${tags}" ] || { echo "No tags found" >&2; kill -ABRT $$; exit 1; }
63
64 if [ "${file}" ]
65 then
66   name="$(basename "${file}")"
67 else
68   name="${hash}"
69 fi
70 extn="${name#*.}"
71
72 unset nabbed
73 if [ "${file}" ]
74 then
75   if [ -e "md5/${name}" ]
76   then
77     echo "Filename md5/${name} already exists"
78   else
79     referer="$(taggery_image_referer "${id}")"
80     wget ${referer:+--referer="${referer}"} -O "md5/${name}" "${file}"
81     nabbed=NABBED
82   fi
83 fi
84
85 if [ "${nabbed}" -o "${tags}" ]
86 then
87   for tag in ${tags}
88   do
89     echo ${tag}
90     tag="$(echo "${tag}" | sed -e 's/\//%47/g; s/[\]/\\&/g')"
91     mkdir -p "tag/${tag}"
92     ln -sf "../../md5/${name}" "tag/${tag}/"
93   done
94 fi
95
96 # Even if we didn't download it, try to link it into the pool
97 [ "${pool}" ] && {
98   plnm="pool/${pool}/${indx}.${extn}"
99   if [ -e "${plnm}" ]
100   then
101     echo "Filename ${plnm} already exists"
102   else
103     mkdir -p "pool/${pool}"
104     ln -svf "../../md5/${name}" "${plnm}"
105   fi
106 }