Ejemplo n.º 1
0
        public static async Task <string> GetDmcSessionResponseDataAsync(
            NiconicoContext context,
            DmcWatchResponse watchData,
            VideoContent videoQuality = null,
            AudioContent audioQuality = null,
            bool hlsMode = false
            )
        {
            var req = new DmcSessionRequest();

            var session        = watchData.Media.Delivery.Movie.Session;
            var videoQualities = watchData.Media.Delivery.Movie.Videos;
            var audioQualities = watchData.Media.Delivery.Movie.Audios;
            var encryption     = watchData.Media.Delivery.Encryption;

            // リクエストする動画品質を決定します
            // モバイルの時は最後の動画品質をモバイル画質として断定して指定
            // それ以外の場合、対象画質とそれ以下の有効な画質をすべて指定
            var requestVideoQuality = new List <string>();

            if (videoQuality?.IsAvailable ?? false)
            {
                requestVideoQuality.Add(videoQuality.Id);
            }
            else
            {
                var fallbackVideoQuality = videoQualities.Last(x => x.Metadata.LevelIndex == 0);
                requestVideoQuality.Add(fallbackVideoQuality.Id);
            }

            var requestAudioQuality = new List <string>();

            if (audioQuality?.IsAvailable ?? false)
            {
                requestAudioQuality.Add(audioQuality.Id);
            }
            else
            {
                var fallbackAudioQuality = audioQualities.Last(x => x.Metadata.LevelIndex == 0);
                requestAudioQuality.Add(fallbackAudioQuality.Id);
            }

            var sessionUrl       = $"{session.Urls[0].UrlUnsafe}?_format=json";
            var useSsl           = true;                                             // session.Urls[0].IsSsl;
            var wellKnownPort    = session.Urls[0].IsWellKnownPort;
            var protocolName     = session.Protocols[0];                             // http,hls
            var protocolAuthType = session.Protocols.ElementAtOrDefault(1) ?? "ht2"; // ht2

            req.Session = new RequestSession()
            {
                RecipeId         = session.RecipeId,
                ContentId        = session.ContentId,
                ContentType      = "movie",
                ContentSrcIdSets = new List <ContentSrcIdSet>()
                {
                    new ContentSrcIdSet()
                    {
                        ContentSrcIds = new List <ContentSrcId>()
                        {
                            new ContentSrcId()
                            {
                                SrcIdToMux = new SrcIdToMux()
                                {
                                    VideoSrcIds = requestVideoQuality,
                                    AudioSrcIds = requestAudioQuality
                                },
                            }
                        }
                    }
                },
                TimingConstraint = "unlimited",
                KeepMethod       = new KeepMethod()
                {
                    Heartbeat = new Heartbeat()
                    {
                        Lifetime = 120000
                    }
                },
                Protocol = new Protocol()
                {
                    Name       = "http",
                    Parameters = new Protocol.ProtocolParameters()
                    {
                        HttpParameters = new Protocol.HttpParameters()
                        {
                            Parameters = new Protocol.ParametersInfo()
                            {
                                HlsParameters = protocolName == "hls" ? new Protocol.HlsParameters()
                                {
                                    UseSsl           = useSsl ? "yes" : "no",
                                    UseWellKnownPort = wellKnownPort ? "yes" : "no",
                                    SegmentDuration  = 5000,
                                    TransferPreset   = "",
                                    Encryption       = encryption != null
                                    ? new Protocol.Encryption()
                                    {
                                        HlsEncryptionV1 = new Protocol.HlsEncryptionV1()
                                        {
                                            EncryptedKey = encryption.EncryptedKey,
                                            KeyUri       = encryption.KeyUri
                                        }
                                    }
                                    : null
                                }
                                : null
                                ,
                                HttpOutputDownloadParameters = protocolName == "http" ? new Protocol.HttpOutputDownloadParameters() : null
                            }
                        }
                    }
                },
                ContentUri           = "",
                SessionOperationAuth = new SessionOperationAuth_Request()
                {
                    SessionOperationAuthBySignature = new SessionOperationAuthBySignature_Request()
                    {
                        Token     = session.Token,
                        Signature = session.Signature
                    }
                },
                ContentAuth = new ContentAuth_Request()
                {
                    AuthType          = "ht2",
                    ContentKeyTimeout = (int)session.ContentKeyTimeout,
                    ServiceId         = "nicovideo",
                    ServiceUserId     = session.ServiceUserId
                },
                ClientInfo = new ClientInfo()
                {
                    PlayerId = session.PlayerId
                },
                Priority = session.Priority
            };

            var requestJson = JsonConvert.SerializeObject(req, new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore
            });

//            var decodedJson = WebUtility.HtmlEncode(requestJson);
            var decodedJson = requestJson;

#if WINDOWS_UWP
            return(await context.PostAsync(sessionUrl, new HttpStringContent(decodedJson, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json")));
#else
            return(await context.PostAsync(sessionUrl, new StringContent(decodedJson, UnicodeEncoding.UTF8, "application/json")));
#endif
        }
Ejemplo n.º 2
0
        public static async Task <string> PostCommentDataAsync(
            NiconicoContext context,
            string commentServerUrl,
            string threadId,
            string ticket,
            int commentCount,
            string comment,
            TimeSpan position,
            string command
            )
        {
            var info = await context.User.GetInfoAsync();

            var userid    = info.Id;
            var isPremium = info.IsPremium;

            // postkeyの取得
            var postKey = await GetPostKeyAsync(context, threadId, commentCount)
                          .ContinueWith(prevResult => ParsePostKey(prevResult.Result));

            Debug.WriteLine(postKey);


            var postComment = new PostComment()
            {
                user_id = userid.ToString(),
                mail    = command,
                thread  = threadId,
                vpos    = ((uint)position.TotalMilliseconds / 10).ToString(),
                ticket  = ticket,
                premium = isPremium.ToString1Or0(),
                postkey = postKey,
                comment = comment,
            };


            string postCommentXml  = "";
            var    emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
            var    serializer      = new XmlSerializer(typeof(PostComment));

            var xmlwriterSettings = new XmlWriterSettings()
            {
                OmitXmlDeclaration = true,
            };

            using (var memoryStream = new MemoryStream())
            {
                serializer.Serialize(XmlWriter.Create(memoryStream, xmlwriterSettings), postComment, emptyNamepsaces);
                memoryStream.Flush();
                memoryStream.Seek(0, SeekOrigin.Begin);

                using (var reader = new StreamReader(memoryStream))
                {
                    postCommentXml = reader.ReadToEnd();
                }
            }

            Debug.WriteLine(postCommentXml);

            return(await context.PostAsync(commentServerUrl, postCommentXml));
        }
Ejemplo n.º 3
0
 public static Task <string> GetHistoriesDataAsync(NiconicoContext context)
 {
     return(context.PostAsync(NiconicoUrls.VideoHistoryUrl));
 }