diff --git a/src/controller/buffers/twitterBuffers.py b/src/controller/buffers/twitterBuffers.py index c46cb091..8735f3ed 100644 --- a/src/controller/buffers/twitterBuffers.py +++ b/src/controller/buffers/twitterBuffers.py @@ -135,10 +135,7 @@ class baseBufferController(baseBuffers.buffer): message = tweet["message"] try: tweet = self.session.twitter.show_status(id=tweet_id, include_ext_alt_text=True, tweet_mode="extended") - urls = utils.find_urls_in_text(tweet["full_text"]) - for url in range(0, len(urls)): - try: tweet["full_text"] = tweet["full_text"].replace(urls[url], tweet["entities"]["urls"][url]["expanded_url"]) - except IndexError: pass + tweet["full_text"] = utils.expand_urls(tweet["full_text"], tweet["entities"]) except TwythonError as e: utils.twitter_error(e) return @@ -149,10 +146,7 @@ class baseBufferController(baseBuffers.buffer): tweetsList.append(tweet) try: tweet = self.session.twitter.show_status(id=l, include_ext_alt_text=True, tweet_mode="extended") - urls = utils.find_urls_in_text(tweet["full_text"]) - for url in range(0, len(urls)): - try: tweet["full_text"] = tweet["full_text"].replace(urls[url], tweet["entities"]["urls"][url]["expanded_url"]) - except IndexError: pass + tweet["full_text"] = utils.find_urls_in_text(tweet["full_text"], tweet["entities"]) except TwythonError as e: utils.twitter_error(e) return @@ -613,23 +607,12 @@ class baseBufferController(baseBuffers.buffer): if hasattr(dlg, "destroy"): dlg.destroy() def get_quoted_tweet(self, tweet): -# try: quoted_tweet = self.session.twitter.show_status(id=tweet["id"]) - urls = utils.find_urls_in_text(quoted_tweet["text"]) - for url in range(0, len(urls)): - try: quoted_tweet["text"] = quoted_tweet["text"].replace(urls[url], quoted_tweet["entities"]["urls"][url]["expanded_url"]) - except IndexError: pass -# except TwythonError as e: -# utils.twitter_error(e) -# return + quoted_tweet["text"] = utils.find_urls_in_text(quoted_tweet["text"], quoted_tweet["entities"]) l = tweets.is_long(quoted_tweet) id = tweets.get_id(l) -# try: original_tweet = self.session.twitter.show_status(id=id) - urls = utils.find_urls_in_text(original_tweet["text"]) - for url in range(0, len(urls)): - try: original_tweet["text"] = original_tweet["text"].replace(urls[url], original_tweet["entities"]["urls"][url]["expanded_url"]) - except IndexError: pass + original_tweet["text"] = utils.find_urls_in_text(original_tweet["text"], original_tweet["entities"]) return compose.compose_quoted_tweet(quoted_tweet, original_tweet, self.session.db, self.session.settings["general"]["relative_times"]) def open_in_browser(self, *args, **kwargs): diff --git a/src/controller/user.py b/src/controller/user.py index 665c5ad5..9fb07388 100644 --- a/src/controller/user.py +++ b/src/controller/user.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals -from builtins import object import wx import webbrowser import widgetUtils @@ -9,6 +7,7 @@ from wxUI.dialogs import update_profile, show_user import logging log = logging.getLogger("controller.user") from twython import TwythonError +from sessions.twitter import utils class profileController(object): def __init__(self, session, user=None): @@ -101,9 +100,7 @@ class profileController(object): string = string+ _(u"URL: %s\n") % (self.data["entities"]["url"]["urls"][0]["expanded_url"]) if self.data["description"] != "": if self.data["entities"].get("description") != None and self.data["entities"]["description"].get("urls"): - for url in self.data["entities"]["description"]["urls"]: - if url["url"] in self.data["description"]: - self.data["description"] = self.data["description"].replace(url["url"], url["expanded_url"]) + self.data["description"] = utils.expand_urls(self.data["description"], self.data["entities"]["description"]) string = string+ _(u"Bio: %s\n") % (self.data["description"]) if self.data["protected"] == True: protected = _(u"Yes") else: protected = _(u"No") diff --git a/src/sessions/twitter/compose.py b/src/sessions/twitter/compose.py index fa345e1f..3b7abdaf 100644 --- a/src/sessions/twitter/compose.py +++ b/src/sessions/twitter/compose.py @@ -69,17 +69,11 @@ def compose_tweet(tweet, db, relative_times, show_screen_names=False, session=No else: text = "RT @%s: %s" % (tweet["retweeted_status"]["user"]["screen_name"], text) if ("message" in tweet) == False: - urls = utils.find_urls_in_text(text) + if "retweeted_status" in tweet: - for url in range(0, len(urls)): - try: - text = text.replace(urls[url], tweet["retweeted_status"]["entities"]["urls"][url]["expanded_url"]) - except: pass + text = utils.expand_urls(text, tweet["retweeted_status"]["entities"]) else: - for url in range(0, len(urls)): - try: - text = text.replace(urls[url], tweet["entities"]["urls"][url]["expanded_url"]) - except: pass + text = utils.expand_urls(text, tweet["entities"]) if config.app['app-settings']['handle_longtweets']: pass return [user+", ", text, ts+", ", source] @@ -110,10 +104,7 @@ def compose_direct_message(item, db, relative_times, show_screen_names=False, se else: user = sender["name"] if text[-1] in chars: text=text+"." - urls = utils.find_urls_in_text(text) - for url in range(0, len(urls)): - try: text = text.replace(urls[url], item["message_create"]["message_data"]["entities"]["urls"][url]["expanded_url"]) - except IndexError: pass + text = utils.expand_urls(text, item["message_create"]["message_data"]["entities"]) return [user+", ", text, ts+", ", source] def compose_quoted_tweet(quoted_tweet, original_tweet, show_screen_names=False, session=None): diff --git a/src/sessions/twitter/session.py b/src/sessions/twitter/session.py index 30d01939..ee2254c8 100644 --- a/src/sessions/twitter/session.py +++ b/src/sessions/twitter/session.py @@ -359,10 +359,7 @@ class Session(base.baseSession): value = "full_text" else: value = "text" - urls = utils.find_urls_in_text(quoted_tweet[value]) - for url in range(0, len(urls)): - try: quoted_tweet[value] = quoted_tweet[value].replace(urls[url], quoted_tweet["entities"]["urls"][url]["expanded_url"]) - except IndexError: pass + quoted_tweet[value] = utils.expand_urls(quoted_tweet[value], quoted_tweet["entities"]) if "quoted_status" in quoted_tweet: original_tweet = quoted_tweet["quoted_status"] elif "retweeted_status" in quoted_tweet and "quoted_status" in quoted_tweet["retweeted_status"]: @@ -370,17 +367,13 @@ class Session(base.baseSession): else: return quoted_tweet original_tweet = self.check_long_tweet(original_tweet) - if "full_text" in original_tweet: value = "full_text" elif "message" in original_tweet: value = "message" else: value = "text" - urls = utils.find_urls_in_text(original_tweet[value]) - for url in range(0, len(urls)): - try: original_tweet[value] = original_tweet[value].replace(urls[url], original_tweet["entities"]["urls"][url]["expanded_url"]) - except IndexError: pass + original_tweet[value] = utils.expand_urls(original_tweet[value], original_tweet["entities"]) return compose.compose_quoted_tweet(quoted_tweet, original_tweet) def check_long_tweet(self, tweet): diff --git a/src/sessions/twitter/utils.py b/src/sessions/twitter/utils.py index a9c88392..ff4db939 100644 --- a/src/sessions/twitter/utils.py +++ b/src/sessions/twitter/utils.py @@ -208,4 +208,12 @@ def twitter_error(error): msg = _(u"No status found with that ID") else: msg = _(u"Error code {0}").format(error.error_code,) - output.speak(msg) \ No newline at end of file + output.speak(msg) + +def expand_urls(text, entities): + """ Expand all URLS present in text with information found in entities""" + urls = find_urls_in_text(text) + for url in entities["urls"]: + if url["url"] in text: + text = text.replace(url["url"], url["expanded_url"]) + return text