Skip to content

Instantly share code, notes, and snippets.

@paulusm
Last active August 29, 2015 14:14
Show Gist options
  • Save paulusm/5e7dae63c16f166b1897 to your computer and use it in GitHub Desktop.
Save paulusm/5e7dae63c16f166b1897 to your computer and use it in GitHub Desktop.
Post output from Crowdcrafting to Buffer App
###
# Function to post a single tweet to Buffer as a scheduled post
###
import requests
import iso8601
from dateutil.relativedelta import relativedelta
def bufferpost(tweetText, tweetDate):
# Credentials for Buffer API
accessToken = "<Your token here>"
clientId = "<Your token here>"
profileId = "<Your token here>"
postUrl = "https://api.bufferapp.com/1/updates/create.json"
utcTime = "T12:00:00"
#tweetText = "I was on duty again from 8.00 am to midday on Thursday 24th December (Xmas eve)... http://goo.gl/Yle49j"
#tweetDate = "1914-12-24"
parsedDate = iso8601.parse_date(tweetDate+utcTime)
# Add 100 years to the diary date
parsedDate= parsedDate + relativedelta(years=100)
postData = {"access_token": accessToken,"text": tweetText,
"profile_ids[]": profileId, "scheduled_at": parsedDate.isoformat(),
"pretty": "true"}
headr = {'Content-Type': 'application/x-www-form-urlencoded'}
#print postData
resp = requests.post(postUrl,postData, headers=headr)
print resp
###
# Main Script to run - Retieves data from Crowdcrafting.org and selectively Buffers it.
# Adjust fromDate and toDate as necessary
###
import requests
import iso8601
from datetime import datetime
import pytz
from bufferpost import bufferpost
# Get the completed tasks from Crowdcrafting.org
# Can only post 10 at a time to Buffer, so base this on a date window
fromDate = datetime(1915,01,12,tzinfo=pytz.UTC)
toDate = datetime(1915,01,22,tzinfo=pytz.UTC)
data = requests.get("http://crowdcrafting.org/api/taskrun?app_id=2268")
for item in data.json():
# Need to check that the tweet has content and a correct date
if 'date' in item['info'][0]:
parsedDate = iso8601.parse_date(item['info'][0]['date'])
if toDate >= parsedDate >= fromDate:
print item['info'][0]['text'], item['info'][0]['date']
bufferpost(item['info'][0]['text'], item['info'][0]['date'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment