Пример #1
0
 public IObservable<PostResult> Send()
 {
     var existedTags = TwitterRegexPatterns.ValidHashtag.Matches(Text)
                                           .OfType<Match>()
                                           .Select(_ => _.Groups[1].Value)
                                           .Distinct()
                                           .ToArray();
     var binds = !IsTagBindEnabled
         ? String.Empty
         : _boundTags.Guard().Except(existedTags)
                     .Distinct()
                     .Select(t => " #" + t)
                     .JoinString(String.Empty);
     RequestBase<TwitterStatus> request;
     if (IsDirectMessage)
     {
         request = new MessagePostingRequest(MessageRecipient, Text);
     }
     else
     {
         request = new TweetPostingRequest(Text + binds, InReplyTo,
             AttachedGeoLocation, _attachedImage);
     }
     var s = Observable.Defer(() => Observable.Start(() => _accounts.Guard().ToObservable()))
                       .SelectMany(a => a)
                       .SelectMany(a => SendInternal(a, request))
                       .WaitForCompletion()
                       .Select(r => r.ToLookup(t => t.Item3 == null))
                       .Select(g =>
                       {
                           InputData succ = null;
                           InputData fail = null;
                           Exception[] exs = null;
                           if (g.Contains(true))
                           {
                               succ = this.Clone();
                               // succeeded results
                               var succeeds = g[true].ToArray();
                               succ.AmendTargetTweets = succeeds.ToDictionary(t => t.Item1, t => t.Item2);
                               succ.Accounts = succeeds.Select(t => t.Item1);
                           }
                           if (g.Contains(false))
                           {
                               fail = this.Clone();
                               // failed results
                               var faileds = g[false].ToArray();
                               fail.Accounts = faileds.Select(t => t.Item1);
                               exs = faileds.Select(t => t.Item3).ToArray();
                           }
                           return new PostResult(succ, fail, exs);
                       });
     if (IsAmend)
     {
         return AmendTargetTweets
             .ToObservable()
             .SelectMany(t => RequestQueue.Enqueue(t.Key, new DeletionRequest(t.Value)))
             .Select(_ => (PostResult)null)
             .Concat(s)
             .Where(r => r != null);
     }
     return s;
 }
Пример #2
0
        public async Task <PostResult> SendAsync()
        {
            var existedTags = TwitterRegexPatterns.ValidHashtag.Matches(Text)
                              .OfType <Match>()
                              .Select(_ => _.Groups[1].Value)
                              .Distinct()
                              .ToArray();
            var binds = !IsTagBindEnabled
                ? String.Empty
                : _boundTags.Guard().Except(existedTags)
                        .Distinct()
                        .Select(t => " #" + t)
                        .JoinString(String.Empty);
            RequestBase <TwitterStatus> request;

            if (IsDirectMessage)
            {
                request = new MessagePostingRequest(MessageRecipient, Text);
            }
            else
            {
                request = new TweetPostingRequest(Text + binds, InReplyTo,
                                                  AttachedGeoLocation, _attachedImages);
            }

            var posts = _accounts.Guard()
                        .Select(a => Tuple.Create(a, SendInternal(a, request)))
                        .ToArray();

            var amendTargets   = new Dictionary <TwitterAccount, TwitterStatus>();
            var failedAccounts = new List <TwitterAccount>();
            var exceptions     = new List <Exception>();

            foreach (var item in posts)
            {
                var account = item.Item1;
                try
                {
                    var result = await item.Item2.ConfigureAwait(false);

                    amendTargets.Add(account, result);
                }
                catch (Exception ex)
                {
                    failedAccounts.Add(account);
                    exceptions.Add(ex);
                }
            }
            InputData successData = null, failedData = null;

            if (amendTargets.Count > 0)
            {
                successData = Clone();
                successData.AmendTargetTweets = amendTargets.ToArray();
                successData.Accounts          = amendTargets.Select(p => p.Key);
            }

            if (failedAccounts.Count > 0)
            {
                failedData          = Clone();
                failedData.Accounts = failedAccounts.ToArray();
            }

            var pr = new PostResult(successData, failedData, exceptions.ToArray());


            if (IsAmend)
            {
                var amends = AmendTargetTweets
                             .Select(pair => RequestQueue.EnqueueAsync(pair.Key, new DeletionRequest(pair.Value)))
                             .OfType <Task>().ToArray();
                Task.WaitAll(amends);
            }
            return(pr);
        }
Пример #3
0
        public IObservable <PostResult> Send()
        {
            var existedTags = TwitterRegexPatterns.ValidHashtag.Matches(Text)
                              .OfType <Match>()
                              .Select(_ => _.Groups[1].Value)
                              .Distinct()
                              .ToArray();
            var binds = !IsTagBindEnabled
                ? String.Empty
                : _boundTags.Guard().Except(existedTags)
                        .Distinct()
                        .Select(t => " #" + t)
                        .JoinString(String.Empty);
            RequestBase <TwitterStatus> request;

            if (IsDirectMessage)
            {
                request = new MessagePostingRequest(MessageRecipient, Text);
            }
            else
            {
                request = new TweetPostingRequest(Text + binds, InReplyTo,
                                                  AttachedGeoLocation, _attachedImage);
            }
            var s = Observable.Defer(() => Observable.Start(() => _accounts.Guard().ToObservable()))
                    .SelectMany(a => a)
                    .SelectMany(a => SendInternal(a, request))
                    .WaitForCompletion()
                    .Select(r => r.ToLookup(t => t.Item3 == null))
                    .Select(g =>
            {
                InputData succ  = null;
                InputData fail  = null;
                Exception[] exs = null;
                if (g.Contains(true))
                {
                    succ = this.Clone();
                    // succeeded results
                    var succeeds           = g[true].ToArray();
                    succ.AmendTargetTweets = succeeds.ToDictionary(t => t.Item1, t => t.Item2);
                    succ.Accounts          = succeeds.Select(t => t.Item1);
                }
                if (g.Contains(false))
                {
                    fail = this.Clone();
                    // failed results
                    var faileds   = g[false].ToArray();
                    fail.Accounts = faileds.Select(t => t.Item1);
                    exs           = faileds.Select(t => t.Item3).ToArray();
                }
                return(new PostResult(succ, fail, exs));
            });

            if (IsAmend)
            {
                return(AmendTargetTweets
                       .ToObservable()
                       .SelectMany(t => RequestQueue.Enqueue(t.Key, new DeletionRequest(t.Value)))
                       .Select(_ => (PostResult)null)
                       .Concat(s)
                       .Where(r => r != null));
            }
            return(s);
        }
Пример #4
0
        public async Task<PostResult> SendAsync()
        {
            var existedTags = TwitterRegexPatterns.ValidHashtag.Matches(Text)
                                                  .OfType<Match>()
                                                  .Select(_ => _.Groups[1].Value)
                                                  .Distinct()
                                                  .ToArray();
            var binds = !IsTagBindEnabled
                ? String.Empty
                : _boundTags.Guard().Except(existedTags)
                            .Distinct()
                            .Select(t => " #" + t)
                            .JoinString(String.Empty);
            RequestBase<TwitterStatus> request;
            if (IsDirectMessage)
            {
                request = new MessagePostingRequest(MessageRecipient, Text);
            }
            else
            {
                request = new TweetPostingRequest(Text + binds, InReplyTo,
                    AttachedGeoLocation, _attachedImage);
            }

            var posts = _accounts.Guard()
                                 .Select(a => Tuple.Create(a, SendInternal(a, request)))
                                 .ToArray();
            var tasks = posts.Select(p => p.Item2).OfType<Task>().ToArray();
            // wait for completion
            Task.WaitAll(tasks);

            var amendTargets = new Dictionary<TwitterAccount, TwitterStatus>();
            var failedAccounts = new List<TwitterAccount>();
            var exceptions = new List<Exception>();

            foreach (var item in posts)
            {
                var account = item.Item1;
                try
                {
                    var result = await item.Item2;
                    amendTargets.Add(account, result);
                }
                catch (Exception ex)
                {
                    failedAccounts.Add(account);
                    exceptions.Add(ex);
                }
            }
            InputData successData = null, failedData = null;

            if (amendTargets.Count > 0)
            {
                successData = this.Clone();
                successData.AmendTargetTweets = amendTargets.ToArray();
                successData.Accounts = amendTargets.Select(p => p.Key);
            }

            if (failedAccounts.Count > 0)
            {
                failedData = this.Clone();
                failedData.Accounts = failedAccounts.ToArray();
            }

            var pr = new PostResult(successData, failedData, exceptions.ToArray());


            if (IsAmend)
            {
                var amends = AmendTargetTweets
                    .Select(pair => RequestQueue.EnqueueAsync(pair.Key, new DeletionRequest(pair.Value)))
                    .OfType<Task>().ToArray();
                Task.WaitAll(amends);
            }
            return pr;
        }