from celery import task
from karmaworld.apps.notes.models import Note
-from karmaworld.secret.twitter import *
import twitter
import logging
-from pyshorteners.shorteners import Shortener
+import gdshortener
logger = logging.getLogger(__name__)
+
@task(name="tweet_note")
def tweet_note():
"""Tweet about a new note."""
- api = twitter.Api(consumer_key=CONSUMER_KEY,
- consumer_secret=CONSUMER_SECRET,
- access_token_key=ACCESS_TOKEN_KEY,
- access_token_secret=ACCESS_TOKEN_SECRET)
+ try:
+ import karmaworld.secret.twitter as secrets
+ except ImportError:
+ logger.warn("No twitter secrets found, not running tweet_note")
+ return
+
+ api = twitter.Api(consumer_key=secrets.CONSUMER_KEY,
+ consumer_secret=secrets.CONSUMER_SECRET,
+ access_token_key=secrets.ACCESS_TOKEN_KEY,
+ access_token_secret=secrets.ACCESS_TOKEN_SECRET)
newest_notes = Note.objects.all()[:100]
for n in newest_notes:
if not n.tweeted:
update = tweet_string(n)
- logger.info("Tweeting:")
- logger.info(update)
+ logger.info("Tweeting: " + update)
# Mark this tweeted before we actually tweet it
# to be extra safe against double tweets
def tweet_string(note):
- # This url will use 13 characters
- shortener = Shortener('GoogleShortener')
+ # This url will use 12 or less characters
+ shortener = gdshortener.VGDShortener()
url = "https://www.karmanotes.org" + \
note.get_absolute_url()
- short_url = shortener.short(url)
+ short_url = shortener.shorten(url)[0]
# space character
# space and colon characters
# 57 characters
- short_note = note.name[:57]
+ short_note = note.name[:58]
return short_url + " #" + short_school + " " + \
short_course + ": " + \
short_note
-