Пример #1
0
        /// <summary>
        /// html形式のコミュニティページからコミュニティ情報を作成します。
        /// </summary>
        public static CommunityInfo CreateFromHtml(int id, string pageStr)
        {
            var community = new CommunityInfo();

            if (string.IsNullOrEmpty(pageStr))
            {
                throw new NicoLiveException(
                          "与えられたページがありません。",
                          LiveUtil.CommunityIdString(id));
            }

            var m = Regex.Match(pageStr,
                                @"<img src=""http://icon\.nimg\.jp/community/\d+/co([0-9]+)\.jpg\?[0-9]+""");

            if (!m.Success)
            {
                throw new NicoLiveException(
                          "コミュニティIDを取得できませんでした。",
                          LiveUtil.CommunityIdString(id));
            }
            community.Id = int.Parse(m.Groups[1].Value);

            m = Regex.Match(pageStr,
                            @"<title>\s*(.*)\s*-?\s*ニコニコ[^<]*?\s*</title>");
            if (!m.Success)
            {
                throw new NicoLiveException(
                          "コミュニティタイトルを取得できませんでした。",
                          LiveUtil.CommunityIdString(id));
            }
            community.Title = WebUtil.DecodeHtmlText(m.Groups[1].Value);

            m = Regex.Match(pageStr,
                            @"<div class=""cnt2"" style=""[^""]+?"">\s*" +
                            @"((.|\s)*?)\s*</div></div></div>\s*" +
                            @"<!-- subbox profile -->");
            if (!m.Success)
            {
                m = Regex.Match(pageStr,
                                @"<div id=""community_description""><!-- ID は暫定 -->\s*" +
                                @"()</div>\s*</div>\s*<div id=""cbox_news"">");

                if (!m.Success)
                {
                    throw new NicoLiveException(
                              "コミュニティプロフィールを取得できませんでした。",
                              LiveUtil.CommunityIdString(id));
                }
            }
            community.Description = WebUtil.DecodeHtmlText(m.Groups[1].Value);

            // <td nowrap>レベル:</td>
            //   <td>
            //      <strong>13</strong>
            m = Regex.Match(pageStr,
                            @"<td nowrap>レベル:</td>\s*<td>\s*<strong>([0-9]+)</strong>");
            if (!m.Success)
            {
                throw new NicoLiveException(
                          "コミュニティのレベルを取得できませんでした。",
                          LiveUtil.CommunityIdString(id));
            }
            community.Level = int.Parse(m.Groups[1].Value);

            // <td nowrap>メンバー:</td>
            //   <td>
            //      <strong>87</strong>
            m = Regex.Match(pageStr,
                            @"<td nowrap>メンバー:</td>\s*<td>\s*<strong>([0-9]+)</strong>");
            if (!m.Success)
            {
                throw new NicoLiveException(
                          "コミュニティの参加メンバー数を取得できませんでした。",
                          LiveUtil.CommunityIdString(id));
            }
            community.NumberOfMembers = int.Parse(m.Groups[1].Value);

            m = Regex.Match(pageStr,
                            @"オーナー:<a href=""http://www\.nicovideo\.jp/user/([0-9]+)"" " +
                            @"style=""color:#FFF;"" target=""_blank"">\s*" +
                            @"<strong>([^<]+)</strong></a>");
            if (!m.Success)
            {
                throw new NicoLiveException(
                          "コミュニティのオーナーを取得できませんでした。",
                          LiveUtil.CommunityIdString(id));
            }
            community.OwnerId   = int.Parse(m.Groups[1].Value);
            community.OwnerName = WebUtil.DecodeHtmlText(m.Groups[2].Value);

            m = Regex.Match(pageStr,
                            @"開設日:<strong>(\d+)年(\d+)月(\d+)日</strong>");
            if (!m.Success)
            {
                throw new NicoLiveException(
                          "コミュニティの開設日が取得できませんでした。",
                          LiveUtil.CommunityIdString(id));
            }
            community.OpenDate = new DateTime(
                int.Parse(m.Groups[1].Value),
                int.Parse(m.Groups[2].Value),
                int.Parse(m.Groups[3].Value));

            return(community);
        }
Пример #2
0
        /// <summary>
        /// html形式のコミュニティページからコミュニティ情報を作成します。
        /// </summary>
        public static CommunityInfo CreateFromHtml(int id, string pageStr)
        {
            var community = new CommunityInfo();

            if (string.IsNullOrEmpty(pageStr))
            {
                throw new NicoLiveException(
                    "与えられたページがありません。",
                    LiveUtil.CommunityIdString(id));
            }

            var m = Regex.Match(pageStr,
                @"<img src=""http://icon\.nimg\.jp/community/\d+/co([0-9]+)\.jpg\?[0-9]+""");
            if (!m.Success)
            {
                throw new NicoLiveException(
                    "コミュニティIDを取得できませんでした。",
                    LiveUtil.CommunityIdString(id));
            }
            community.Id = int.Parse(m.Groups[1].Value);

            m = Regex.Match(pageStr,
                @"<title>\s*(.*)\s*-?\s*ニコニコ[^<]*?\s*</title>");
            if (!m.Success)
            {
                throw new NicoLiveException(
                    "コミュニティタイトルを取得できませんでした。",
                    LiveUtil.CommunityIdString(id));
            }
            community.Title = WebUtil.DecodeHtmlText(m.Groups[1].Value);

            m = Regex.Match(pageStr,
                @"<div class=""cnt2"" style=""[^""]+?"">\s*" +
                @"((.|\s)*?)\s*</div></div></div>\s*" +
                @"<!-- subbox profile -->");
            if (!m.Success)
            {
                m = Regex.Match(pageStr,
                    @"<div id=""community_description""><!-- ID は暫定 -->\s*" +
                    @"()</div>\s*</div>\s*<div id=""cbox_news"">");

                if (!m.Success)
                {
                    throw new NicoLiveException(
                        "コミュニティプロフィールを取得できませんでした。",
                        LiveUtil.CommunityIdString(id));
                }
            }
            community.Description = WebUtil.DecodeHtmlText(m.Groups[1].Value);            

            // <td nowrap>レベル:</td> 
            //   <td> 
            //      <strong>13</strong>
            m = Regex.Match(pageStr,
                @"<td nowrap>レベル:</td>\s*<td>\s*<strong>([0-9]+)</strong>");
            if (!m.Success)
            {
                throw new NicoLiveException(
                    "コミュニティのレベルを取得できませんでした。",
                    LiveUtil.CommunityIdString(id));
            }
            community.Level = int.Parse(m.Groups[1].Value);

            // <td nowrap>メンバー:</td> 
            //   <td> 
            //      <strong>87</strong>
            m = Regex.Match(pageStr,
                @"<td nowrap>メンバー:</td>\s*<td>\s*<strong>([0-9]+)</strong>");
            if (!m.Success)
            {
                throw new NicoLiveException(
                    "コミュニティの参加メンバー数を取得できませんでした。",
                    LiveUtil.CommunityIdString(id));
            }
            community.NumberOfMembers = int.Parse(m.Groups[1].Value);

            m = Regex.Match(pageStr,
                @"オーナー:<a href=""http://www\.nicovideo\.jp/user/([0-9]+)"" " +
                @"style=""color:#FFF;"" target=""_blank"">\s*" +
                @"<strong>([^<]+)</strong></a>");
            if (!m.Success)
            {
                throw new NicoLiveException(
                    "コミュニティのオーナーを取得できませんでした。",
                    LiveUtil.CommunityIdString(id));
            }
            community.OwnerId = int.Parse(m.Groups[1].Value);
            community.OwnerName = WebUtil.DecodeHtmlText(m.Groups[2].Value);

            m = Regex.Match(pageStr,
                @"開設日:<strong>(\d+)年(\d+)月(\d+)日</strong>");
            if (!m.Success)
            {
                throw new NicoLiveException(
                    "コミュニティの開設日が取得できませんでした。",
                    LiveUtil.CommunityIdString(id));
            }
            community.OpenDate = new DateTime(
                int.Parse(m.Groups[1].Value),
                int.Parse(m.Groups[2].Value),
                int.Parse(m.Groups[3].Value));

            return community;
        }