예제 #1
0
        private Task <UploadFinalizeCommandResult> UploadStatusCommandAsyncImpl(IEnumerable <KeyValuePair <string, object> > parameters, CancellationToken cancellationToken, string urlPrefix, string urlSuffix)
        {
            var options = Tokens.ConnectionOptions ?? ConnectionOptions.Default;

            if (urlPrefix != null || urlSuffix != null)
            {
                options = options.Clone();

                if (urlPrefix != null)
                {
                    options.UrlPrefix = urlPrefix;
                }

                if (urlSuffix != null)
                {
                    options.UrlSuffix = urlSuffix;
                }
            }

            return(this.Tokens.SendRequestAsyncImpl(MethodType.Get, InternalUtils.GetUrl(options, options.UploadUrl, true, "media/upload.json"),
                                                    parameters.EndWith(new KeyValuePair <string, object>("command", "STATUS")), cancellationToken)
                   .ReadResponse(s => CoreBase.Convert <UploadFinalizeCommandResult>(s), cancellationToken));
        }
예제 #2
0
 private Task <MediaUploadResult> UploadAsyncImpl(IEnumerable <KeyValuePair <string, object> > parameters, CancellationToken cancellationToken)
 {
     return(this.AccessUploadApiAsync(parameters, cancellationToken)
            .ReadResponse(s => CoreBase.Convert <MediaUploadResult>(s), cancellationToken));
 }
예제 #3
0
        private Task <MediaUploadResult> UploadChunkedAsyncImpl(Stream media, int totalBytes, UploadMediaType mediaType, IEnumerable <KeyValuePair <string, object> > parameters, CancellationToken cancellationToken)
        {
            return(this.AccessUploadApiAsync(
                       new Dictionary <string, object>()
            {
                { "command", "INIT" },
                { "total_bytes", totalBytes },
                { "media_type", GetMediaTypeString(mediaType) }
            }.Concat(parameters), cancellationToken)
                   .ReadResponse(s => (string)JObject.Parse(s)["media_id_string"], cancellationToken)
                   .Done(mediaId =>
            {
                var tasks = new List <Task>();
                const int maxChunkSize = 5 * 1000 * 1000;
                var remainingBytes = totalBytes;

                for (var i = 0; remainingBytes > 0; i++)
                {
                    var segmentIndex = i;     // to capture the variable
                    var chunkSize = remainingBytes < maxChunkSize ? remainingBytes : maxChunkSize;
                    var chunk = new byte[chunkSize];
                    var readCount = media.Read(chunk, 0, chunkSize);
                    if (readCount == 0)
                    {
                        break;
                    }
                    if (chunkSize != readCount)
                    {
                        var newChunk = new byte[readCount];
                        Buffer.BlockCopy(chunk, 0, newChunk, 0, readCount);
                        chunk = newChunk;
                    }
                    remainingBytes -= readCount;
                    tasks.Add(
                        this.AccessUploadApiAsync(
                            new Dictionary <string, object>()
                    {
                        { "command", "APPEND" },
                        { "media_id", mediaId },
                        { "segment_index", segmentIndex },
                        { "media", chunk }
                    }, cancellationToken
                            )
                        .Done(res => res.Dispose(), cancellationToken)
                        );
                }

                return WhenAll(tasks)
                .Done(() =>
                {
                    return AccessUploadApiAsync(
                        new Dictionary <string, object>()
                    {
                        { "command", "FINALIZE" },
                        { "media_id", mediaId }
                    }, cancellationToken)
                    .ReadResponse(s => CoreBase.Convert <MediaUploadResult>(s), cancellationToken);
                }, cancellationToken
                      )
                .Unwrap();
            }, cancellationToken)
                   .Unwrap());
        }
예제 #4
0
        /// <summary>
        /// Converts the JSON to a <see cref="ActivityEvent"/> object.
        /// </summary>
        /// <param name="x">The JSON value.</param>
        /// <returns>The <see cref="ActivityEvent"/> object.</returns>
        public static ActivityEvent Parse(string x)
        {
            try
            {
                var           j = JObject.Parse(x);
                ActivityEvent e;

                if (j["tweet_create_events"] != null)
                {
                    e = CoreBase.Convert <TweetCreateEvents>(x);
                }
                else if (j["favorite_events"] != null)
                {
                    e = CoreBase.Convert <FavoriteEvents>(x);
                }
                else if (j["follow_events"] != null)
                {
                    e = CoreBase.Convert <FollowEvents>(x);
                }
                else if (j["block_events"] != null)
                {
                    e = CoreBase.Convert <BlockEvents>(x);
                }
                else if (j["mute_events"] != null)
                {
                    e = CoreBase.Convert <MuteEvents>(x);
                }
                else if (j["user_event"] != null)
                {
                    e = CoreBase.Convert <UserEvent>(x, "user_event");
                }
                else if (j["direct_message_events"] != null)
                {
                    e = CoreBase.Convert <DirectMessageEvents>(x);
                }
                else if (j["direct_message_indicate_typing_events"] != null)
                {
                    e = CoreBase.Convert <DirectMessageIndicateTypingEvents>(x);
                }
                else if (j["direct_message_mark_read_events"] != null)
                {
                    e = CoreBase.Convert <DirectMessageMarkReadEvents>(x);
                }
                else if (j["tweet_delete_events"] != null)
                {
                    e = CoreBase.Convert <TweetDeleteEvents>(x);
                }
                else
                {
                    throw new ParsingException("on account activity api, cannot parse the json: unsupported type", j.ToString(Formatting.Indented), null);
                }

                e.Json = x;
                return(e);
            }
            catch (ParsingException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new ParsingException("on account activity api, cannot parse the json", x, e);
            }
        }
예제 #5
0
 private Task <T> CommandAsync <T>(string command, IEnumerable <KeyValuePair <string, object> > parameters, CancellationToken cancellationToken)
 {
     return(this.CommandAsync(command, parameters, cancellationToken)
            .ReadResponse(s => CoreBase.Convert <T>(s), cancellationToken));
 }
예제 #6
0
 private Task <MediaUploadResult> UploadAsyncImpl(IEnumerable <KeyValuePair <string, object> > parameters, CancellationToken cancellationToken, string urlPrefix, string urlSuffix, IProgress <UploadProgressInfo> progress = null)
 {
     return(this.AccessUploadApiAsync(parameters, cancellationToken, urlPrefix, urlSuffix, progress)
            .ReadResponse(s => CoreBase.Convert <MediaUploadResult>(s), cancellationToken));
 }