Commons:Ancient Chinese characters:Linux tutorial

From Wikimedia Commons, the free media repository
Jump to: navigation, search

First you need to get a token and session ID; For this you can you the login API:

curl -d "action=login&lgname=Myusername&lgpassword=Mypassword&format=xml" "http://commons.wikimedia.org/w/api.php"

A sample output is shown below

<?xml version="1.0"?>
<api>
<login 
result="Success" 
lguserid="999999" 
lgusername="Myusername" 
lgtoken="43f11918fcbb662ec5118c78721e03b1" 
cookieprefix="commonswiki" 
sessionid="b22577b457293dd37a161e80bbb40943" />
</api>

With the values obtained above, you need to fill the first four lines of the script below. The script assumes it is run in a directory whose name is the character to process (in UTF8) and it contains a GIF for each styles (bronze, seal, big seal, oracle). The name of the GIF files are the same as the names in the Richard Sears' site.

session=""
token=""
user=""
userid=""

char=`basename $PWD`

for file in *gif
do

    echo "----------------------"
    gifbase=`basename $file .gif` 
    width=`identify -format '%w' $gifbase.gif`
    height=`identify -format '%h' $gifbase.gif`
    echo "size $width x $height"
    if [ "$width" -gt "$height" ] 
    then
        max=$width
    else
        max=$height
    fi
    echo "Max is $max"
    # convert the GIF to a squared centered BMP image 
    # which side is the largest of the width and height of the GIF
    convert \( -size $max\x$max xc:white \) \
        \( -filter lanczos -size x$max $gifbase.gif \) \
        -gravity center -composite +repage $gifbase.bmp
            
    potrace -s $gifbase.bmp
    rm $gifbase.bmp
    
    
    case "$gifbase" in
        j*) echo "oracle"
            description="{{ACClicense|$char|oracle|oracle|$gifbase|}}"
            filen="oracle"
            ;;
        s*) echo "seal"
            description="{{ACClicense|$char|seal|seal|$gifbase||}}"
            filen="seal"
            ;;
        L*) echo "bigseal"
            description="{{ACClicense|$char|bigseal|Great seal|$gifbase||}}"
            filen="bigseal"
            ;;
        b*) echo "bronze"
            description="{{ACClicense|$char|bronze|bronze|$gifbase|}}"
            filen="bronze"
            ;;
    esac
    echo $description
    rm -f $char-$filen.svg
    ln -s $gifbase.svg $char-$filen.svg
    # repost: wpIgnoreWarning wpForReUpload=1
    curl -v \
        -L \
        -H "Cookie: commonswikiToken=$token; commonswikiUserName=$user; commonswikiUserID=$userid; commonswiki_session=$session" \
        -F "wpUploadFile=@$char-$filen.svg;type=image/svg+xml" \
        --form-string "wpSourceType=file" \
        --form-string "wpDestFile=$char-$filen.svg" \
        --form-string "wpUploadDescription=$description" \
        --form-string "wpIgnoreWarning=true" \
        --form-string "wpUpload=Upload file" \
        --form-string "wpDestFileWarningAck=" \
        --form-string "wpForReUpload=1" \
        -H "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.22) Gecko/20090605 Iceape/1.1.17 (Debian-1.1.17-1)" \
        -H "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" \
        -H "Accept-Language: en-us,en;q=0.5" \
        -H "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" \
        -H "Keep-Alive: 300" \
        -H "Expect: " \
        "http://commons.wikimedia.org/wiki/Special:Upload"

    
done

Note: the margin obtained in the SVG image is not guaranteed to follow the 300px/270px convention of the Inkscape tutorial, but in practice the results are satisfying.