Example #1
0
        private void findElementsInText(string fullText)
        {
            if (parsingOfTextItemsInProgress)
            {
                return;
            }
            if (string.IsNullOrEmpty(fullText))
            {
                return;
            }
            parsingOfTextItemsInProgress = true;
            try
            {
                string twitlongerLink      = "";
                string twitlongerShortLink = "";
                List <TextSubTypes.ISubType> foundElements = new List <TextSubTypes.ISubType>();
                string[] words = Regex.Split(fullText, @"([\r\n \(\)\{\}\[\];])");
                foreach (string word in words)
                {
                    twitlongerLink = "";
                    if (word.ToLower().StartsWith("http://") || word.ToLower().StartsWith("https://"))
                    {
                        string url          = word;
                        string expandedLink = word;

                        if (AppController.Current.AllShortenedLinksInItems.ContainsKey(word))
                        {
                            //AppController.Current.Logger.writeToLogfile("Cached shortened link found for " + word);
                            expandedLink = AppController.Current.AllShortenedLinksInItems[word];
                        }
                        else
                        {
                            //AppController.Current.Logger.writeToLogfile("No cached shortened link found for " + word);
                            // tl.gd wird sonst zu www.twitlonger.com/show expandiert :(
                            if (!word.StartsWith("http://tl.gd/") && 1 == 2)
                            {
                                foreach (API.UrlShortener.ILinkShortener shortener in AppController.Current.AlllinkShortenerServices)
                                {
                                    if (shortener.IsLinkOfThisShortener(word))
                                    {
                                        string unshortedLink = shortener.ExpandLink(word);
                                        if (!string.IsNullOrEmpty(unshortedLink) && unshortedLink != word)
                                        {
                                            expandedLink = unshortedLink;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        if (expandedLink.StartsWith("http://tl.gd/") || expandedLink.StartsWith("http://www.twitlonger.com/show/"))
                        {
                            twitlongerLink      = expandedLink;
                            twitlongerShortLink = word;
                        }

                        if (AppController.Current.AllImagesInItems.ContainsKey(expandedLink))
                        {
                            TextSubTypes.ImageLink imageLink = new TextSubTypes.ImageLink(expandedLink, word, AppController.Current.AllImagesInItems[expandedLink]);
                            foundElements.Add(imageLink);
                        }
                        else
                        {
                            TextSubTypes.Link currentLink = new TextSubTypes.Link(expandedLink, word);
                            foundElements.Add(currentLink);
                        }
                    }
                    else if (word.StartsWith("#") && word.Length > 1)
                    {
                        TextSubTypes.HashTag hashTag = new TextSubTypes.HashTag(word);
                        foundElements.Add(hashTag);
                    }
                    else if (word.StartsWith("@") && word.Length > 1)
                    {
                        TextSubTypes.User user = new TextSubTypes.User(word);
                        foundElements.Add(user);
                    }
                    else
                    {
                        TextSubTypes.Text currentText = new TextSubTypes.Text(word);
                        foundElements.Add(currentText);
                    }
                }
                if (twitlongerLink != "")
                {
                    fullText = fullText.Replace(twitlongerShortLink, twitlongerLink);
                    ExternalServices.Twitlonger.TwitLongerResponse response = ExternalServices.Twitlonger.GetLongText(fullText);
                    if (response != null)
                    {
                        if (!string.IsNullOrEmpty(response.MessageText))
                        {
                            IsTwitLongerItem = true;
                            fullText         = response.MessageText;
                            findElementsInText(fullText);
                            return;
                        }
                    }
                }
                ElementsInText.Clear();
                foreach (TextSubTypes.ISubType element in foundElements)
                {
                    ElementsInText.Add(element);
                }
                _text = fullText;
                parsingOfTextItemsInProgress = false;
            }
            catch (Exception exp)
            {
                AppController.Current.Logger.writeToLogfile(exp, true);
                parsingOfTextItemsInProgress = false;
            }
        }
Example #2
0
        private void buttonSendTweet_Click(object sender, RoutedEventArgs e)
        {
            if ((textBoxTweet.Text.Length > 0 && characters_left >= 0) || (characters_left < 0 && useTwitlonger))
            {
                AccountTwitter account;
                if (AppController.Current.AllAccounts.Count == 1)
                {
                    account = AppController.Current.AllAccounts[0] as AccountTwitter;
                }
                else
                {
                    account = comboBoxAccount.selectedAccount;
                }


                if (account != null)
                {
                    string      text = textBoxTweet.Text;
                    TwitterItem sentItem;
                    ExternalServices.Twitlonger.TwitLongerResponse twitLongerResponse = new ExternalServices.Twitlonger.TwitLongerResponse();

                    this.Dispatcher.BeginInvoke(new Action(() => this.progressBarUploading.BusyContent = "Sending tweet..."));
                    this.Dispatcher.BeginInvoke(new Action(() => this.progressBarUploading.Visibility  = Visibility.Visible));

                    /* if (Properties.Settings.Default.AutomaticUrlShortening)
                     * {
                     *   text = AppController.Current.ActualLinkShortener.ShortenAllLinksInText(textBoxTweet.Text);
                     * } */

                    if (useTwitlonger)
                    {
                        twitLongerResponse = ExternalServices.Twitlonger.Send(text, account);
                        if (!string.IsNullOrEmpty(twitLongerResponse.ErrorMessage))
                        {
                            MessageBox.Show(twitLongerResponse.ErrorMessage, "Sending to Twitlonger failed");
                            return;
                        }
                        else
                        {
                            text = twitLongerResponse.MessageText;
                            textBoxTweet.Text = text;
                        }
                    }

                    if (directMessageReceiver != null)
                    {
                        sentItem = AppController.Current.writeDirectMessage(account, text, directMessageReceiver);
                    }
                    else if (inReplyToId <= 0)
                    {
                        sentItem = AppController.Current.writeNewTweet(account, text, upload_image_url);
                    }
                    else
                    {
                        sentItem = AppController.Current.replyToTweet(account, text, inReplyToId, upload_image_url);
                    }

                    if (sentItem != null)
                    {
                        if (!string.IsNullOrEmpty(twitLongerResponse.MessageId))
                        {
                            ExternalServices.Twitlonger.IdCallback(sentItem.Id, twitLongerResponse.MessageId);
                        }
                        if (toBeEditedItem != null)
                        {
                            toBeEditedItem.DeleteThisTweet();
                        }
                        Close();
                    }
                    this.Dispatcher.BeginInvoke(new Action(() => this.progressBarUploading.Visibility = Visibility.Collapsed));
                }
            }
        }
        public static TwitterItem  getItemFromStatus(TwitterStatus status, AccountTwitter account)
        {
            if (status == null)
            {
                return(null);
            }
            try
            {
                TwitterItem item = new TwitterItem();
                item.RetrievingAccount = account;
                string text;
                if (ExternalServices.Twitlonger.IsTwitLongerText(Functions.decodeHtml(status.Text)))
                {
                    ExternalServices.Twitlonger.TwitLongerResponse twitLongerResponse = ExternalServices.Twitlonger.GetLongText(Functions.decodeHtml(status.Text));
                    text = twitLongerResponse.MessageText;
                    item.IsTwitLongerItem = true;
                }
                else
                {
                    text = Functions.decodeHtml(status.Text);
                }

                if (string.IsNullOrEmpty(text))
                {
                    return(null);
                }

                try
                {
                    if (AppController.Current.AllTwitterAccounts.Where(a => a.Login.Id == status.User.Id).Count() > 0)
                    {
                        Nymphicus.Model.AccountTwitter ownaccount = AppController.Current.AllTwitterAccounts.Where(a => a.Login.Id == status.User.Id).First();
                        item.OwnAccountHavingWrittenThisTweet = ownaccount;
                    }
                }
                catch { }

                if (item.Author == null)
                {
                    item.Author = getPersonFromUser(status.User, account);
                    if (item.Author != null)
                    {
                        try
                        {
                            AppController.Current.AllPersons.Add(item.Author);
                        }
                        catch { }
                    }
                }
                item.Id = status.Id;


                item.SourceString = status.Source;
                item.CreatedAt    = status.CreatedDate.ToLocalTime();

                item.isFavorited = status.IsFavorited;

                item.Entities = status.Entities;
                if (status.InReplyToStatusId.HasValue)
                {
                    item.InReplyToStatusId = status.InReplyToStatusId.Value;
                }
                if (item.Entities != null)
                {
                    if (item.Entities.Urls != null)
                    {
                        foreach (TwitterUrl urlEntity in item.Entities.Urls)
                        {
                            if (!string.IsNullOrEmpty(urlEntity.ExpandedValue))
                            {
                                text = text.Replace(urlEntity.Value, urlEntity.ExpandedValue);
                                if (!AppController.Current.AllShortenedLinksInItems.ContainsKey(urlEntity.Value))
                                {
                                    try
                                    {
                                        AppController.Current.AllShortenedLinksInItems.Add(urlEntity.Value, urlEntity.ExpandedValue);
                                    }
                                    catch
                                    {
                                        // as we are asynchron there might be another thread having added it already...
                                    }
                                }
                            }
                        }
                    }

                    if (item.Entities.Media != null)
                    {
                        foreach (TwitterMedia mediaEntity in item.Entities.Media)
                        {
                            if (mediaEntity.MediaType == TwitterMediaType.Photo)
                            {
                                if (!string.IsNullOrEmpty(mediaEntity.ExpandedUrl))
                                {
                                    text = text.Replace(mediaEntity.Url, mediaEntity.ExpandedUrl);
                                    TwitterItem.embedded_image image = new TwitterItem.embedded_image();
                                    image.url           = mediaEntity.ExpandedUrl;
                                    image.thumbnail_url = mediaEntity.MediaUrl;
                                    item.imagesInPost.Add(image);
                                    if (!AppController.Current.AllImagesInItems.ContainsKey(mediaEntity.ExpandedUrl))
                                    {
                                        AppController.Current.AllImagesInItems.Add(mediaEntity.ExpandedUrl, mediaEntity.MediaUrl);
                                    }
                                }
                            }
                        }
                    }
                    if (item.Entities.HashTags != null)
                    {
                        foreach (TwitterHashTag hashEntity in item.Entities.HashTags)
                        {
                            if (hashEntity != null)
                            {
                                if (!AppController.Current.AllKnownHashtags.Contains("#" + hashEntity.Text))
                                {
                                    AppController.Current.AllKnownHashtags.Add("#" + hashEntity.Text);
                                }
                            }
                        }
                    }
                }
                item.Text = text;
                if (status.Place != null)
                {
                    Geo geo = new Geo();
                    geo.CityOrEqual = status.Place.FullName;
                    geo.StreetName  = status.Place.Name;
                    item.Place      = geo;
                }
                if (status.RetweetedStatus != null)
                {
                    item.RetweetedItem = getItemFromStatus(status.RetweetedStatus, account);
                    if (item.RetweetedItem != null)
                    {
                        if (AppController.Current.AllAccounts.Where(saccount => saccount.Id == status.User.Id).Count() > 0)
                        {
                            item.isRetweetedByMe = true;
                        }
                        else
                        {
                            item.isRetweetedToMe = true;
                        }
                    }
                }


                if (item.Author.ProfileBackgroundColorString != "")
                {
                    //  item.BackgroundColor = "#" + item.Author.ProfileBackgroundColorString;
                }
                return(item);
            }
            catch
            {
                return(null);
            }
        }