Beispiel #1
0
        private async Task <List <CommentViewModel> > RecursePost(List <CommentViewModel> retrievedComments, string screen_name, string tweet_id, string since_id, string access_token, string access_token_secret)
        {
            var fields = new Dictionary <string, string>
            {
                { "screen_name", screen_name },
                { "since_id", since_id },
                { "count", "200" }
            };

            string response = await _client.GetAsync("/1.1/statuses/user_timeline.json",
                                                     Utils.GetQueryString(fields), TwitterOAuthAuthenticator.GetOAuthAuthenticator(fields, access_token, access_token_secret));

            JArray jsonResponse = JArray.Parse(response);

            if (jsonResponse.Count == 0)
            {
                return(retrievedComments);
            }

            List <CommentViewModel> comments = jsonResponse
                                               .Where(tweet => tweet["in_reply_to_status_id"].ToString().Equals(tweet_id))
                                               .Select(comment => ParseReply(comment.ToString()))
                                               .ToList();

            if (jsonResponse.Count < 200)
            {
                return(comments);
            }

            CommentViewModel lastComment = ParseReply(jsonResponse.FirstOrDefault().ToString());

            comments.AddRange(retrievedComments);

            return(await RecursePost(comments, screen_name, tweet_id, lastComment.Id, access_token, access_token_secret));
        }
Beispiel #2
0
        public async Task <SocialMediaAccountViewModel> GetProfileInfoAsync(string screen_name, string access_token, string access_token_secret)
        {
            var fields = new Dictionary <string, string>
            {
                { "screen_name", screen_name }
            };

            string response = await _client.GetAsync("/1.1/users/show.json",
                                                     Utils.GetQueryString(fields), TwitterOAuthAuthenticator.GetOAuthAuthenticator(fields, access_token, access_token_secret));

            JObject jsonResponse = JObject.Parse(response);

            return(new SocialMediaAccountViewModel
            {
                Id = jsonResponse["screen_name"].ToString(),
                Name = $"@{jsonResponse["screen_name"].ToString()}",
                PictureUrl = jsonResponse["profile_image_url"].ToString().Replace("normal", "bigger"),
                Type = "twitter"
            });
        }
Beispiel #3
0
        public async Task <string> DeleteTweetAsync(string tweet_id, string access_token, string access_token_secret)
        {
            var payload = new Dictionary <string, string>
            {
                { "id", tweet_id }
            };

            return(await _client.PostAsync($"/1.1/statuses/destroy/{tweet_id}.json", payload, TwitterOAuthAuthenticator.GetOAuthAuthenticator(payload, access_token, access_token_secret)));
        }
Beispiel #4
0
        public async Task <CommentViewModel> CreatePostCommentAsync(string tweet_id, string message, string authorUsername, string access_token, string access_token_secret)
        {
            var escapedMessage = Uri.EscapeDataString(message);

            var payload = new Dictionary <string, string>
            {
                { "status", message },
                { "in_reply_to_status_id", tweet_id }
            };

            string response = await _client.PostAsync("/1.1/statuses/update.json", payload, TwitterOAuthAuthenticator.GetOAuthAuthenticator(payload, access_token, access_token_secret));

            CommentViewModel comment = ParseReply(response);

            return(comment);
        }
Beispiel #5
0
        public async Task <Tuple <List <PostViewModel>, string> > GetPostedPostsAsync(string screen_name, string access_token, string access_token_secret, int count = 0, string max_id = "")
        {
            Dictionary <string, string> fields = new Dictionary <string, string>
            {
                { "screen_name", screen_name }
            };

            if (count != 0)
            {
                fields.Add("count", $"{count}");
                if (max_id != "")
                {
                    fields.Add("max_id", max_id);
                }
            }

            string response = await _client.GetAsync("/1.1/statuses/user_timeline.json", Utils.GetQueryString(fields), TwitterOAuthAuthenticator.GetOAuthAuthenticator(fields, access_token, access_token_secret));

            JArray jsonResponse = JArray.Parse(response);

            List <PostViewModel> allPosts = jsonResponse
                                            .Select(post => ParseTweet(post.ToString()))
                                            .ToList();

            List <PostViewModel> posts = jsonResponse
                                         .Where(post => post["in_reply_to_status_id"].ToString() == "")
                                         .Select(post => ParseTweet(post.ToString()))
                                         .ToList();
            string newMaxIdString = allPosts.Count != 0 ?
                                    long.TryParse(allPosts.LastOrDefault().Id, out long newMaxId) ? Convert.ToString(--newMaxId) : "" :
                                    "";

            return(Tuple.Create(posts, newMaxIdString));
        }
Beispiel #6
0
        public async Task <PostViewModel> CreatePostAsync(string message, List <MemoryStream> images, string access_token, string access_token_secret)
        {
            Dictionary <string, string> payload = new Dictionary <string, string>
            {
                { "status", message }
            };

            if (images.Count != 0)
            {
                IEnumerable <Task <string> > uploadTasks = images
                                                           .Take(4)
                                                           .Select(image => UploadFileAsync(image, access_token, access_token_secret));

                string[] imageIds = await Task.WhenAll(uploadTasks.ToArray());

                string media_ids = String.Join(",", imageIds);
                payload.Add("media_ids", media_ids);
            }

            string response = await _client.PostAsync("/1.1/statuses/update.json", payload, TwitterOAuthAuthenticator.GetOAuthAuthenticator(payload, access_token, access_token_secret));

            PostViewModel post = ParseTweet(response);

            return(post);
        }
Beispiel #7
0
        public async Task <string> UploadFileAsync(MemoryStream file, string access_token, string access_token_secret)
        {
            string response;

            using (var content = new MultipartFormDataContent())
            {
                byte[] fileData = file.ToArray();
                //using (BinaryReader binaryReader = new BinaryReader(file.InputStream))
                //{
                //    fileData = binaryReader.ReadBytes(file.ContentLength);
                //}

                content.Add(new ByteArrayContent(fileData), "media", "upload");

                response = await _client.PostFileAsync(@"https://upload.twitter.com/1.1/media/upload.json", content, TwitterOAuthAuthenticator.GetOAuthAuthenticator(new Dictionary <string, string>(), access_token, access_token_secret));
            }

            return(JObject.Parse(response).SelectToken("media_id_string").ToString());
        }