Skip to content

Instantly share code, notes, and snippets.

@mafflerbach
Last active November 8, 2020 19:41
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 mafflerbach/bdb16ebba3d3705918b4c9f322212d28 to your computer and use it in GitHub Desktop.
Save mafflerbach/bdb16ebba3d3705918b4c9f322212d28 to your computer and use it in GitHub Desktop.
#!/bin/bash
# this file contains all my api keys as variable.
# here used:
# devtoApiToken="<your dev.to api key>"
# dropboxApiToken="<your dropbox api key>"
source ~/dotfiles/.credencials
function publishingToDevTo {
filePath=$1
devtoApiToken=$2
# Content has to be json encoded
content=$(cat "$filePath" | jq '.' --raw-input --slurp)
echo -e "=== START publishingToDevTo ==="
tee /tmp/devtoArticle.md << END
{"article":{"body_markdown":$content}}
END
curl -X POST -H "Content-Type: application/json" \
-H "api-key: ${devtoApiToken}" \
-d @/tmp/devtoArticle.md \
https://dev.to/api/articles
echo -e "=== END publishingToDevTo ==="
}
function movingFilesToPublishDir {
filePath=$1
echo -e "=== START movingFilesToPublishDir ==="
# escaping filenames with spaces for the mv command
#get the current date
date=$(date +%m-%d-%Y)
# grap only the filename
fileName=$(echo "$filePath" | cut -d"/" -f3)
mv "$filePath" "$(pwd)/content/published/$date $fileName"
echo -e "=== END movingFilesToPublishDir ==="
}
function gitAction {
fileName=$1
commitMessage="Add and published -$fileName-"
echo -e "=== START gitAction ==="
git add .
git commit -m"$commitMessage"
git push
echo -e "=== END gitAction ==="
}
function uploadImagesToDropbox {
dropboxApiToken=$2
#grep all images in content and read as array
readarray -t content < <(grep "^\!\[.*\]" "$1")
echo -e "=== START uploadImagesToDropbox ==="
for i in "${content[@]}"
do :
#Get image Name in between [ and ]
imageName=$(echo $i | sed -e"s/\!\[//g" -e "s/\]//g")
# Upload all images
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer $dropboxApiToken" \
--header "Dropbox-API-Arg: {\"path\": \"/Images/$imageName\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @"/home/maren/development/blog/content/images/$imageName"
# Create shared link
curl -X POST https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings \
--header "Authorization: Bearer $dropboxApiToken" \
--header "Content-Type: application/json" \
--data "{\"path\": \"/Images/$imageName\",\"settings\": {\"requested_visibility\": \"public\",\"audience\": \"public\",\"access\": \"viewer\"}}"
# Get the shared image url,
# get the url value from response with jq '.url'
# replaces link parameter dl=0 to raw=1
# removed " from string
url=$(curl -X POST https://api.dropboxapi.com/2/sharing/list_shared_links \
--header "Authorization: Bearer $dropboxApiToken" \
--header "Content-Type: application/json" \
--data "{\"path\": \"/Images/$imageName\"}" | jq '.links[0].url' | sed -e "s/dl=0/raw=1/g" -e "s/\"//g")
# add url to image line
echo -e "=== REPLACE image tags $imageName in $1 ==="
sed -i "s#\[$imageName\]#\[$imageName\]\($url\)#g" "$1"
done
echo -e "=== END uploadImagesToDropbox ==="
}
filePath=$1
# to keep it simple, use the first 6 lines
headOfContent=$(cat "$filePath" | head -n6)
if [[ "$headOfContent" != *"title"* ]]; then
echo "No title Flag found"
exit
fi
if [[ "$headOfContent" != *"published"* ]]; then
echo "No publisehd Flag found"
exit
fi
uploadImagesToDropbox "$filePath" $dropboxApiToken
publishingToDevTo "$filePath" $devtoApiToken
movingFilesToPublishDir "$filePath"
gitAction "$fileName"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment