Support for shorten and unshorten URL's on tweets has been added

This commit is contained in:
Manuel Cortez 2014-11-15 19:40:19 -06:00
parent 05dc8e5a31
commit cd0aab90ef
3 changed files with 58 additions and 4 deletions

View File

@ -1,18 +1,21 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import widgetUtils import widgetUtils
import output import output
from wxUI.dialogs import message import url_shortener
import sound
from wxUI.dialogs import message, urlList
from extra import translator from extra import translator
from twitter import utils
class tweet(object): class tweet(object):
def __init__(self, session): def __init__(self, session):
super(tweet, self).__init__() super(tweet, self).__init__()
self.message = message.tweet(_(u"Write the tweet here"), _(u"tweet - 0 characters"), "") self.message = message.tweet(_(u"Write the tweet here"), _(u"tweet - 0 characters"), "")
# widgetUtils.connect_event(self.message.upload_image, widgetUtils.BUTTON_PRESSED, self.upload_image)
# widgetUtils.connect_event(self.message.spellcheck, widgetUtils.BUTTON_PRESSED, self.spellcheck) # widgetUtils.connect_event(self.message.spellcheck, widgetUtils.BUTTON_PRESSED, self.spellcheck)
# widgetUtils.connect_event(self.message.attach, widgetUtils.BUTTON_PRESSED, self.attach) # widgetUtils.connect_event(self.message.attach, widgetUtils.BUTTON_PRESSED, self.attach)
# widgetUtils.connect_event(self.message.shortenButton, widgetUtils.BUTTON_PRESSED, self.shorten) widgetUtils.connect_event(self.message.text, widgetUtils.ENTERED_TEXT, self.text_processor)
# widgetUtils.connect_event(self.message.unshortenButton, widgetUtils.BUTTON_PRESSED, self.unshorten) widgetUtils.connect_event(self.message.shortenButton, widgetUtils.BUTTON_PRESSED, self.shorten)
widgetUtils.connect_event(self.message.unshortenButton, widgetUtils.BUTTON_PRESSED, self.unshorten)
widgetUtils.connect_event(self.message.translateButton, widgetUtils.BUTTON_PRESSED, self.translate) widgetUtils.connect_event(self.message.translateButton, widgetUtils.BUTTON_PRESSED, self.translate)
def translate(self, event=None): def translate(self, event=None):
@ -29,3 +32,42 @@ class tweet(object):
output.speak(_(u"Translated")) output.speak(_(u"Translated"))
else: else:
return return
def shorten(self, event=None):
urls = utils.find_urls_in_text(self.message.get_text())
if len(urls) == 0:
output.speak(_(u"There's no URL to be shortened"))
elif len(urls) == 1:
self.message.set_text(self.message.get_text().replace(urls[0], url_shortener.shorten(urls[0])))
output.speak(_(u"URL shortened"))
elif len(urls) > 1:
list_urls = urlList.urlList()
list_urls.populate_list(urls)
if list_urls.get_response() == widgetUtils.OK:
self.message.set_text(self.message.get_text().replace(urls[list_urls.get_item()], url_shortener.shorten(list_urls.get_string())))
output.speak(_(u"URL shortened"))
def unshorten(self, event=None):
urls = utils.find_urls_in_text(self.message.get_text())
if len(urls) == 0:
output.speak(_(u"There's no URL to be sexpanded"))
elif len(urls) == 1:
self.message.set_text(self.message.get_text().replace(urls[0], url_shortener.unshorten(urls[0])))
output.speak(_(u"URL expanded"))
elif len(urls) > 1:
list_urls = urlList.urlList()
list_urls.populate_list(urls)
if list_urls.get_response() == widgetUtils.OK:
self.message.set_text(self.message.get_text().replace(urls[list_urls.get_item()], url_shortener.unshorten(list_urls.get_string())))
output.speak(_(u"URL expanded"))
def text_processor(self, event=None):
self.message.set_title("%s of 140 characters" % (len(self.message.get_text())))
if len(self.message.get_text()) > 1:
self.message.enable_button("shortenButton")
self.message.enable_button("unshortenButton")
else:
self.message.disable_button("shortenButton")
self.message.disable_button("unshortenButton")
if len(self.message.get_text()) > 140:
sound.player.play("max_length.ogg")

View File

@ -10,6 +10,7 @@ NO = wx.ID_NO
#events #events
CLOSE_EVENT = wx.EVT_CLOSE CLOSE_EVENT = wx.EVT_CLOSE
BUTTON_PRESSED = wx.EVT_BUTTON BUTTON_PRESSED = wx.EVT_BUTTON
ENTERED_TEXT = wx.EVT_TEXT
def exit_application(): def exit_application():
wx.GetApp().ExitMainLoop() wx.GetApp().ExitMainLoop()

View File

@ -27,6 +27,17 @@ class textLimited(baseDialog.BaseWXDialog):
def set_text(self, text): def set_text(self, text):
return self.text.ChangeValue(text) return self.text.ChangeValue(text)
def set_title(self, new_title):
return self.SetTitle(new_title)
def enable_button(self, buttonName):
if getattr(self, buttonName):
return getattr(self, buttonName).Enable()
def disable_button(self, buttonName):
if getattr(self, buttonName):
return getattr(self, buttonName).Disable()
def onSelect(self, ev): def onSelect(self, ev):
self.text.SelectAll() self.text.SelectAll()