]> CyberLeo.Net >> Repos - CDN/taggery.git/blob - fetch
Add rule34.paheal profile
[CDN/taggery.git] / fetch
1 #!/bin/sh -e
2
3 pebkac() {
4   [ "${*}" ] && printf "%s\n\n" "${*}"
5   cat <<EOF
6 Usage: $(basename "${0}") <url>
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   -n             Noop-mode; do not download image nor write tags
13   -p <poolname>  Associates the downloaded image with a pool
14   -i <indexnum>  Used with -p to indicate the position in the pool
15
16   -t             Refresh the tags, not the picture
17   -f <filename>  If the image is deleted, provide the local filename
18 EOF
19   kill $$
20   exit 1
21 }
22
23 while getopts dnp:i:tf: OPT
24 do
25   case "${OPT}" in
26   d)  DEBUG=DEBUG      ;;
27   n)  noop=noop        ;;
28   p)  pool="${OPTARG}" ;;
29   i)  indx="${OPTARG}" ;;
30   t)  tags=TAGS        ;;
31   f)  hash="${OPTARG}" ;;
32   ?)  pebkac           ;;
33   esac
34 done
35 shift $(( $OPTIND - 1 ))
36
37 [ "${*}" ] || pebkac
38 url="${1}"
39 #[ -z "$(echo "${id}" | tr -d '[0-9]')" ] || pebkac "ID should be an integer"
40
41 taggery_base="$(dirname "${0}")"
42 taggery_libs="$(dirname "${0}")/lib"
43
44 # Try and find the profile that can handle the url
45 profiles="$(cd "${taggery_libs}/profiles"; ls -1 | sed -e 's/.sh$//')"
46 for profile in ${profiles}
47 do
48   . "${taggery_libs}/profiles/${profile}.sh"
49   "${profile}_can_handle" "${url}" || continue
50   taggery_name="${profile}"
51   break
52 done
53
54 # Set up temporary area
55 #mkdir -p "$(dirname "${0}")/tmp"
56 #TMPDIR="$(dirname "${0}")/tmp"
57
58 # Load KVS
59 #kvs="${TMPDIR}/.${taggery_name}.kvdb"
60 #. "$(dirname "${0}")/lib/kvs.sh"
61
62 # Load Taggery
63 . "${taggery_libs}/taggery.sh"
64
65 id="$(taggery_id "${url}")"
66 name="$(taggery_image_name "${id}")"
67 file="$(taggery_image_url "${id}")"
68 tags="$(taggery_image_tags "${id}")"
69 [ "${file}" ] || name="${hash}"
70 extn="${name#*.}"
71
72 [ "${DEBUG}" ] && {
73   echo "id: ${id}"
74   echo "name: ${name}"
75   echo "file: ${file}"
76   echo "tags: ${tags}"
77   echo ""
78   kvs_get_all "${taggery_name}/${id}"
79   kill -KILL $$
80 }
81
82 [ "${file}" -o "${hash}" ] || { echo "No download URL (deleted?) and -f not provided" >&2; kill -ABRT $$; exit 1; }
83 [ "${tags}" ] || { echo "No tags found" >&2; kill -ABRT $$; exit 1; }
84
85 unset nabbed
86 if [ "${file}" ]
87 then
88   if [ -e "${taggery_base}/md5/${name}" ]
89   then
90     echo "Filename ${name} already exists"
91   else
92     if [ "${noop}" ]
93     then
94       echo "taggery_fetch_image(${id})"
95       echo "taggery_image_url => $(taggery_image_url "${id}")"
96       echo "taggery_image_name => $(taggery_image_name "${id}")"
97     else
98       taggery_fetch_image "${id}"
99       nabbed=NABBED
100     fi
101   fi
102 fi
103
104 if [ "${nabbed}" -o "${tags}" ]
105 then
106   if [ "${noop}" ]
107   then
108     echo "taggery_link_tags(${id}):"
109     echo "${tags}" | sed -e 's/^/  /g'
110   else
111     taggery_link_tags "${id}"
112   fi
113 fi
114
115 # Even if we didn't download it, try to link it into the pool
116 [ -z "${noop}" ] && [ "${pool}" ] && {
117   plnm="${taggery_base}/pool/${pool}/${indx}.${extn}"
118   if [ -e "${plnm}" ]
119   then
120     echo "Filename ${plnm} already exists"
121   else
122     mkdir -p "${taggery_base}/pool/${pool}"
123     ln -svf "../../md5/${name}" "${plnm}"
124   fi
125 }