Skip to content

Instantly share code, notes, and snippets.

@thesnapdragon
Last active December 14, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thesnapdragon/5033469 to your computer and use it in GitHub Desktop.
Save thesnapdragon/5033469 to your computer and use it in GitHub Desktop.
Shell script for processing Blogspot .html posts commandline generated with UberWriter. Image upload, justify and another features.
#!/bin/bash
# Milán Unicsovics, u.milan at gmail dot com
# usage: ./processPost.sh <postname>.html
# required: googlecl (http://code.google.com/p/googlecl/)
post=$1
imgdir=<RESOURCE DIRECTORY>
blogname="<BLOGNAME>"
# Check post and test its format
if [ -z $post ]; then
exit -1
fi
ext=$(echo $post | cut -d '.' -f2)
if [ $ext != 'html' ]; then
echo 'Error! No .html file!'
exit -1
else
if ! [ -w $post ]; then
echo 'Error! File is not writable!'
exit -1
fi
fi
# Delete unused .css file
if [ `grep -c 'file:////usr/share/uberwriter/media/uberwriter.css' $post` == 1 ]; then
echo "Deleting unused .css file..."
delLine=`grep -n 'file:////usr/share/uberwriter/media/uberwriter.css' $post | cut -d ':' -f1`
sed -i $delLine'd' $post
fi
# Add justify to post
justify='.post { text-align: justify; }'
if [ `grep -c "$justify" $post` == 0 ]; then
echo "Adding justify to post..."
addLine=`grep -n '<style type="text/css">' $post | cut -d ':' -f1`
if [ -z $addLine ]; then
addLine=`grep -n '</title>' $post | cut -d ':' -f1`
justify='<style type="text/css"> .post { text-align: justify; } </style>'
sed -i $addLine"a$justify" $post
else
sed -i $addLine"a$justify" $post
fi
fi
# Open links in new tab
if [ `grep -c 'a href=' $post` != 0 ]; then
echo "Adding target=\"_blank\" to urls..."
sed -i 's/a href=/a target="_blank" href=/' $post
fi
# Insert images
while read line; do
if [ `echo "$line" | grep -c "\.#.*#\."` == 1 ]; then
img=$(echo "$line" | grep -o "\.#.*#\." | cut -d '#' -f2)
if ! [ -r $imgdir/$img ]; then
echo "Error! $imgdir/$img not found!"
exit -1
fi
echo "Inserting $imgdir/$img..."
imgname=$(echo "$img" | cut -d "." -f1)
# Calculate size
imgwidth=$(convert $imgdir/$img -print "%w\n" /dev/null)
imgheight=$(convert $imgdir/$img -print "%h\n" /dev/null)
if [ $imgwidth -gt 400 ] && [ $imgheight -gt 400 ]; then
imgwidth=400
div=$(echo `convert $imgdir/$img -print "%w\n" /dev/null`/400 | bc -l)
imgheight=$(echo `convert $imgdir/$img -print "%h\n" /dev/null`/$div | bc -l)
imgheight=$(echo $imgheight | cut -d "." -f1)
fi
# Upload
if [ `google picasa list -n "$blogname" --fields url-direct --query $imgname | grep -c '.*'` == 0 ]; then
echo "Uploading $imgdir/$img..."
google picasa post -n "$blogname" $imgdir/$img
fi
# Get url
try=0
imgurl=''
while [ -z $imgurl ]; do
imgurl=$(google picasa list -n "$blogname" --fields url-direct | grep $imgname | head -n 1)
echo "Trying to fetch url of $img...$try"
sleep 1
try=$[$try+1]
done
imgurl_big=$(echo "$imgurl" | sed -e "s/\(.*\)\/\(.*\)/\1\/s1600\/\2/")
imgurl_small=$(echo "$imgurl" | sed -e "s/\(.*\)\/\(.*\)/\1\/s400\/\2/")
imgenv="<div class=\"separator\" style=\"clear: both; text-align: center;\"> \
<a href=\"$imgurl_big\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"> \
<img border=\"0\" height=\"$imgheight\" src=\"$imgurl_small\" width=\"$imgwidth\" /> \
</a> \
</div>"
# Escape imgenv
imgenv=$(echo $imgenv | sed -e 's/[]\/()$*.^|[]/\\&/g')
# Replace with urls
sed -i "s/\.#$img#\./$imgenv/" $post
echo "Image $imgdir/$img inserted!"
fi
done < $post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment