Created
May 30, 2010 19:23
-
-
Save Apreche/419244 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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) |
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
This is an example of a Django/Celery task that will tweet out a link to a new episode.