Esempio n. 1
0
        //public string getAndroidVideoPath(string filename)
        //{
        //    var dir = Android.OS.Environment.GetExternalStoragePublicDirectory(
        //    Android.OS.Environment.DirectoryDcim);
        //    //var dir = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        //    var pictures = dir.AbsolutePath;
        //    var newFilepath = System.IO.Path.Combine(pictures, filename);
        //    return newFilepath;
        //}


        //private List<string> extractURLFromIframeTag(string path, string url)
        //{
        //    try
        //    {
        //        List<string> urls = null;
        //        try
        //        {
        //            string pageSource = getPageSource(url).ToLower();
        //            if (pageSource.Contains("<iframe"))
        //            {
        //                var htmlDoc = new XmlDocument();
        //                htmlDoc.LoadXml(pageSource);
        //                urls = new List<string>();
        //                var htmlNodes = htmlDoc.DocumentNode.SelectNodes(".//iframe");
        //                if (null != htmlNodes)
        //                {
        //                    foreach (var node in htmlNodes)
        //                    {
        //                        string iframeSrc = node.Attributes["src"].Value;
        //                        if (!string.IsNullOrEmpty(iframeSrc))
        //                        {

        //                            if (!iframeSrc.Contains(YOUTUBE_LINK_01) && !iframeSrc.Contains(YOUTUBE_LINK_02))
        //                            {
        //                                if (!iframeSrc.Contains("google.com/") && !iframeSrc.Contains("twitter.com") && !iframeSrc.Contains("eclick.vn") && !iframeSrc.Contains("googletagmanager.com"))
        //                                {
        //                                    if (!iframeSrc.Contains(FACEBOOK_VIDEO_BASE))
        //                                    {

        //                                        iframeSrc = iframeSrc.Replace("&amp;", "&");
        //                                        if (iframeSrc != "" && iframeSrc != null && !urls.Contains(iframeSrc))
        //                                        {
        //                                            List<string> links = extractURLFromHTMLHasVideoURL(null, iframeSrc);
        //                                            if (null != links)
        //                                            {
        //                                                urls.AddRange(links);
        //                                            }
        //                                        }
        //                                    }
        //                                    else
        //                                    {
        //                                        FacebookDownloader.Downloader downloader = new FacebookDownloader.Downloader();
        //                                        urls.AddRange(downloader.findURL(iframeSrc));
        //                                    }
        //                                }
        //                            }
        //                            else
        //                            {
        //                                Console.WriteLine("there is youtube embeded in iframe, calling youtubedownloader...");
        //                                WriteLog("there is youtube embeded in iframe,youtube link = " + iframeSrc + "| calling youtubedownloader...");
        //                                Lcbc_YouTubeDownloader.YouTubeDownloader.Instant().findLinkAndDownload(iframeSrc, path);
        //                            }
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            WriteLog("Exception in extractURLFromIframeTag, ex = " + ex.ToString());
        //        }
        //        return urls;
        //    }
        //    catch (Exception ex)
        //    {
        //        WriteLog("Exception extractURLFromIframeTag, ex = " + ex.ToString());
        //        return new List<string>();
        //    }
        //}

        private ObservableCollection <VideoInfor> RetrieveVideoList()
        {
            string path = string.Empty;

#if __ANDROID__
            path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim).AbsolutePath;
#endif
#if __IOS__
            path = DependencyService.Get <IDownloadState>().OnDownloadStarted();
#endif
            //string[] filePaths = Directory.GetFiles(path, "*.mp4");
            string[] array1 = Directory.GetFiles(path, "*.flv");
            string[] array2 = Directory.GetFiles(path, "*.mp4");
            int      array1OriginalLength = array1.Length;
            Array.Resize <string>(ref array1, array1OriginalLength + array2.Length);
            Array.Copy(array2, 0, array1, array1OriginalLength, array2.Length);
            if (null != array2)
            {
                if (array2.Length > 0)
                {
                    ObservableCollection <VideoInfor> videos = new ObservableCollection <VideoInfor>();
                    foreach (string p in array2)
                    {
                        VideoInfor v = new VideoInfor();
                        v.Path = p;
                        int e = p.LastIndexOf(@"/");
                        v.Title = p.Substring(p.LastIndexOf(@"/"), (p.Length - 1) - p.LastIndexOf(@"/"));
                        v.Title = v.Title.Replace(@"/", "");
                        v.Title = v.Title.Replace(".mp4", "");
                        v.Title = v.Title.Replace(".flv", "");
                        videos.Add(v);
                    }
                    return(videos);
                }
                else
                {
                    return(null);
                }
            }
            return(null);
        }
Esempio n. 2
0
        private async void downloadAsync(List <string> urlsWithFindLink, List <string> urlsWithIframe, List <string> urlsToDownload, string url, RequestDownload request)
        {
            try
            {
                string pageSource = "";

                pageSource       = getPageSource(url);
                urlsWithFindLink = extractURLFromHTMLHasVideoURL(url);

                if (pageSource.Contains("<iframe"))
                {
                    //urlsWithIframe = extractURLFromIframeTag(path, url);
                }

                if (null != urlsWithFindLink)
                {
                    for (int i = 0; i < urlsWithFindLink.Count; i++)
                    {
                        if (!urlsToDownload.Contains(urlsWithFindLink[i]))
                        {
                            urlsToDownload.Add(urlsWithFindLink[i]);
                        }
                    }
                }

                if (null != urlsWithIframe)
                {
                    for (int i = 0; i < urlsWithIframe.Count; i++)
                    {
                        if (!urlsToDownload.Contains(urlsWithIframe[i]))
                        {
                            urlsToDownload.Add(urlsWithIframe[i]);
                        }
                    }
                }



                if (null != urlsToDownload)
                {
                    for (int i = 0; i < urlsToDownload.Count; i++)
                    {
                        try
                        {
                            VideoInfor video = new VideoInfor();
                            video.DownloadUrl = WebUtility.UrlDecode(urlsToDownload[i]);
                            video.BasURL      = url;
                            string[] splitedURL = urlsToDownload[i].Split('/');
                            string   title      = RemoveIllegalPathCharacters(splitedURL[splitedURL.Length - 1]).ToLower();
                            if (!title.Contains(".mp4") && !title.Contains(".flv") && !title.Contains(".3gp") && !video.DownloadUrl.Contains(".m3u8"))
                            {
                                title = title + ".mp4";
                            }
                            video.Title = title;

#if __ANDROID__
                            var dir         = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);
                            var newFilepath = System.IO.Path.Combine(dir.AbsolutePath, title);
                            video.Path = newFilepath;
#endif
#if __IOS__
                            video.Path = System.IO.Path.Combine(DependencyService.Get <IDownloadState>().OnDownloadStarted(), title);
#endif
                            new FileInfo(video.Path).Directory.Create();
                            downloadVideo(video, false);
                        }
                        catch (Exception ex)
                        {
                            txtPageSource.Text = ex.ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
Esempio n. 3
0
        public async void downloadVideo(VideoInfor video, bool isRetried)
        {
            if (!video.DownloadUrl.Contains(HLS) && !video.DownloadUrl.Contains(HLS_1))
            {
                string filepath = video.Path;

                if (Uri.IsWellFormedUriString(video.DownloadUrl, UriKind.RelativeOrAbsolute) && !video.DownloadUrl.Contains("https://v.vnecdn.net/vnexpress/video/video_default.mp4"))
                {
                    video.DownloadUrl = video.DownloadUrl.Replace("&amp;", "&");
                    Console.WriteLine("Downloading url:  " + video.DownloadUrl);
                    try
                    {
                        if (!video.DownloadUrl.Contains(".m3u8"))
                        {
                            Console.WriteLine("Downloading.....");

                            var request = (HttpWebRequest)WebRequest.Create(video.DownloadUrl);
                            if (isRetried)
                            {
                                request.Timeout = 120000;
                            }
                            using (WebResponse response = request.GetResponse())
                            {
                                using (Stream source = response.GetResponseStream())
                                {
                                    using (FileStream target = File.Open(filepath, FileMode.Create, System.IO.FileAccess.Write))
                                    {
                                        var  buffer = new byte[1024];
                                        bool cancel = false;
                                        int  bytes;
                                        int  copiedBytes = 0;
                                        while (!cancel && (bytes = source.Read(buffer, 0, buffer.Length)) > 0)
                                        {
                                            target.Write(buffer, 0, bytes);
                                            copiedBytes += bytes;
                                            Double percent = ((Double)copiedBytes / (Double)source.Length) * 100;
                                            Console.WriteLine("downloading  " + percent + "%");
                                            Device.BeginInvokeOnMainThread(() =>
                                            {
                                                progressBar1.Progress = percent / 100;
                                                percent1.Text         = (int)percent + " %";
                                            });
                                        }
                                        if (null == videos)
                                        {
                                            videos = RetrieveVideoList();
                                            downloadedListView.ItemsSource = videos;
                                        }
                                        else
                                        {
                                            videos.Add(video);
                                        }
                                    }
                                    Device.BeginInvokeOnMainThread(() =>
                                    {
                                        DependencyService.Get <IDownloadState>().OnDownloadFinished(video.Path);
                                    });
                                }
                            }
                        }
                        else
                        {
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception download url = " + video.DownloadUrl);
                        Console.WriteLine("Exception download url Exception = " + ex.ToString());
                        if (!isRetried)
                        {
                            downloadVideo(video, true);
                        }
                        else
                        {
                            // DependencyService.Get<IDownloadState>().OnDownloadError();
                        }
                    }
                }
            }
        }