Skip to content

Instantly share code, notes, and snippets.

@hanachin
Forked from kimihito/pivtweet.py
Created July 10, 2012 01:01
Show Gist options
  • Save hanachin/3080290 to your computer and use it in GitHub Desktop.
Save hanachin/3080290 to your computer and use it in GitHub Desktop.
PivotalTracker twitter bot: tweet story names that was accepted within 10 minutes ago. Its works on Heroku & heroku scheduler.

How to use

git clone git://gist.github.com/3080290.git pivtweet
cd pivtweet

You have to modify pivotal tracker api key, twitter oauth key, then git add ., git commit.

Run on local machine

pip install -r requirements.txt
python pivtweet.py

Run on the Heroku

heroku create --stack cedar your-proj-name
heroku addons:add scheduler
git push heroku master
heroku addons:open scheduler
  1. Click Add Job
  2. Past python pivtweet.py to input field.
  3. Change FREQUENCY Daily to every 10 minutes.
  4. That's it!
#!/usr/bin/env python
# coding: utf-8
import pivotal
import tweepy
from bs4 import BeautifulSoup
from datetime import datetime
from dateutil.parser import parse
from dateutil.tz import tzlocal
TOKEN = 'YOUR_TOKEN'
PROJECT_ID = 'YOUR_PROJECT_ID'
# get accepted stories from PivotalTracker
pv = pivotal.Pivotal(TOKEN)
_, content = pv.projects(PROJECT_ID).stories(filter = 'state:accepted').get()
soup = BeautifulSoup(content)
# pickup each story names and accepted_at times
stories = [(s.find('name').string, parse(s.accepted_at.string))
for s in soup.findAll('story')]
# remove hash tag #gokidea from name
stories = [(n.replace('#gokidea',''), t) for (n, t) in stories]
# select story names that was accepted within 10 minutes ago
now = datetime.now(tzlocal())
names = [n for (n, t) in stories if (now - t).total_seconds() <= 10 * 60]
# tweepy settings
CONSUMER_KEY = "YOUR_CONSUMER_KEY"
CONSUMER_SECRET = "YOUR_CONSUMER_SECRET"
ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"
ACCESS_TOKEN_SECRET = "YOUR_ACCESS_TOKEN_SECRET"
auth = tweepy.OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth_handler = auth)
# tweet each story names
for n in names:
api.update_status(n + u"を完了しました")
pivotal-py==0.1.3
tweepy==1.9
beautifulsoup4==4.1.1
python-dateutil==1.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment