2wiki.sh: Unterschied zwischen den Versionen

Aus Wiki CCC Göttingen
Wechseln zu: Navigation, Suche
(highlightSyntax)
(15 dazwischenliegende Versionen von 6 Benutzern werden nicht angezeigt)
Zeile 17: Zeile 17:
 
== Skript ==
 
== Skript ==
  
<pre>
+
<highlightSyntax language="bash">
 
#!/bin/bash
 
#!/bin/bash
 
# whacky hacky mediawiki uploader.
 
# whacky hacky mediawiki uploader.
Zeile 30: Zeile 30:
  
 
# hier konfigurieren:
 
# hier konfigurieren:
APIURL="http://cccgoe.de/w/api.php"
+
APIURL="https://cccgoe.de/w/api.php"
 
LGNAME=""
 
LGNAME=""
 
LGPASS=""
 
LGPASS=""
Zeile 139: Zeile 139:
 
rm "$TEMPPOSTDATA"
 
rm "$TEMPPOSTDATA"
 
rm "$SAVECOOKIES"
 
rm "$SAVECOOKIES"
</pre>
+
</highlightSyntax>
 +
 
 
== N900 Extensions ==
 
== N900 Extensions ==
This is a script with the n900 extensions
+
===Script===
<pre>
+
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.)
 +
 
 +
<highlightSyntax language="bash">
 
#!/bin/bash
 
#!/bin/bash
 
# whacky hacky mediawiki uploader.
 
# whacky hacky mediawiki uploader.
Zeile 373: Zeile 380:
 
fi
 
fi
 
fi
 
fi
</pre>
+
</highlightSyntax>
===N900 Setup===
+
 
Download the script and put it into a script, 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:
+
===Setup===
  /sbin/2wiki.sh wrap-n900-xterm "%s" "%s"
+
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"
  
This will open an x terminal which contains all the necessary log information. If you want silent upload using only the n900 messaging system, add the following cli sharing account:
+
===Usage===
/sbin/2wiki.sh allow-n900-messages "%s" "%s"
+
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.
 
[[Category:Projekte]]
 
[[Category:Projekte]]
 +
[[Category:N900]]

Version vom 7. April 2013, 18:19 Uhr

Idee

Latest test image that has been uploaded by the testers of this script.

Upload ins Mediawiki via möglichst einfach gehaltenem Bash-Skript. Konstruiert, um via N900 direkt aus der Kamera-App Bilder hochzuladen - es kann aber auf jedem x-beliebigen System verwendet werden. Speziell fuer das N900 gibts die #N900 Extensions.

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

<highlightSyntax language="bash">

  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. ----------------------------------------------------------------------------------------------


  1. hier konfigurieren:

APIURL="https://cccgoe.de/w/api.php" LGNAME="" LGPASS=""

  1. hier debuggen: :-)
  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

  1. temp file fuer die cookies

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 echo "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:

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 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" </highlightSyntax>

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.)

<highlightSyntax language="bash">

  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="" LGPASS="" FILE_PREFIX="changemeplease_"

  1. 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}

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="$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
</highlightSyntax>

===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.
[[Category:Projekte]]
[[Category:N900]]