aboutsummaryrefslogtreecommitdiff
path: root/scuttle
blob: e065b48c3645fa27464c5388ec1b5da55f84ed3d (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
#
# Post a link to a scuttle service.
#

# Thanks https://gist.github.com/cdown/1163649
function urlencode() {
  # urlencode <string>
  old_lc_collate=$LC_COLLATE
  LC_COLLATE=C

  local length="${#1}"
  for (( i = 0; i < length; i++ )); do
    local c="${1:i:1}"
    case $c in
      [a-zA-Z0-9.~_-]) printf "$c" ;;
      *) printf '%%%02X' "'$c" ;;
    esac
  done

  LC_COLLATE=$old_lc_collate
}

# Parameters
BASENAME="`basename $0`"
URL="$1"
TAGS="$2"
shift 2
DESC="$*"
TMP="${TMP:=/tmp}"

# Syntax
if [ -z "$TAGS" ]; then
  echo "usage: $BASENAME <url> <tags> [description]"
  exit
fi

# Config
source "$HOME/.custom/scuttle" || exit 1

# See http://www.wired.com/2010/02/using_the_delicious_api/
#CALL="$SCUTTLE_URL/api/posts/add?url=`urlencode $URL`"
CALL="$SCUTTLE_URL/api/posts/add"

# Tags
TAGS="`echo $TAGS | sed -e 's/,/ /g'`"
#TAGS="`urlencode "$TAGS"`"
TAGS="`echo $TAGS | sed -e 's/%2C/,/g'`"
#CALL="$CALL&tags=$TAGS"

# Description
if [ -z "$DESC" ]; then
  # See http://stackoverflow.com/questions/3195851/ddg#3195895
  DESC="`torify curl -L -s $URL | grep -i "<title>" | sed -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q' 2> /dev/null`"
  echo "Fetched description: $DESC"
fi

# Full API call
#CALL="$CALL&description=`urlencode "$DESC"`"

# Write config
CONF="`mktemp -t scuttle.XXXXXXXXXX -p $TMP`"
echo "url = $CALL" > $CONF
echo "user = $SCUTTLE_USER:$SCUTTLE_PASS" >> $CONF

# Remove trailing slash which might lead to urlencode errors by curl (dunny why)
URL="`echo $URL | sed -e 's|/$||'`"

# Call curl
# See https://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command#2027690
#curl -s -K $CONF &> /dev/null
curl -s -K $CONF -G --data-urlencode "url=$URL" --data-urlencode "tags=$TAGS" --data-urlencode "description=$DESC" &> /dev/null

# Status
STATUS="$?"

# Teardown
rm $CONF
exit $STATUS