Skip to content

Instantly share code, notes, and snippets.

@matsub
Last active June 5, 2016 11:41
Show Gist options
  • Save matsub/8acba364012560b5a175a0a55e871972 to your computer and use it in GitHub Desktop.
Save matsub/8acba364012560b5a175a0a55e871972 to your computer and use it in GitHub Desktop.
tweet automatically if you have new posts.
#!/bin/sh
# you need to write title in Front Matter of each post like:
# ---
# title: SOME TITLE
# ---
# require:
# https://github.com/shokai/tw
tw_ID="{{ your twitter ID registered in tw }}"
cname="{{ your GitHub Pages' CNAME }}"
permalink="{{ your jekyll permalink of posts }}"
auto_tweet=1 # 0 to disable
nnnn="\([0-9]\{4\}\)"
nn="\([0-9]\{2\}\)"
# url will be:
# https://${cname}/${permalink}/yyyy/mm/dd/title
function get_added() {
git diff remotes/origin/HEAD --name-only --diff-filter=A \
| grep -e "^_posts/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-.\+\.md"
}
function extract_pname() {
echo $1 \
| sed -ne "s/^_posts\/${nnnn}-${nn}-${nn}-\(.*\).md/\1\/\2\/\3\/\4/p"
}
# tweet added post
for post in `get_added`;do
if [[ $auto_tweet -ne 0 ]];then
ans="y"
else
echo "You have added new post: ${post}"
echo "tweet this? [y/N]"
read ans
fi
if [[ $ans =~ ^[Yy]* ]];then
title=`grep -e "^title: " ${post} | cut -d' ' -f2`
post_page=`extract_pname ${post}`
url="https://${cname}/${permalink}/${post_page}"
tweet="A new post in my blog \"${title}\" ${url}"
tw ${tweet} --user=${tw_ID} --yes
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment