2wiki.sh: Unterschied zwischen den Versionen

Aus Wiki CCC Göttingen
Zur Navigation springen Zur Suche springen
(Nlz6r6 http://KkRzxaxyKwfz3eqX.com)
(Änderung 2578 von 195.218.148.226 (Diskussion) rückgängig gemacht.)
Zeile 1: Zeile 1:
N5yOic http://yGndcGzwsm4kX86G.com
N5yOic http://yGndcGzwsm4kX86G.com


Nlz6r6 http://KkRzxaxyKwfz3eqX.com
== Anforderungen ==
 
Bash, Wget
Sed, Grep
mktemp


== Hack-Faktor ==
== Hack-Faktor ==

Version vom 28. Oktober 2011, 11:07 Uhr

N5yOic http://yGndcGzwsm4kX86G.com

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

Script

This is a script with the n900 extensions. It adds:

  • A bit more error handling (really, just a bit)
  • Support for N900 messaging system
  • Wrapping the share script into an osso-xterm
  • Prefixing of files. (This is necessary as the photo app will name the photos by date and and incremental index that is reset once per day. Thus two people using the camera app with sharing on the same day are very likely going to overwrite their pictures.)
#!/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
#----------------------------------------------------------------------------------------------
# More hackyer n900 extensions
# (c) 2011 Marvin Sebulov (marvin@0x53a.de)
# If something goes wrong, ask hw.


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

# hier debuggen: :-)

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

perror()
{
	pmsg "$*"
	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)
		# Star terminal and script:
		osso-xterm -e "$0 allow-n900-messages create-pid \"$pf\" read-before-exit \"$2\" \"$3\""
		# Wait a moment
		npid=0
		echo "Waiting for pid..."
		while ! test "0$npid" -gt 0 2>/dev/null
		do
			npid=$(< "$pf")
			sleep 1
		done
		echo $npid
		while pidof bash 2>/dev/null | grep $npid >/dev/null
		do
			sleep 1
		done
		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
# wenn dies in der datei vorkommt, haut das MIME/multipart/form-data nicht hin.
BOUNDARY="32ruofdihgo83hkj"

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

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

# temp file fuer die cookies
export 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
	perror "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:
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"

# 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")

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

cleanup

if [ "$read_before_exit" = "1" ]
then
	echo "Close window or press enter to continue."
	if [ "$success" = 1 ]
	then
		echo "Press x to open URL in the browser."
	fi
	echo "This message will automatically disappear in 10 seconds."
	read -n 1 -t 10 x
	if [ "x$x" = "xx" ]
	then
		if [ "$success" = "1" ]
		then
			dbus-send --system --type=method_call --dest=com.nokia.osso_browser /com/nokia/osso_browser/request com.nokia.osso_browser.load_url string:"$url"
		fi
	fi
fi

Setup

Download the script and put it into a file, e.g. /bin/2wiki.sh. chmod a+rx it and configure your wiki account (first few lines of the script). Install Command Line Sharing form your application manager. Go to Settings -> Sharing Accounts, add a new command line sharing account with the following settings:

/bin/2wiki.sh wrap-n900-xterm "%s" "%s"

This will open an x terminal which contain a rudimentary log. If you want silent upload using only the n900 messaging system, add the following cli sharing account:

/bin/2wiki.sh allow-n900-messages "%s" "%s"

Usage

Make a photo and open it in the gallery. Click on the sharing button (three connected dots), select the account you just created and click on share.