3 # Copyright (C) 2013 FinalsClub Foundation
5 from celery import task
6 from karmaworld.apps.notes.models import Note
11 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()[: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 12 or less characters
47 shortener = gdshortener.VGDShortener()
48 url = "https://www.karmanotes.org" + \
49 note.get_absolute_url()
50 short_url = shortener.shorten(url)[0]
55 school = note.course.school.slug
56 short_school = school[:school.find('-')][:16]
61 short_course = note.course.name[:50]
63 # space and colon characters
66 short_note = note.name[:58]
68 return short_url + " #" + short_school + " " + \
69 short_course + ": " + \