Exemple #1
0
        internal bool TryGetVideoItem(string url, out VideoItem video)
        {
            video = new VideoItem {
                Url = url
            };
            if (string.IsNullOrEmpty(url))
            {
                return(false);
            }
            Uri uri;

            if (!Uri.TryCreate(url, UriKind.Absolute, out uri))
            {
                return(false);
            }
            var host = uri.Host;
            VideoSiteSetting site = null;

            foreach (var st in this.SiteSettings)
            {
                if (string.Compare(st.Host, host, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    site = st;
                    break;
                }
                if (host.EndsWith("." + st.Host, StringComparison.InvariantCultureIgnoreCase))
                {
                    site = st;
                    break;
                }
            }
            if (site == null)
            {
                return(false);
            }
            video.Host = site.Host;
            return(TryGetVideoItem(url, site, video));
        }
Exemple #2
0
        internal static bool TryGetVideoItem(string url, VideoSiteSetting site, VideoItem video)
        {
            var matches = site.RegexID.Matches(url);

            if (matches.Count <= 0)
            {
                return(false);
            }
            var id = video.VideoID = matches[matches.Count - 1].Groups[1].Value;

            video.VideoUrl = string.Format(site.VideoUrl, id);
            var contentUrl = string.IsNullOrEmpty(site.ContentUrl) ? url : string.Format(site.ContentUrl, id);
            var html       = video.UrlHtml = GetContent(contentUrl);

            if (string.IsNullOrEmpty(html))
            {
                return(false);
            }
            if (site.DataFormat == "json")
            {
                var js   = new JavaScriptSerializer();
                var data = js.DeserializeObject(html) as IDictionary <string, object>;
                if (data != null)
                {
                    var    names = site.IconRegex.Split('/');
                    object value = data;
                    foreach (var name in names)
                    {
                        if (value is IDictionary <string, object> )
                        {
                            var dict = value as IDictionary <string, object>;
                            if (dict.ContainsKey(name))
                            {
                                value = dict[name];
                            }
                        }
                        else if (value is Array)
                        {
                            var array = value as Array;
                            int index;
                            if (!int.TryParse(name, out index))
                            {
                                break;
                            }
                            value = array.GetValue(index);
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (value != null)
                    {
                        video.IconUrl = Convert.ToString(value);
                    }
                }
            }
            else
            {
                var match = site.RegexIcon.Match(html);
                if (match.Success)
                {
                    video.IconUrl = match.Groups[1].Value;
                }
            }
            return(true);
        }