]> CyberLeo.Net >> Repos - CDN/taggery.git/blob - fetch
Gelbooru is finally switching to https, yay!
[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 a post URL 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 url="${1}"
36 #[ -z "$(echo "${id}" | tr -d '[0-9]')" ] || pebkac "ID should be an integer"
37
38 taggery_base="$(dirname "${0}")"
39 taggery_libs="$(dirname "${0}")/lib"
40
41 # Try and find the profile that can handle the url
42 profiles="$(cd "${taggery_libs}/profiles"; ls -1)"
43 for profile in ${profiles}
44 do
45   . "${taggery_libs}/profiles/${profile}"
46   "${profile}_can_handle" "${url}" || continue
47   taggery_name="${profile}"
48   break
49 done
50
51 # Set up temporary area
52 #mkdir -p "$(dirname "${0}")/tmp"
53 #TMPDIR="$(dirname "${0}")/tmp"
54
55 # Load KVS
56 #kvs="${TMPDIR}/.${taggery_name}.kvdb"
57 #. "$(dirname "${0}")/lib/kvs.sh"
58
59 # Load Taggery
60 . "${taggery_libs}/taggery.sh"
61
62 id="$(taggery_id "${url}")"
63 name="$(taggery_image_name "${id}")"
64 file="$(taggery_image_url "${id}")"
65 tags="$(taggery_image_tags "${id}")"
66 [ "${file}" ] || name="${hash}"
67 extn="${name#*.}"
68
69 [ "${DEBUG}" ] && {
70   echo "id: ${id}"
71   echo "name: ${name}"
72   echo "file: ${file}"
73   echo "tags: ${tags}"
74   echo ""
75   kvs_get_all "${taggery_name}/${id}"
76   kill -KILL $$
77 }
78
79 [ "${file}" -o "${hash}" ] || { echo "No download URL (deleted?) and -f not provided" >&2; kill -ABRT $$; exit 1; }
80 [ "${tags}" ] || { echo "No tags found" >&2; kill -ABRT $$; exit 1; }
81
82 unset nabbed
83 if [ "${file}" ]
84 then
85   if [ -e "${taggery_base}/md5/${name}" ]
86   then
87     echo "Filename ${name} already exists"
88   else
89     taggery_fetch_image "${id}"
90     nabbed=NABBED
91   fi
92 fi
93
94 if [ "${nabbed}" -o "${tags}" ]
95 then
96   taggery_link_tags "${id}"
97 fi
98
99 # Even if we didn't download it, try to link it into the pool
100 [ "${pool}" ] && {
101   plnm="${taggery_base}/pool/${pool}/${indx}.${extn}"
102   if [ -e "${plnm}" ]
103   then
104     echo "Filename ${plnm} already exists"
105   else
106     mkdir -p "${taggery_base}/pool/${pool}"
107     ln -svf "../../md5/${name}" "${plnm}"
108   fi
109 }