Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Commit

Permalink
management command posts new job listings to Twitter account
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanpitts committed Jan 24, 2014
1 parent 6403345 commit 353366b
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions source/jobs/management/commands/tweet_new_jobs.py
Expand Up @@ -15,17 +15,18 @@
from datetime import datetime
import logging
import oauth2 as oauth
import urllib

from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.urlresolvers import reverse

from source.jobs.models import Job

CONSUMER_KEY=settings.TWITTER_CONSUMER_KEY
CONSUMER_SECRET=settings.TWITTER_CONSUMER_SECRET
ACCESS_KEY=settings.TWITTER_ACCESS_TOKEN
ACCESS_SECRET=settings.TWITTER_ACCESS_TOKEN_SECRET
CONSUMER_KEY=settings.JOBS_TWITTER_CONSUMER_KEY
CONSUMER_SECRET=settings.JOBS_TWITTER_CONSUMER_SECRET
ACCESS_KEY=settings.JOBS_TWITTER_ACCESS_TOKEN
ACCESS_SECRET=settings.JOBS_TWITTER_ACCESS_TOKEN_SECRET

logging.basicConfig(filename='twitter_job_posts.log', filemode='w', level=logging.INFO)

Expand All @@ -41,21 +42,33 @@ def handle(self, *args, **options):
client = oauth.Client(consumer, access_token)
api_endpoint = 'https://api.twitter.com/1.1/statuses/update.json'

# get all the Job records that are live, within proper
# time frame, but have not been posted to Twitter yet
jobs = Job.live_objects.filter(tweeted_at=None)
# get up to three Job records that are live, within proper time frame,
# but have not been posted to Twitter yet. oldest records first
jobs = Job.live_objects.filter(tweeted_at=None).order_by('listing_start_date')[:3]

# loop through our queryset
for job in jobs:
try:
# build the tweet
job_url = job.url or ('%s%s' % (settings.BASE_SITE_URL, reverse('job_list')))
tweet = "New job listing from %s: %s %s" % (job.organization.name, job.name, job_url)
if settings.DEBUG:
tweet = "TEST POST: %s" % tweet

# TODO: actually post to Twitter
print tweet
# post the tweet to Twitter
try:
response, content = client.request(
api_endpoint, method='POST',
body = urllib.urlencode({
'status': tweet,
'wrap_links': True
}),
)
except oauth.Error as err:
logging.info('TWITTER ERROR: %s' % err)

# update `tweeted_at` timestamp
# update `tweeted_at` timestamp so this record
# won't be picked up in queryset on next run
job.tweeted_at = datetime.now()
job.save()

Expand All @@ -65,3 +78,4 @@ def handle(self, *args, **options):
pass

logging.info('Finished posting: %s' % datetime.now())

0 comments on commit 353366b

Please sign in to comment.