コード例 #1
0
        public override IMetacriticData Parse(UrlResponsePair urlResponsePair)
        {
            string html = urlResponsePair.Response;

            if (String.IsNullOrEmpty(m_thirdLevelRequest))
            {
                Album album = new Album();
                album.Title         = ParseItem(ref html, @"<h1>", @"</h1>");
                album.PrimaryArtist = ParseItem(ref html, @"<span class=""band_name"" itemprop=""name"">", @"</span>");
                string   releaseDateStr = ParseItem(ref html, @"<span class=""data"" itemprop=""datePublished"">", @"</span>");
                DateTime releaseDate;
                if (DateTime.TryParse(releaseDateStr, out releaseDate))
                {
                    album.ReleaseDate = releaseDate.ToString("MM/dd/yyyy");
                }

                short?criticRating      = null;
                short?criticRatingCount = null;
                if (short.TryParse(ParseItem(ref html, @"<span itemprop=""ratingValue"">", @"</span>"), out short tempCriticRating))
                {
                    criticRating      = tempCriticRating;
                    criticRatingCount = Int16.Parse(ParseItem(ref html, @"<span itemprop=""reviewCount"">", @"</span>"));
                }

                float?userRating      = null;
                short?userRatingCount = null;
                int   userRatingIdx   = html.IndexOf("metascore_w user large");
                if (userRatingIdx != -1)
                {
                    html = html.Substring(userRatingIdx);
                    if (float.TryParse(ParseItem(ref html, @""">", @"</div>"), out float tempUserRating))
                    {
                        userRating      = tempUserRating;
                        userRatingCount = Int16.Parse(ParseItem(ref html, @"user-reviews"">", @" Ratings"));
                    }
                }

                album.Rating = new Rating(criticRating, userRating, criticRatingCount, userRatingCount);

                if (UrlImagePath != null)
                {
                    string imgPath;
                    if (UrlImagePath.TryGetValue(urlResponsePair.Url, out imgPath))
                    {
                        album.ImageUrl = imgPath;
                    }
                }

                return(album);
            }
            else if (m_thirdLevelRequest == "details")
            {
                MediaDetail mediaDetail = new MediaDetail();
                html = html.Substring(html.IndexOf(@"""new_details"""));
                while (html.Contains(@"span class=""label"">"))
                {
                    string desc  = ParseItem(ref html, @"span class=""label"">", @":</span>");
                    string value = string.Empty;

                    int nextIdx;
                    int valueIdx;
                    do
                    {
                        if (string.IsNullOrEmpty(value))
                        {
                            value = ParseItem(ref html, @"span class=""data"">", @"</span>");
                        }
                        else
                        {
                            value = string.Format("{0}, {1}", value,
                                                  ParseItem(ref html, @"span class=""data"">", @"</span>"));
                        }

                        nextIdx  = html.IndexOf(@"<span class=""label"">");
                        valueIdx = html.IndexOf(@"span class=""data"">");
                    } while (nextIdx != -1 && valueIdx < nextIdx);

                    DetailItem detail = new DetailItem(desc, value);
                    mediaDetail.Details.Add(detail);
                }

                return(mediaDetail);
            }

            return(null);
        }
コード例 #2
0
        public override IMetacriticData Parse(UrlResponsePair urlResponsePair)
        {
            string html = urlResponsePair.Response;

            if (String.IsNullOrEmpty(m_thirdLevelRequest))
            {
                TVShow tvShow = new TVShow();
                tvShow.Title  = ParseItem(ref html, @"<h1>", @"</h1>");
                tvShow.Season = Int32.Parse(ParseItem(ref html, @"Season ", @"</h2>"));

                tvShow.Studio = ParseItem(ref html, @"<span itemprop=""name"">", @"</span>");

                short?criticRating      = null;
                short?criticRatingCount = null;
                if (short.TryParse(ParseItem(ref html, @"<span itemprop=""ratingValue"">", @"</span>"), out short tempCriticRating))
                {
                    criticRating      = tempCriticRating;
                    criticRatingCount = Int16.Parse(ParseItem(ref html, @"<span itemprop=""reviewCount"">", @"</span>"));
                }

                float?userRating      = null;
                short?userRatingCount = null;
                int   userRatingIdx   = html.IndexOf("metascore_w user large");
                if (userRatingIdx != -1)
                {
                    html = html.Substring(userRatingIdx);
                    if (float.TryParse(ParseItem(ref html, @""">", @"</div>"), out float tempUserRating))
                    {
                        userRating      = tempUserRating;
                        userRatingCount = Int16.Parse(ParseItem(ref html, @"user-reviews"">", @" Ratings"));
                    }
                }

                tvShow.Rating = new Rating(criticRating, userRating, criticRatingCount, userRatingCount);

                string   releaseDateStr = ParseItem(ref html, @"<span class=""data"" itemprop=""startDate"">", @"</span>");
                DateTime releaseDate;
                if (DateTime.TryParse(releaseDateStr, out releaseDate))
                {
                    tvShow.ReleaseDate = releaseDate.ToString("MM/dd/yyyy");
                }

                if (UrlImagePath != null)
                {
                    string key = UrlImagePath.Keys.FirstOrDefault(k => urlResponsePair.Url.Contains(k));
                    if (key != null)
                    {
                        string imgPath;
                        if (UrlImagePath.TryGetValue(key, out imgPath))
                        {
                            tvShow.ImageUrl = imgPath;
                        }
                    }
                }

                return(tvShow);
            }
            else if (m_thirdLevelRequest == "details")
            {
                MediaDetail mediaDetails = new MediaDetail();

                while (html.Contains(@"<th scope=""row"">"))
                {
                    string desc  = ParseItem(ref html, @"<th scope=""row"">", @":</th>");
                    string value = ParseItem(ref html, @"<td>", @"</td>");
                    if (value.Contains("</a>"))
                    {
                        value = ParseItem(ref value, @""">", @"</a>");
                    }

                    if (desc == "Seasons")
                    {
                        value = value.Replace(" ", String.Empty);
                    }

                    if (desc.Contains("Genre"))
                    {
                        Regex rgx = new Regex("\\s+");
                        value = rgx.Replace(value, " ");
                    }

                    DetailItem detail = new DetailItem(desc, value);
                    mediaDetails.Details.Add(detail);
                }

                while (html.Contains(@"<td class=""person"">"))
                {
                    html = html.Substring(html.IndexOf(@"<td class=""person"">") +
                                          @"<td class=""person"">".Length);
                    string      name   = ParseItem(ref html, @""">", @"</a>");
                    string      role   = ParseItem(ref html, @"<td class=""role"">", @"</td>");
                    MediaCredit credit = new MediaCredit(name, role);
                    mediaDetails.Credits.Add(credit);
                }

                return(mediaDetails);
            }

            return(null);
        }
コード例 #3
0
        public override IMetacriticData Parse(UrlResponsePair urlResponsePair)
        {
            string html = urlResponsePair.Response;

            if (String.IsNullOrEmpty(m_thirdLevelRequest))
            {
                Movie movie      = new Movie();
                int   startIndex = html.IndexOf(@"<script type=""application/ld+json"">");
                if (startIndex != -1)
                {
                    string infoString = html.Substring(startIndex);
                    int    endIndex   = infoString.IndexOf(@"</script>");
                    if (endIndex != -1)
                    {
                        infoString  = infoString.Substring(0, endIndex);
                        movie.Title = ParseItem(ref infoString, @"""name"" : """, @"""");

                        string   releaseDateStr = ParseItem(ref infoString, @"""datePublished"" : """, @"""");
                        DateTime releaseDate;
                        if (DateTime.TryParse(releaseDateStr, out releaseDate))
                        {
                            movie.ReleaseDate = releaseDate.ToString("MM/dd/yyyy");;
                        }

                        short?criticRating      = null;
                        short?criticRatingCount = null;
                        if (infoString.Contains(@"""ratingValue"" : """))
                        {
                            if (short.TryParse(ParseItem(ref infoString, @"""ratingValue"" : """, @""""), out short tempCriticRating))
                            {
                                criticRating      = tempCriticRating;
                                criticRatingCount = Int16.Parse(ParseItem(ref infoString, @"""ratingCount"" : """, @""""));
                            }

                            // Critic
                            html = html.Substring(html.IndexOf("Critics</span>"));
                        }

                        // User
                        float?userRating      = null;
                        short?userRatingCount = null;
                        if (html.Contains(@">based on "))
                        {
                            html = html.Substring(html.IndexOf(">based on "));

                            if (short.TryParse(ParseItem(ref html, @">based on ", " Ratings"), out short tempUserRatingCount))
                            {
                                userRatingCount = tempUserRatingCount;
                                html            = html.Substring(html.IndexOf("metascore_w user"));
                                userRating      = float.Parse(ParseItem(ref html, @">", @"</span>"));
                            }
                        }

                        movie.Rating = new Rating(criticRating, userRating, criticRatingCount, userRatingCount);


                        infoString     = infoString.Substring(infoString.IndexOf(@"""director"""));
                        movie.Director = ParseItem(ref infoString, @"""name"": """, @"""");
                    }
                }

                if (UrlImagePath != null)
                {
                    string imgPath;
                    if (UrlImagePath.TryGetValue(urlResponsePair.Url, out imgPath))
                    {
                        movie.ImageUrl = imgPath;
                    }
                }

                return(movie);
            }
            else if (m_thirdLevelRequest == "details")
            {
                MediaDetail mediaDetail = new MediaDetail();
                while (html.Contains(@"<td class=""label"">"))
                {
                    string desc  = ParseItem(ref html, @"<td class=""label"">", @":</td>");
                    string value = ParseItem(ref html, @"<td class=""data"">", @"</td>");
                    if (value.Contains("</a>"))
                    {
                        value = ParseItem(ref value, @""">", @"</a>");
                    }

                    if (value.Contains("<span>"))
                    {
                        value = value.Replace("<span>", "").Replace("</span>", "");
                    }

                    if (desc == "Genres" || desc == "Languages" || desc == "Countries")
                    {
                        Regex rgx = new Regex("\\s+");
                        value = rgx.Replace(value, " ");
                    }

                    DetailItem detail = new DetailItem(desc, value);
                    mediaDetail.Details.Add(detail);
                }

                while (html.Contains(@"<td class=""person"">"))
                {
                    string name = ParseItem(ref html, @"<td class=""person"">", @"</td>");
                    if (name.Contains("</a>"))
                    {
                        name = ParseItem(ref name, @""">", @"</a>");
                    }

                    string role = ParseItem(ref html, @"<td class=""role"">", @"</td>");

                    MediaCredit credit = new MediaCredit(name, role);
                    mediaDetail.Credits.Add(credit);
                }

                return(mediaDetail);
            }

            return(null);
        }