2wiki.sh

Aus Wiki CCC Göttingen
Zur Navigation springen Zur Suche springen

Idee

Upload ins Mediawiki via möglichst einfach gehaltenem Bash-Skript. Konstruiert, um via N900 direkt aus der Kamera-App Bilder hochzuladen

Anforderungen

Bash, Wget Sed, Grep mktemp

Hack-Faktor

Baut ein MIME multipart/form-data für wget, das das eigentlich nicht kann... Dürfte auch der Schwachpunkt der ganzen Angelegenheit sein...

Skript

#!/bin/bash
# whacky hacky mediawiki uploader.
# (c) 2011 Hans-Werner Hilse <hilse@web.de>
# this software is published under the know-what-you're-doing licence:
#
# 1. you may use this software for whatever you want, given that you know what you're doing.
# 2. author of this software isn't responsible for anything since you knew what you're doing.
# 3. if you have still questions, chances are you do not fullfill requirements
#----------------------------------------------------------------------------------------------


# hier konfigurieren:
APIURL="http://cccgoe.de/w/api.php"
LGNAME=""
LGPASS=""

# hier debuggen: :-)

# wenn dies in der datei vorkommt, haut das MIME/multipart/form-data nicht hin.
BOUNDARY="32ruofdihgo83hkj"

SOURCENAME="$1"
FILENAME="$2"
if [ "x$FILENAME" = "x" ] ; then
	FILENAME="$1"
fi

# temp file fuer die cookies
SAVECOOKIES=$(mktemp /tmp/2wiki.XXXXXXX)

# einloggen
RESULT=$(wget --quiet -O - \
--save-cookies "$SAVECOOKIES" \
--keep-session-cookies \
--post-data="action=login&format=xml&lgname=$LGNAME&lgpassword=$LGPASS" \
"$APIURL")

# neuere mediawiki brauchen dann dies zur token-bestaetigung:
if echo "$RESULT"|grep -q '<login result="NeedToken"'; then

	LGTOKEN=$(echo $RESULT|sed 's/^.*<login result="NeedToken" token="\([^"]\+\)".*$/\1/')
	RESULT=$(wget --quiet -O - \
	--load-cookies "$SAVECOOKIES" \
	--save-cookies "$SAVECOOKIES" \
	--keep-session-cookies \
	--post-data="action=login&format=xml&lgname=$LGNAME&lgpassword=$LGPASS&lgtoken=$LGTOKEN" \
	"$APIURL")
fi

# gucken, ob wir drin sind:
if ! echo "$RESULT" | grep -q '<login result="Success"'; then
	echo "Login failure"
	echo $RESULT
	rm "$SAVECOOKIES"
fi

echo "Logged in."

# edit-token holen (irgendwie brauchts einen Seitentitel - wir nehmen "FooBar"
RESULT=$(wget --quiet -O - \
--load-cookies "$SAVECOOKIES" \
--save-cookies "$SAVECOOKIES" \
--keep-session-cookies \
--post-data="action=query&format=xml&prop=info&intoken=edit&titles=FooBar" \
"$APIURL")
EDITTOKEN=$(echo $RESULT|sed 's/^.* edittoken="\([^"]\+\)".*$/\1/')

# hacky MIME multipart/form-data construction:

# wir brauchen das in einer Datei, da wget nicht gepuffert aus stdin liest:
TEMPPOSTDATA=$(mktemp /tmp/2wiki.XXXXXX)

echo -e "--$BOUNDARY\r
Content-Disposition: form-data; name=\"action\"\r
\r
upload\r
--$BOUNDARY\r
Content-Disposition: form-data; name=\"ignorewarnings\"\r
\r
true\r
--$BOUNDARY\r
Content-Disposition: form-data; name=\"filename\"\r
\r
$FILENAME\r
--$BOUNDARY\r
Content-Disposition: form-data; name=\"format\"\r
\r
xml\r
--$BOUNDARY\r
Content-Disposition: form-data; name=\"token\"\r
\r
$EDITTOKEN
--$BOUNDARY\r
Content-Disposition: form-data; name=\"file\"; filename=\"$FILENAME\"\r
Content-Type: application/octet-stream\r
Content-Transfer-Encoding: binary\r
\r" > "$TEMPPOSTDATA"

cat "$SOURCENAME" >> "$TEMPPOSTDATA"

echo -e "\r\n--$BOUNDARY--\r" >> "$TEMPPOSTDATA"

# und raus mit dem Kram!
RESULT=$(wget --quiet -O - \
--load-cookies "$SAVECOOKIES" \
--save-cookies "$SAVECOOKIES" \
--keep-session-cookies \
--header="Content-Type: multipart/form-data; boundary=$BOUNDARY" \
--post-file="$TEMPPOSTDATA" \
"$APIURL")

if ! echo "$RESULT" | grep -q '<upload result="Success"'; then
	echo "Upload failure" > /dev/stderr
	echo $RESULT > /dev/stderr
else
	echo "Upload successful" > /dev/stderr
	echo $RESULT | sed 's/^.* url="\([^"]\+\)".*$/\1/'
fi

rm "$TEMPPOSTDATA"
rm "$SAVECOOKIES"

N900 extensions

This is a script with the n900 extensions

  1. !/bin/bash
  2. whacky hacky mediawiki uploader.
  3. (c) 2011 Hans-Werner Hilse <hilse@web.de>
  4. this software is published under the know-what-you're-doing licence:
  5. 1. you may use this software for whatever you want, given that you know what you're doing.
  6. 2. author of this software isn't responsible for anything since you knew what you're doing.
  7. 3. if you have still questions, chances are you do not fullfill requirements
  8. ----------------------------------------------------------------------------------------------
  9. More hackyer n900 extensions
  10. (c) 2011 Marvin Sebulov (marvin@0x53a.de)
  11. If something goes wrong, ask hw.


  1. hier konfigurieren:

APIURL="http://cccgoe.de/w/api.php" LGNAME="Marvin" LGPASS="q64ZRdtB31WFbnHgo75"

  1. hier debuggen: :-)


perror() { if [ "x$allow_n900_messages" = "x1" ] then dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$*" else echo "$*" >&2 fi cleanup if [ "$read_before_exit" = "1" ] then read fi exit }

cleanup() { [ -e "$SAVECOOKIES" ] && rm "$SAVECOOKIES"; [ -e "$TEMPPOSTDATA" ] && rm "$TEMPPOSTDATA"; }

export allow_n900_messages=0 export read_before_exit=0

found=1 while [ "$found" = 1 ] do found=0 if [ "x$1" = "xwrap-n900-xterm" ] then pf=$(mktemp /tmp/2wiki.XXXXXXX) osso-xterm -e "$0 allow-n900-messages create-pid \"$pf\" read-before-exit \"$2\" \"$3\"" while ! [ -e "$pf" ] do sleep 0.5 done wait $(< "$pf") exit fi if [ "x$1" = "xcreate-pid" ] then echo $$ > "$2" shift shift found=1 fi if [ "x$1" = "xread-before-exit" ] then export read_before_exit=1 shift found=1 fi if [ "x$1" = "xallow-n900-messages" ] then export allow_n900_messages=1 shift found=1 fi done

if ! [ "x$#" = "x2" ] then echo 'Usage: '"$0" ' {create-pid <pidfile>|wrap-n900-xterm|read-before-exit|allow_n900_messages} <source file> <desired file name>' exit 1 fi

  1. wenn dies in der datei vorkommt, haut das MIME/multipart/form-data nicht hin.

BOUNDARY="32ruofdihgo83hkj"

SOURCENAME="$1" FILENAME="$2" if [ "x$FILENAME" = "x" ] ; then FILENAME="$1" fi

if ! [ -e "$SOURCENAME" ] then perror "Source file does not exist" fi

  1. temp file fuer die cookies

export SAVECOOKIES=$(mktemp /tmp/2wiki.XXXXXXX)

  1. einloggen

RESULT=$(wget --quiet -O - \ --save-cookies "$SAVECOOKIES" \ --keep-session-cookies \ --post-data="action=login&format=xml&lgname=$LGNAME&lgpassword=$LGPASS" \ "$APIURL")

  1. neuere mediawiki brauchen dann dies zur token-bestaetigung:

if echo "$RESULT"|grep -q '<login result="NeedToken"'; then

LGTOKEN=$(echo $RESULT|sed 's/^.*<login result="NeedToken" token="\([^"]\+\)".*$/\1/') RESULT=$(wget --quiet -O - \ --load-cookies "$SAVECOOKIES" \ --save-cookies "$SAVECOOKIES" \ --keep-session-cookies \ --post-data="action=login&format=xml&lgname=$LGNAME&lgpassword=$LGPASS&lgtoken=$LGTOKEN" \ "$APIURL") fi

  1. gucken, ob wir drin sind:

if ! echo "$RESULT" | grep -q '<login result="Success"'; then perror "Login failure" echo $RESULT rm "$SAVECOOKIES" fi

echo "Logged in."

  1. edit-token holen (irgendwie brauchts einen Seitentitel - wir nehmen "FooBar"

RESULT=$(wget --quiet -O - \ --load-cookies "$SAVECOOKIES" \ --save-cookies "$SAVECOOKIES" \ --keep-session-cookies \ --post-data="action=query&format=xml&prop=info&intoken=edit&titles=FooBar" \ "$APIURL") EDITTOKEN=$(echo $RESULT|sed 's/^.* edittoken="\([^"]\+\)".*$/\1/')

  1. hacky MIME multipart/form-data construction:
  1. wir brauchen das in einer Datei, da wget nicht gepuffert aus stdin liest:

export TEMPPOSTDATA=$(mktemp /tmp/2wiki.XXXXXX)

echo -e "--$BOUNDARY\r Content-Disposition: form-data; name=\"action\"\r \r upload\r --$BOUNDARY\r Content-Disposition: form-data; name=\"ignorewarnings\"\r \r true\r --$BOUNDARY\r Content-Disposition: form-data; name=\"filename\"\r \r $FILENAME\r --$BOUNDARY\r Content-Disposition: form-data; name=\"format\"\r \r xml\r --$BOUNDARY\r Content-Disposition: form-data; name=\"token\"\r \r $EDITTOKEN --$BOUNDARY\r Content-Disposition: form-data; name=\"file\"; filename=\"$FILENAME\"\r Content-Type: application/octet-stream\r Content-Transfer-Encoding: binary\r \r" > "$TEMPPOSTDATA"

cat "$SOURCENAME" >> "$TEMPPOSTDATA"

echo -e "\r\n--$BOUNDARY--\r" >> "$TEMPPOSTDATA"

  1. und raus mit dem Kram!

RESULT=$(wget --quiet -O - \ --load-cookies "$SAVECOOKIES" \ --save-cookies "$SAVECOOKIES" \ --keep-session-cookies \ --header="Content-Type: multipart/form-data; boundary=$BOUNDARY" \ --post-file="$TEMPPOSTDATA" \ "$APIURL")

if ! echo "$RESULT" | grep -q '<upload result="Success"'; then perror "Upload failure" echo $RESULT > /dev/stderr else perror "Upload successful" echo $RESULT | sed 's/^.* url="\([^"]\+\)".*$/\1/' fi

cleanup

if [ "$read_before_exit" = "1" ] then read fi