API Zugang zu üäx.de Linkshorter

Shellscript  (04.06.2010 23:53) Eine Bash-Schnittstelle zum Linkshorter üäx.de.

Quellcode (ausblenden | aufklappen)

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
#!/bin/bash
# ueaex.sh
 
 
API_URL="http://xn--x-zfa5e.de/api/"
API_KEY="<api_key>"
OUTPUT="xml"
 
function init()
{
    API_URL=${API_URL}${API_KEY}"/"
    API_URL=${API_URL}${OUTPUT}"/"
}
 
function add()
{
    API_URL=${API_URL}"add/"
    POST="url="$1
    BUF=`curl -s -d "$POST" $API_URL`
echo $BUF
    ERR=`echo $BUF | sed -n 's|\(.*\)<error_code>\(.*\)</error_code>\(.*\)|\2|p'`
    if [ "$ERR" = "no_error" ]; then
        echo $BUF | sed -n 's|\(.*\)<short_url>\(.*\)</short_url>\(.*\)|\2|p'
    else
        echo $BUF | sed -n 's|\(.*\)<error_message>\(.*\)</error_message>\(.*\)|\2|p'
    fi
}
 
function get()
{
    API_URL=${API_URL}"get/"
    POST="url_code="$1
    BUF=`curl -s -d "$POST" $API_URL`
    ERR=`echo $BUF | sed -n 's|\(.*\)<error_code>\(.*\)</error_code>\(.*\)|\2|p'`
    if [ "$ERR" = "no_error" ]; then
        echo $BUF | sed -n 's|\(.*\)<url>\(.*\)</url>\(.*\)|\2|p'
    else
        echo $BUF | sed -n 's|\(.*\)<error_message>\(.*\)</error_message>\(.*\)|\2|p'
    fi
}
 
if [ -n "$2" ]; then
    if [ "$1" = "add" ]; then
        init
        add $2
        exit
    fi
 
    if [ "$1" = "get" ]; then
        init
        get $2
        exit
    fi
fi
 
echo "$0 add <url>"
echo "$0 get <code>"