3 # Copyright (C) 2013 FinalsClub Foundation
5 from celery import task
6 from karmaworld.apps.notes.models import Note
12 logger = logging.getLogger(__name__)
14 @task(name="tweet_note")
16 """Tweet about a new note."""
19 import karmaworld.secret.twitter as secrets
21 logger.warn("No twitter secrets found, not running tweet_note")
24 api = twitter.Api(consumer_key=secrets.CONSUMER_KEY,
25 consumer_secret=secrets.CONSUMER_SECRET,
26 access_token_key=secrets.ACCESS_TOKEN_KEY,
27 access_token_secret=secrets.ACCESS_TOKEN_SECRET)
29 newest_notes = Note.objects.all().order_by('-uploaded_at')[:100]
30 for n in newest_notes:
32 update = tweet_string(n)
33 logger.info("Tweeting: " + update)
35 # Mark this tweeted before we actually tweet it
36 # to be extra safe against double tweets
40 api.PostUpdate(tweet_string(n))
45 def tweet_string(note):
46 # This url will use 13 or less characters
47 shortener = gdshortener.ISGDShortener()
48 url = "http://www.karmanotes.org" + \
49 note.get_absolute_url()
50 short_url = shortener.shorten(url)[0]
53 short_course = note.course.name[:50]
56 short_note = note.name[:57]
58 if note.course.school.hashtag:
59 return short_url + " #" + note.course.school.hashtag + " " + \
60 short_course + ": " + \
63 return short_url + " " + \
64 short_course + ": " + \