Skip to content

Instantly share code, notes, and snippets.

@Apreche
Created May 30, 2010 19:23
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 Apreche/419244 to your computer and use it in GitHub Desktop.
Save Apreche/419244 to your computer and use it in GitHub Desktop.
from django.conf import settings
from django.contrib.sites.models import Site
from celery.task import Task
from celery.registry import tasks
from bitlyapi import BitLy
from twitter import Api
class Tweet(Task):
def run(self, object, **kwargs):
twitter = Api(settings.TWITTER_USER, settings.TWITTER_PASSWORD)
bitly = BitLy(settings.BITLY_USER, settings.BITLY_API_KEY)
site = Site.objects.get(id=settings.SITE_ID)
domain = site.domain
path = object.get_absolute_url()
long_url = "http://%s%s" % (domain, path)
bitly_result = bitly.shorten(longUrl=long_url)
short_url = bitly_result['url']
object_type = object._meta.verbose_name.title()
title = object.title
text = "New %s - %s - %s" % (object_type, title, short_url)
msg_len = len(text)
if msg_len > 140:
diff = msg_len - 140
title = title[0:-diff-3]
title += "..."
text = "New %s - %s - %s" % (object_type, title, short_url)
twitter.PostUpdate(text)
tasks.register(Tweet)
@Apreche
Copy link
Author

Apreche commented May 30, 2010

This is an example of a Django/Celery task that will tweet out a link to a new episode.

@Apreche
Copy link
Author

Apreche commented May 30, 2010

It now tweets any instance of a django model that has a title field

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment