summaryrefslogtreecommitdiffstats
path: root/bin/comics/xkcd
blob: 351790b19380111db6531146d17576aa20704abd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash

if [ -z "$1" -o ! -d "$1" ]; then
    echo "Usage: $0 <top-directory> [<days ago>]" 1>&2
    exit 1
fi

top="${1}"
shift

source "${top}/etc/settings.sh"
tmp="$(mktemp -d)"
cd "${tmp}"

browser="Mozilla/4.76 [de] (X11; U; Linux 2.2.18 i586)"
newn="${day}.png"

# get url
echo $(date -d"$daysago days ago" +%Y-%-m-%-d)
url="$(wget ${wget_args} -U "$browser" https://xkcd.com/archive/ -O - | \
    grep -o "href=\".*\" .*\"$(date -d"$daysago days ago" +%Y-%-m-%-d)\"" | \
    sed 's/^href="\(.*\)" title.*$/\1/')"

if [ -n "$url" ]; then
    # fetch comic
    wget ${wget_args} -U "$browser" "https://www.xkcd.com${url}" -O index.html

    url=$(grep -A 1 "Image URL" index.html | sed 'N;s/.*\(http.*png\)<\?.*/\1/')
    imgname=$(echo "$url" | tr '/' '\n' | tail -1)

    # get additional information
    img_tag=$(grep -A2 "src=\"${url:5}" index.html | sed 'N;N;s/.*\(<img [^>]\+>\).*/\1/')
    alt=$(sed 's/.*title="\([^"]*\)".*/\1/'<<< $img_tag)
    title=$(sed 's/.*alt="\([^"]*\)".*/\1/'<<< $img_tag)

    wget ${wget_args} -U "$browser" -O "${tmp}/${newn}" --header="Referer: https://www.xkcd.com/" "$url"

    if [ -s "${tmp}/${newn}" ]; then
        # save
        mv "${tmp}/${newn}" "${image_dir}/$newn"
        echo "$title" > "${image_dir}/${day}.title"
        echo "$alt" > "${image_dir}/${day}.alt"

        # update symlinks
        rm -f "${comic_dir}/latest.png" "${comic_dir}/latest.title" "${comic_dir}/latest.alt"
        ln -s "${image_offset}/$newn" "${comic_dir}/latest.png"
        ln -s "${image_offset}/${day}.title" "${comic_dir}/latest.title"
        ln -s "${image_offset}/${day}.alt" "${comic_dir}/latest.alt"
    fi
fi

rm -rf ${tmp}