Different URL shortening system
authorCharles Connell <charles@connells.org>
Mon, 23 Dec 2013 22:55:25 +0000 (17:55 -0500)
committerCharles Connell <charles@connells.org>
Mon, 23 Dec 2013 22:55:25 +0000 (17:55 -0500)
karmaworld/apps/notes/tasks.py
reqs/common.txt

index b627ccf71d7ec5427202e1ebf2ba2ec474836d1a..16cb43837e0f86b9daefdb654792e520cb9de6e6 100644 (file)
@@ -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
-
index 850d8b98c94ff761d0377de5aa4e8dc640de6ed7..bc168eef7adc8776aa1f58df62597eb82f4ac6c1 100644 (file)
@@ -17,4 +17,4 @@ requests
 beautifulsoup4
 pyopenssl
 python-twitter
-pyshorteners
+gdshortener