Пример #1
0
        /// <summary>
        /// Searchs for the tweets containing a specific search string.
        /// </summary>
        /// <param name="tweetText">The search string.</param>
        public async void SearchTweets(string tweetText)
        {
            if (!this.authorizer.IsAuthorized)
            {
                await this.authorizer.AuthorizeAsync();

                this.context = new TwitterContext(authorizer);
            }

            string roamingFolder = ApplicationData.Current.RoamingFolder.Path + "\\";

            var searchResult =
                (from search in context.Search
                 where search.Type == SearchType.Search &&
                 search.Query == tweetText
                 select search)
                .SingleOrDefault();

            foreach (var result in searchResult.Statuses)
            {
                bool                alreadyExists = false;
                bool                fileExists    = true;
                HttpClient          http          = new HttpClient();
                HttpResponseMessage response      = await http.GetAsync(result.User.ProfileImageUrl);

                string userAvatarName = result.User.Identifier.UserID + Path.GetExtension(result.User.ProfileImageUrl);

                Tweets[userAvatarName] = new TweetInfo("@" + result.User.Identifier.ScreenName, result.Text);

                try
                {
                    StorageFile existingFile = await ApplicationData.Current.RoamingFolder.GetFileAsync(userAvatarName);
                }
                catch (FileNotFoundException f)
                {
                    fileExists = false;
                }

                if (!fileExists)
                {
                    using (NativeFileStream file = new NativeFileStream(roamingFolder + userAvatarName, NativeFileMode.Create, NativeFileAccess.Write))
                    {
                        await response.Content.CopyToAsync(file.AsOutputStream().AsStreamForWrite());
                    }
                }

                for (int i = 0; i < AvatarTextures.Count; i++)
                {
                    if (AvatarTextures[i].TexturePath == userAvatarName)
                    {
                        alreadyExists = true;
                        break;
                    }
                }

                if (!alreadyExists)
                {
                    AddTextureFromFile(userAvatarName);
                }
            }
        }