2wiki.sh

Aus Wiki CCC Göttingen
Wechseln zu: Navigation, Suche

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 </pre>

  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}

Ungültige Sprache.

Die gewünschte Sprache muss wie folgt definiert werden: <source lang="html4strict">...</source>

Unterstützte Sprachen für die Syntaxhervorhebung:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


 <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="$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")

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
</pre>
[[Category:Projekte]]