]> git.g-eek.se Git - users/gustav/scripts.git/commitdiff
[confluence-tk] A downloader for confluence created
authorGustav Eek <gustav.eek@etraveli.com>
Thu, 6 Aug 2015 12:34:46 +0000 (14:34 +0200)
committerGustav Eek <gustav.eek@etraveli.com>
Thu, 6 Aug 2015 12:34:46 +0000 (14:34 +0200)
The confluence toolkit consists of a markdown downloader.

Makefile
confluence-tk [new file with mode: 0755]

index 8fc6a36f3837aefa1676d1365feae4a8f101db4e..6249a042b6d712f24087b6e3e6c754b898608ea9 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -7,6 +7,7 @@ INSTALL = $(HOME)/bin
 scripts = \
 msmtp-notify \
 repo-encrypt \
+confluence-tk
 
 bash-completion = \
 repo-encrypt \
diff --git a/confluence-tk b/confluence-tk
new file mode 100755 (executable)
index 0000000..73c03fa
--- /dev/null
@@ -0,0 +1,156 @@
+#!/bin/bash
+
+# Toolkit for retrieval and upload of confluence wiki page information
+# and similar based on curl and pandoc
+
+# Parse the command line. Regarding getopt(1) external or getopts(3)
+# builtin
+# http://blog.onetechnical.com/2012/07/16/bash-getopt-versus-getopts/
+
+HELP="${0##*/}: toolkit for confluence wiki page information retrieaval
+Usage: ${0##*/} [-h] [-i PAGE_ID] [-m]
+
+  -i PAGE_ID    Page ID of page to operate on
+  -m            Print page as markdown
+  -o            Out file (optional)
+  -c            HTML out (optional)
+  -h            Help -- show this page and exit
+  -v            Vebose output
+
+Page content is *not* printed on stdout, tough that would be
+preferred."
+
+example_page_id="46306514"
+example_page_id="46301431"
+
+function echoerr { echo "$@" 1>&2; }
+
+page_id=
+output_file=
+action=
+v=
+curl_out_file="/tmp/${0##*/}-$$-curlout"
+html_out_file="/tmp/${0##*/}-$$-htmlout"
+html_save=
+while getopts hvmi:o:c: flag; do
+    case "$flag" in
+       h)
+           echoerr "$HELP"
+           exit 0 ;;
+       i)
+           page_id="$OPTARG" ;;
+       o)
+           out_file="$OPTARG" ;;
+       c)
+           html_save="t"
+           html_out_file="$OPTARG" ;;
+       m)
+           action="m" ;;
+       v)
+           v="v" ;;
+       *)
+           echoerr "$HELP"
+           exit 1 ;;
+    esac
+done
+shift $(( OPTIND - 1 ))
+
+if [[ -z "$action" || -z "$page_id" ]]; then
+    echoerr "${0##*/}: ERROR: no action, page ID, and/or ouput page provided. Exits."
+    exit 1
+fi
+
+message="Continues with page id '$page_id' and action '$action'."
+if [ "$v" ]; then echoerr "${0##*/}: $message"; fi
+
+
+# Try to get page
+curl https://wiki.etraveli.net/plugins/viewstorage/viewpagestorage.action?pageId="$page_id"\
+     --verbose\
+     --cookie /tmp/cookie.cook --cookie-jar /tmp/cookie.cook\
+     > "$html_out_file" \
+     2> "$curl_out_file"
+
+if grep -q "HTTP/1.1 200 OK" < "$curl_out_file"; then
+    message="Download went smooth."
+    if [ "$v" ]; then echoerr "${0##*/}: $message"; fi
+
+elif grep -q "HTTP/1.1 302 Found" < "$curl_out_file" &&
+       grep -q "Location: .*login.action" < "$curl_out_file"; then
+    message="Found reason to believe that login is required."
+    if [ "$v" ]; then echoerr "${0##*/}: $message"; fi
+    
+    # Get usernama and password
+    echo "${0##*/}: Login is required. Provide username and password." 1>&2
+    stty -echo
+    printf "Username: "  1>&2; read username; echo 1>&2
+    printf "Password: "  1>&2; read password; echo 1>&2
+    stty echo
+    
+    # Login
+    curl https://wiki.etraveli.net/dologin.action\
+        --verbose\
+        --cookie /tmp/cookie.cook --cookie-jar /tmp/cookie.cook\
+        --data "os_username=$username"\
+        --data "os_password=$password"\
+        --data "formname=loginform"\
+        --data "login='Log In'"\
+        > /dev/null\
+        2> "$curl_out_file"
+    
+    if grep -q "HTTP/1.1 200 OK" < "$curl_out_file" &&
+           grep -q "X-AUSERNAME: $username" < "$curl_out_file"; then
+       message="Login went through."
+       if [ "$v" ]; then echoerr "${0##*/}: $message"; fi
+    else
+       echo "${0##*/}: ERROR: Login error. Wrong username or password?. Exits." 1>&2
+       echo "${0##*/}: See Curl output file '$curl_out_file'." 1>&2
+       if [ -z "$html_save" ]; then rm -f "$html_out_file"; fi
+       exit 2
+    fi
+
+    curl https://wiki.etraveli.net/plugins/viewstorage/viewpagestorage.action?pageId="$page_id"\
+        --verbose\
+        --cookie /tmp/cookie.cook --cookie-jar /tmp/cookie.cook\
+        > "$html_out_file" \
+        2> "$curl_out_file"
+
+    if grep -q "HTTP/1.1 200 OK" < "$curl_out_file"; then
+       message="Download went smooth."
+       if [ "$v" ]; then echoerr "${0##*/}: $message"; fi
+    else
+       echo "${0##*/}: Unknown error. Exits." 1>&2
+       echo "${0##*/}: See Curl output file '$curl_out_file'." 1>&2
+       if [ -z "$html_save" ]; then rm -f "$html_out_file"; fi
+       exit 4
+    fi
+    
+else 
+    echo "${0##*/}: Unknown error. Exits." 1>&2
+    echo "${0##*/}: See Curl output file '$curl_out_file'." 1>&2
+    if [ -z "$html_save" ]; then rm -f "$html_out_file"; fi
+    exit 3
+fi
+
+# Perfor action
+case "$action" in
+    "m")
+       if [ "$v" ]; then
+           echo  "${0##*/}: Translates to markdown using Pandoc." 1>&2; fi
+       if [ "$output_file" ]; then
+           pandoc -f html -t markdown-escaped_line_breaks -o "$output_file" < "$html_out_file"
+       else
+           pandoc -f html -t markdown-escaped_line_breaks < "$html_out_file" >&1
+       fi ;;
+    *)
+       echo "${0##*/}: Not a valid action. Exits." 1>&2
+       echo "${0##*/}: See Curl output file '$curl_out_file'." 1>&2
+       if [ -z "$html_save" ]; then rm -f "$html_out_file"; fi
+       exit 4 ;;
+esac
+
+# Clean up
+if [ "$v" ]; then echo "${0##*/}: Cleans up and exits." 1>&2; fi
+rm -f "$curl_out_file"
+if [ -z "$html_save" ]; then rm -f "$html_out_file"; fi
+exit 0