Created
July 16, 2019 00:45
-
-
Save mrrcollins/ac11e2b829413a89c0f7f1801506cf33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Store private config information in ~/.schedarchive | |
# Requirements: | |
# wp-cli installed and working | |
# Buffer account | |
# Buffer API KEY | |
# Buffer profile ids for the accounts that you want to post to | |
if [ ! -f ~/.schedarchive ]; then | |
SITE="@SITE" #wp-cli site alias | |
ACCESS_TOKEN=BUFFERAPITOKEN #Access token for Buffer | |
PROFILES="profile_ids[]=PROFILEID1" #profile_ids is an array | |
PROFILES+="&profile_ids[]=PROFILEID2" #of ids to use with Buffer | |
PROFILES+="&profile_ids[]=PROFILEID3" | |
PROFILES+="&profile_ids[]=PROFILEID4" | |
else | |
source ~/.schedarchive | |
fi | |
function schedpostfor() { | |
D=${1} | |
YEAR=$(date +%Y -d "${D}" ) | |
MONTH=$(date +%m -d "${D}" ) | |
DAY=$(date +%d -d "${D} + ${dl} day" ) | |
echo "Working on ${YEAR}-${MONTH}-${DAY}" | |
unset POST | |
POST=$(wp ${SITE} post list --year=${YEAR} --monthnum=${MONTH} --day=${DAY} --fields=post_title,url,ID,post_date,post_status --format=csv | tail -n +2 | grep publish | grep "12:30:00") | |
#echo "Post: ${POST}" | |
TITLE=$(echo "${POST}" | cut -d "," -f 1) | |
URL=$(echo "${POST}" | cut -d "," -f 2) | |
POSTID=$(echo "${POST}" | cut -d "," -f 3) | |
TAGS=$(wp ${SITE} post term list ${POSTID} post_tag --field=name --format=csv) | |
MSGTAG="" | |
ARR=(${TAGS}) | |
for tag in "${ARR[@]}" | |
do | |
MSGTAG="#${tag} ${MSGTAG}" | |
done | |
MSG=$(urlencode "${TITLE} #eduk8me ${MSGTAG}") | |
if [ ${#POST} -lt 10 ]; then | |
echo "No post at 12:30pm for ${YEAR}-${MONTH}-${DAY}" | |
else | |
echo "Posting ${TITLE} - ${URL}" | |
SCHEDULED=$(date +%Y-%m-%d -d "${TODAY} + ${dl} day")T${HOUR}:03:00-04:00 | |
POSTDATA="access_token=${ACCESS_TOKEN}&${PROFILES}&scheduled_at=${SCHEDULED}&text=${MSG}&media[link]=${URL}" | |
#echo ${POSTDATA} | |
#echo ${SCHEDULED} | |
#curl -s -d ${POSTDATA} https://api.bufferapp.com/1/updates/create.json | jq '.' | |
curl -s -d ${POSTDATA} https://api.bufferapp.com/1/updates/create.json > /dev/null | |
fi | |
} | |
TODAY=$(date +"%Y-%m-%d" -d "+ 1 day") # Start with tomorrow's date | |
# Schedule for last two years | |
for yl in 1 2 | |
do | |
for dl in {0..6} | |
do | |
THISDATE=$(date -d "${TODAY} - ${yl} year") #subtract ${yl} years | |
let HOUR=${yl}*10 | |
schedpostfor "${THISDATE}" #${dl} adds a day in the function for each day of the week | |
done | |
done | |
# Schedule from last month | |
for dl in {0..6} | |
do | |
THISDATE=$(date -d "${TODAY} - 1 month") | |
HOUR=13 | |
schedpostfor "${THISDATE}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment