Beispiel #1
0
        /*
         *
         * try {
         *  window.NicoGoogleTagManagerDataLayer = [];
         *
         *  var data = {
         *  };
         *
         *
         *  data.user = (function() {
         *    var user = {
         *    };
         *
         *
         *          user.user_id = parseInt('0000000', 10) || null;
         *    user.login_status = 'login';
         *    user.member_status = 'normal';
         *    user.birthday = '1979-01-01';
         *    user.sex = 'male';
         *    user.country = 'Japan';
         *    user.prefecture = '○○県';
         *    user.ui_area = 'jp';
         *    user.ui_lang = 'ja-jp';
         *
         *
         *
         *    return user;
         *  })();
         *
         *  window.NicoGoogleTagManagerDataLayer.push(data);
         * } catch(e) {
         * }
         *
         */

        // 未ログイン時は user.login_status = 'login'; のみ設定される

        public static InfoResponse ParseInfoData(string userInfoData)
        {
            var html = new HtmlDocument();

            html.LoadHtml(userInfoData);

            var htmlHtml = html.DocumentNode.Element("html");
            var language = htmlHtml.GetAttributeValue("lang", "ja-jp");


            var head = htmlHtml.Element("head");
            var userInfoJSStartString = "user.user_id = ";
            var userInfoStartPos      = head.InnerHtml.IndexOf(userInfoJSStartString);

            var userInfoJsonCharArray = head.InnerHtml.Skip(userInfoStartPos).TakeWhile(x => x != '}').ToArray();
            var rawText = new string(userInfoJsonCharArray);

            var splited = rawText.Trim().Split(';', '=');

            UserMyPageJSInfo info = new UserMyPageJSInfo();

            for (int index = 0; index < splited.Length; index += 2)
            {
                var key   = splited[index].Trim(' ', '\n');;
                var value = splited[index + 1].Trim('\'', ' ', '\n');

                if (key == "user.user_id")
                {
                    info.Id = new string(value.Skip("parseInt('".Length).TakeWhile(x => x != '\'').ToArray());
                }
                else if (key == "user.member_status")
                {
                    info.IsPremium = value == "premium";
                }
                else if (key == "user.birthday")
                {
                    info.Age = (int)(DateTime.Now - DateTime.Parse(value)).TotalDays / 365;
                }
            }

            return(new InfoResponse(htmlHtml.Element("body"), language, info));
        }
        internal InfoResponse(HtmlNode bodyHtml, string language, UserMyPageJSInfo info)
        {
            try
            {
                this.Id = uint.Parse(info.Id);
            }
            catch { }

            try
            {
                this.IsPremium = info.IsPremium;
            }
            catch { }

            try
            {
                this.IsOver18 = info.Age >= 18;
            }
            catch { }

            /*
             * var profileHtml = bodyHtml
             * .GetElementByClassName("BaseLayout")
             * .GetElementByClassName("userDetail")
             * .GetElementByClassName("profile");
             * try
             * {
             *      var h2Html = profileHtml.Element("h2");
             *      this.Name = h2Html.FirstChild.InnerText;
             * }
             * catch //(Exception ex)
             * {
             *      //throw new Exception("ユーザー名の取得に失敗しました", ex);
             * }
             */
            /*
             * {
             *      var accountIdHtml = profileHtml.GetElementByClassName( "account" ).GetElementByClassName( "accountNumber" ).Element( "span" );
             *      var keywords = accountIdHtml.InnerText.Split( new char[] { ' ', '(', ')' } );
             *      if( keywords.Count() >= 4 )
             *      {
             *              switch( language )
             *              {
             *                      case "ja-jp":
             *                              this.Id = keywords[0].ToUInt();
             *                              this.JoinedVersion = keywords[1];
             *                              this.IsPremium = keywords[3] == "プレミアム会員";
             *                              break;
             *                      case "en-us":
             *                              this.Id = keywords[0].ToUInt();
             *                              this.JoinedVersion = keywords[1];
             *                              this.IsPremium = keywords[3] == "Premium";
             *                              break;
             *                      case "zh-tw":
             *                              this.Id = keywords[0].ToUInt();
             *                              this.JoinedVersion = keywords[1];
             *                              this.IsPremium = keywords[3] == "白金會員";
             *                              break;
             *              }
             *      }
             * }
             */

            /*
             *          try
             *          {
             *                  var stats = profileHtml.GetElementByClassName("stats");
             *                  var statsItems = stats.SelectNodes("./li/a/span");
             *
             *                  var statsItemNumbers = statsItems.Select(x =>
             *                  {
             *                          var numberWithUnit = x.InnerText.Where(y => y != ',');
             *                          var numberText = string.Join("", numberWithUnit.TakeWhile(y => y >= '0' && y <= '9'));
             *                          return uint.Parse(numberText);
             *                  })
             *                  .ToArray();
             *
             *                  this.FavoriteCount = (ushort)statsItemNumbers[0];
             *                  this.StampCount = (ushort)statsItemNumbers[1];
             *                  this.Points = statsItemNumbers[2];
             *                  this.CreatorScore = statsItemNumbers[3];
             *          }
             *          catch (Exception) { }
             */
        }