コード例 #1
0
ファイル: CrawlerBO.cs プロジェクト: faelpires/LnkCapture
        public async Task <UriData> GetUriDataAsync(Uri uri)
        {
            var uriData = new UriData {
                Uri     = uri,
                IsValid = false
            };

            try
            {
                if (IsYoutubeUri(uri, out var videoId))
                {
                    return(await GetYoutubeData(uri, videoId));
                }
                else
                {
                    return(await GetDataAsync(uri));
                }
            }
            catch (Exception)
            {
                uriData.Title = null;

                return(uriData);
            }
        }
コード例 #2
0
        public DrNuRtmpStreamFactory(ILibRtmpWrapper libRtmpWrapper, UriData.Factory uriDataFactory)
        {
            if (libRtmpWrapper == null) throw new ArgumentNullException("libRtmpWrapper");
            if (uriDataFactory == null) throw new ArgumentNullException("uriDataFactory");

            _libRtmpWrapper = libRtmpWrapper;
            _uriDataFactory = uriDataFactory;
        }
コード例 #3
0
        /// <internalonly />
        protected override IAsyncResult BeginLoadUri(Uri uri, Page uriContext, AsyncCallback callback, object asyncState)
        {
            if (_currentController != null) {
                throw new InvalidOperationException();
            }

            UriData uriData = new UriData(uri);

            IList<string> path = uriData.GetPath();
            int minimumCount = _controllerType != null ? 1 : 2;

            if ((path != null) && (path.Count >= minimumCount)) {
                Controller controller = null;

                Type controllerType = _controllerType;
                if (controllerType == null) {
                    Type appType = Application.Current.GetType();

                    _currentControllerName = path[0];
                    path.RemoveAt(0);

                    string controllerTypeName =
                        appType.Namespace + ".Controllers." + _currentControllerName + "Controller";

                    controllerType = appType.Assembly.GetType(controllerTypeName, /* throwOnError */ false);
                    if (typeof(Controller).IsAssignableFrom(controllerType) == false) {
                        controllerType = null;
                    }
                }
                if (controllerType != null) {
                    controller = CreateController(controllerType);
                }

                if (controller != null) {
                    _currentController = controller;

                    _currentActionName = path[0];
                    path.RemoveAt(0);

                    ActionInvocation action = new ActionInvocation(_currentActionName);
                    foreach (string parameterItem in path) {
                        action.Parameters.Add(parameterItem);
                    }

                    IDictionary<string, string> queryString = uriData.GetQueryString();
                    if (queryString != null) {
                        foreach (KeyValuePair<string, string> parameterItem in queryString) {
                            action.NamedParameters[parameterItem.Key] = parameterItem.Value;
                        }
                    }

                    return ((IController)controller).BeginExecute(action, callback, asyncState);
                }
            }

            return base.BeginLoadUri(uri, uriContext, callback, asyncState);
        }
コード例 #4
0
ファイル: CrawlerBO.cs プロジェクト: faelpires/LnkCapture
        private async Task <string> DownloadStringAwareOfEncodingAsync(Uri uri)
        {
            var uriData = new UriData();

            try
            {
                using (var webClient = new WebClient())
                {
                    try
                    {
                        webClient.Headers.Add(CrawlerResources.WebClientUserAgent);

                        var rawData = await webClient.DownloadDataTaskAsync(uri);

                        var encoding = GetEncodingFrom(webClient.ResponseHeaders, Encoding.UTF8);

                        uriData.Content = encoding.GetString(rawData);

                        return(uriData.Content);
                    }
                    catch (WebException exception)
                    {
                        var response = ((HttpWebResponse)exception.Response);

                        if (response.StatusCode == HttpStatusCode.MovedPermanently)
                        {
                            if (response.Headers.AllKeys.Contains("Location"))
                            {
                                var location = response.Headers["Location"];

                                if (location != null)
                                {
                                    return(await DownloadStringAwareOfEncodingAsync(new Uri(location)));
                                }
                            }
                        }

                        throw;
                    }
                    catch (Exception exception)
                    {
                        Logger.LogError(exception.Message);

                        throw;
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.LogError(exception.Message);

                throw;
            }
        }
コード例 #5
0
ファイル: CrawlerBO.cs プロジェクト: faelpires/LnkCapture
        private async Task <UriData> GetYoutubeData(Uri uri, string videoId)
        {
            var uriData = new UriData {
                Uri     = uri,
                IsValid = false
            };

            try
            {
                using (var webClient = new WebClient())
                {
                    webClient.Headers.Add(CrawlerResources.WebClientUserAgent);

                    var rawData = await webClient.DownloadStringTaskAsync(string.Format(Configuration.GetSection("AppConfiguration")["YoutubeApiUri"], videoId, Configuration.GetSection("AppConfiguration")["YoutubeApiKey"]));

                    var youtubeData = JsonConvert.DeserializeObject <YoutubeData>(rawData);

                    if (youtubeData == null || youtubeData.Items == null || youtubeData.Items.Length == 0 || youtubeData.Items[0].Snippet == null)
                    {
                        return(uriData);
                    }

                    uriData.IsValid = true;

                    if (!string.IsNullOrWhiteSpace(youtubeData.Items[0].Snippet.Title))
                    {
                        uriData.Title = youtubeData.Items[0].Snippet.Title;
                    }

                    if (!string.IsNullOrWhiteSpace(youtubeData.Items[0].Snippet.Description))
                    {
                        uriData.Description = youtubeData.Items[0].Snippet.Description.Trim();
                    }

                    if (youtubeData.Items[0].Snippet.Thumbnails != null && youtubeData.Items[0].Snippet.Thumbnails.Default != null & youtubeData.Items[0].Snippet.Thumbnails.Default.Url != null)
                    {
                        uriData.ThumbnailUri = new Uri(youtubeData.Items[0].Snippet.Thumbnails.Default.Url);
                    }

                    if (youtubeData.Items[0].Snippet.Tags != null && youtubeData.Items[0].Snippet.Tags.Length > 0)
                    {
                        uriData.Keywords = string.Join(",", youtubeData.Items[0].Snippet.Tags);
                    }

                    return(uriData);
                }
            }
            catch (Exception exception)
            {
                Logger.LogError(exception.Message);

                return(uriData);
            }
        }
コード例 #6
0
ファイル: PageFrame.cs プロジェクト: nikhilk/silverlightfx
 public NavigationState(Uri uri)
 {
     this.uri = new UriData(uri);
     this.journalNavigation = true;
 }
コード例 #7
0
ファイル: PageFrame.cs プロジェクト: nikhilk/silverlightfx
        private bool NavigateInternal(NavigationState navigationState)
        {
            if (_contentView == null) {
                return true;
            }
            if (_navigateResult != null) {
                ((NavigationState)_navigateResult.AsyncState).canceled = true;
                _navigateResult = null;
            }

            Page currentPage = Page;

            string fragment;
            if (navigationState.uri.TryGetFragment(out fragment)) {
                if (navigationState.uri.GetPath() == null) {
                    // Fragment navigation

                    if (currentPage != null) {
                        UriData currentUri = new UriData(currentPage.OriginalUri);
                        currentUri.SetFragment(fragment);

                        Uri newUri = currentUri.GetUri();
                        _journal.AddEntry(newUri);
                        _backCommand.UpdateStatus(_journal.CanGoBack);
                        _forwardCommand.UpdateStatus(_journal.CanGoForward);

                        currentPage.OnStateChanged(new PageStateEventArgs(fragment));

                        return true;
                    }

                    return false;
                }
                else {
                    navigationState.fragment = fragment;
                    navigationState.uri.SetFragment(null);
                }
            }

            if (_redirecting == false) {
                bool canCancel = !_journal.IsIntegratedWithBrowser;

                if (currentPage != null) {
                    PageNavigatingEventArgs e = new PageNavigatingEventArgs(canCancel);
                    currentPage.OnNavigating(e);

                    if (canCancel && e.Cancel) {
                        return false;
                    }
                }

                NavigatingEventArgs ne = new NavigatingEventArgs(navigationState.uri.OriginalUri, canCancel);
                OnNavigating(ne);
                if (canCancel && ne.Cancel) {
                    return false;
                }
            }

            Page page = _cache.GetPage(navigationState.uri.OriginalUri);
            if (page != null) {
                navigationState.cachedPage = true;
                Dispatcher.BeginInvoke(delegate() {
                    OnNavigationCompleted(navigationState, page);
                });
                return true;
            }

            try {
                IAsyncResult navigateResult = _loader.BeginLoadPage(navigationState.uri.GetUri(), Page,
                                                                    OnPageLoadCallback, navigationState);
                if (navigateResult.CompletedSynchronously == false) {
                    _navigateResult = navigateResult;
                    IsNavigating = true;
                }
            }
            catch (Exception e) {
                Page errorPage = GetErrorPage(e);

                Dispatcher.BeginInvoke(delegate() {
                    OnNavigationCompleted(navigationState, errorPage);
                });
            }

            return true;
        }
コード例 #8
0
ファイル: CrawlerBO.cs プロジェクト: faelpires/LnkCapture
        private UriData GetOpenGraphData(Uri uri, HtmlDocument htmlDocument)
        {
            var uriData = new UriData {
                Uri     = uri,
                IsValid = false
            };

            try
            {
                var metaTags = htmlDocument.DocumentNode.SelectNodes("//meta");

                if (metaTags != null)
                {
                    var matchCount = 0;

                    foreach (var tag in metaTags)
                    {
                        var tagName     = tag.Attributes["name"];
                        var tagContent  = tag.Attributes["content"];
                        var tagProperty = tag.Attributes["property"];

                        if (tagName != null && tagContent != null)
                        {
                            switch (tagName.Value.ToLower())
                            {
                            case "title":
                                uriData.Title = tagContent.Value.Trim();
                                matchCount++;
                                break;

                            case "description":
                                uriData.Description = string.IsNullOrWhiteSpace(tagContent.Value) ? null : tagContent.Value.Trim();
                                matchCount++;
                                break;

                            case "twitter:title":
                                uriData.Title = string.IsNullOrWhiteSpace(uriData.Title) ? tagContent.Value.Trim() : uriData.Title;
                                matchCount++;
                                break;

                            case "twitter:description":
                                uriData.Description = string.IsNullOrWhiteSpace(uriData.Description) ? tagContent.Value.Trim() : uriData.Description;
                                matchCount++;
                                break;

                            case "keywords":
                                uriData.Keywords = tagContent.Value.Trim();
                                matchCount++;
                                break;

                            case "twitter:image":
                                uriData.ThumbnailUri = uriData.ThumbnailUri ?? new Uri(tagContent.Value.Trim());
                                matchCount++;
                                break;
                            }
                        }
                        else if (tagProperty != null && tagContent != null)
                        {
                            switch (tagProperty.Value.ToLower())
                            {
                            case "og:title":
                                uriData.Title = string.IsNullOrWhiteSpace(uriData.Title) ? tagContent.Value.Trim() : uriData.Title;
                                matchCount++;
                                break;

                            case "og:description":
                                uriData.Description = string.IsNullOrWhiteSpace(uriData.Description) ? tagContent.Value.Trim() : uriData.Description;
                                matchCount++;
                                break;

                            case "og:image":
                                uriData.ThumbnailUri = uriData.ThumbnailUri ?? new Uri(tagContent.Value.Trim());
                                matchCount++;
                                break;
                            }
                        }
                    }

                    uriData.HasOpenGraphData = matchCount > 0;
                }

                return(uriData);
            }
            catch (Exception exception)
            {
                Logger.LogError(exception.Message);

                return(uriData);
            }
        }
コード例 #9
0
ファイル: Primatives.cs プロジェクト: Erguotou/protobuf-net
        public void TestNonEmptyUriAllCompilationModes()
        {
            var model = TypeModel.Create();
            model.Add(typeof(UriData), true);
            UriData test = new UriData { Foo = new Uri("http://test.example.com/demo") };

            UriData clone = (UriData) model.DeepClone(test);
            Assert.AreEqual(test.Foo, clone.Foo, "Runtime");

            var compiled = model.Compile("TestNonEmptyUriAllCompilationModes", "TestNonEmptyUriAllCompilationModes.dll");
            PEVerify.AssertValid("TestNonEmptyUriAllCompilationModes.dll");
            model.CompileInPlace();
            clone = (UriData)model.DeepClone(test);
            Assert.AreEqual(test.Foo, clone.Foo, "CompileInPlace");

            clone = (UriData)compiled.DeepClone(test);
            Assert.AreEqual(test.Foo, clone.Foo, "CompileIn");
        }