From 79f8b9f6533fee18550762c09da77ea16637f40b Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Mon, 23 Dec 2013 17:55:25 -0500 Subject: [PATCH] Different URL shortening system --- karmaworld/apps/notes/tasks.py | 30 +++++++++++++++++------------- reqs/common.txt | 2 +- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/karmaworld/apps/notes/tasks.py b/karmaworld/apps/notes/tasks.py index b627ccf..16cb438 100644 --- a/karmaworld/apps/notes/tasks.py +++ b/karmaworld/apps/notes/tasks.py @@ -4,28 +4,33 @@ 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 @@ -38,11 +43,11 @@ def tweet_note(): 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 @@ -58,9 +63,8 @@ def tweet_string(note): # 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 - diff --git a/reqs/common.txt b/reqs/common.txt index 850d8b9..bc168ee 100644 --- a/reqs/common.txt +++ b/reqs/common.txt @@ -17,4 +17,4 @@ requests beautifulsoup4 pyopenssl python-twitter -pyshorteners +gdshortener -- 2.25.1