Exemplo n.º 1
0
        /// <summary>
        /// Check if Poster has downloaded.
        /// </summary>
        /// <returns>
        /// Downloaded status
        /// </returns>
        public static bool PosterDownloaded()
        {
            string url      = currentMovie.CurrentPosterImageUrl;
            string urlCache = WebCache.GetPathFromUrl(url, Section.Movies);

            return(File.Exists(urlCache));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handles the DoWork event of the bgwPoster control.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.
        /// </param>
        private static void BgwPoster_DoWork(object sender, DoWorkEventArgs e)
        {
            var    url      = e.Argument as string;
            string urlCache = WebCache.GetPathFromUrl(url, Section.Movies);

            if (!File.Exists(urlCache))
            {
                string path = Downloader.ProcessDownload(url, DownloadType.Binary, Section.Movies);
                e.Result = path;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// The load poster.
        /// </summary>
        /// <returns>
        /// Image object of poster
        /// </returns>
        public static Image LoadPoster()
        {
            if (!string.IsNullOrEmpty(currentMovie.PosterPathOnDisk) && File.Exists(currentMovie.PosterPathOnDisk))
            {
                return(ImageHandler.LoadImage(currentMovie.PosterPathOnDisk));
            }

            string urlCache = WebCache.GetPathFromUrl(currentMovie.CurrentPosterImageUrl, Section.Movies);

            if (!File.Exists(urlCache) || Downloader.Downloading.Contains(currentMovie.CurrentPosterImageUrl))
            {
                return(null);
            }

            return(ImageHandler.LoadImage(urlCache));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Add downloaditem to background que
        /// </summary>
        /// <param name="downloadItem">
        /// The download item.
        /// </param>
        public static void AddToBackgroundQue(DownloadItem downloadItem)
        {
            if (Get.Web.EnableAddToBackgroundQue == false)
            {
                return;
            }

            string path = WebCache.GetPathFromUrl(downloadItem.Url, downloadItem.Section);

            if (!File.Exists(path))
            {
                lock (BackgroundDownloadQue)
                {
                    BackgroundDownloadQue.Add(downloadItem);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Add downloaditem to background que
        /// </summary>
        /// <param name="downloadItem">
        /// The download item.
        /// </param>
        public static void AddToBackgroundQue(DownloadItem downloadItem, DownloadPriority downloadPriority = DownloadPriority.Normal)
        {
            if (Get.Web.EnableAddToBackgroundQue == false)
            {
                return;
            }

            string path = WebCache.GetPathFromUrl(downloadItem.Url, downloadItem.Section);

            if (!File.Exists(path))
            {
                lock (BackgroundDownloadQue)
                {
                    var check = (from d in BackgroundDownloadQue where d.Url == downloadItem.Url select d).SingleOrDefault();

                    if (check == null)
                    {
                        downloadItem.Priority = downloadPriority;
                        BackgroundDownloadQue.Add(downloadItem);
                    }
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the specified download item.
        /// </summary>
        /// <param name="downloadItem">The download item.</param>
        public static void Get(DownloadItem downloadItem)
        {
            downloadItem.Result = new BinaryResult();

            if (downloadItem.Url == null || !WebCache.CheckIfUrlIsTooLong(downloadItem.Url))
            {
                return;
            }

            if (!downloadItem.Url.ToLower().StartsWith("http://"))
            {
                downloadItem.Result.Success = true;
                downloadItem.Result.Result  = downloadItem.Url.ToLower();
                return;
            }

            var path = WebCache.GetPathFromUrl(downloadItem.Url, downloadItem.Section);

            var pathDir = path.Replace(Path.GetFileName(path), string.Empty);

            if (!Directory.Exists(pathDir))
            {
                Directory.CreateDirectory(pathDir);
            }

            if (!downloadItem.IgnoreCache && File.Exists(path))
            {
                downloadItem.Result.Success = true;
                downloadItem.Result.Result  = path;
                return;
            }

            if (File.Exists(path))
            {
                //TODO System.IO.IOException: file used by another process - possible exception
                File.Delete(path);
            }

            // the URL to download the file from
            var urlToReadFileFrom = downloadItem.Url;

            // the path to write the file to
            var filePathToWriteFileTo = path;

            downloadItem.Progress.Percent = 0;
            downloadItem.Progress.Message = string.Format("Connecting to {0}", urlToReadFileFrom);

            HttpWebResponse response;
            HttpWebRequest  request;
            bool            error;
            var             count = 0;

            do
            {
                try
                {
                    long runningByteTotal = 0;

                    var url2 = new Uri(urlToReadFileFrom);
                    request = (HttpWebRequest)WebRequest.Create(url2);
                    request.MaximumAutomaticRedirections = 4;
                    request.UserAgent        = Settings.Get.Web.UserAgent;
                    request.Timeout          = 20000;
                    request.Credentials      = CredentialCache.DefaultCredentials;
                    request.ReadWriteTimeout = 20000;
                    request.Proxy            = null;
                    request.KeepAlive        = false;

                    if (Settings.Get.Web.ProxyUse)
                    {
                        var proxy = new WebProxy(string.Format("{0}:{1}", Settings.Get.Web.ProxyIP, Settings.Get.Web.ProxyPort));

                        if (!string.IsNullOrEmpty(Settings.Get.Web.ProxyUserName) && !string.IsNullOrEmpty(Settings.Get.Web.ProxyPassword))
                        {
                            proxy.Credentials = new NetworkCredential(Settings.Get.Web.ProxyUserName, Settings.Get.Web.ProxyPassword);
                        }

                        request.Proxy = proxy;
                    }
                    else
                    {
                        request.Proxy = null;
                    }

                    request.KeepAlive = false;
                    using (response = (HttpWebResponse)request.GetResponse())
                    {
                        var size = response.ContentLength;

                        var streamRemote = response.GetResponseStream();

                        if (File.Exists(filePathToWriteFileTo) || size == -1)
                        {
                            return;
                        }

                        using (Stream streamLocal = new FileStream(
                                   filePathToWriteFileTo,
                                   FileMode.Create,
                                   FileAccess.Write,
                                   FileShare.None))
                        {
                            var byteBuffer = new byte[size];

                            downloadItem.Progress.Percent = 0;
                            downloadItem.Progress.Message = string.Format("Downloading {0}: 0%", urlToReadFileFrom);

                            string    speed = "";
                            Stopwatch sw1   = new Stopwatch();

                            if (streamRemote != null)
                            {
                                int byteSize;
                                sw1.Start();
                                while ((byteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                                {
                                    sw1.Stop();
                                    // write the bytes to the file system at the file path specified
                                    streamLocal.Write(byteBuffer, 0, byteSize);
                                    runningByteTotal += byteSize;

                                    // calculate the progress out of a base "100"
                                    var index = (double)runningByteTotal;
                                    var total = (double)byteBuffer.Length;
                                    var progressPercentageDouble  = index / total;
                                    var progressPercentageInteger = (int)(progressPercentageDouble * 100);

                                    // calculate the average speed of the download
                                    if (sw1.ElapsedMilliseconds != 0)
                                    {
                                        var speedbps  = (double)(((runningByteTotal) / ((double)sw1.ElapsedMilliseconds / 1000)));
                                        var speedkbps = (double)(((runningByteTotal) / ((double)sw1.ElapsedMilliseconds / 1000)) / 1024);
                                        var speedmbps = (double)(((runningByteTotal) / ((double)sw1.ElapsedMilliseconds / 1000)) / (1024 * 1024));
                                        if (speedmbps >= 1)
                                        {
                                            speed = string.Format("{0:#.##} MBytes/s", speedmbps);
                                        }
                                        else if (speedkbps >= 1)
                                        {
                                            speed = string.Format("{0:#.##} KBytes/s", speedkbps);
                                        }
                                        else if (speedbps >= 1)
                                        {
                                            speed = string.Format("{0:#.##} Bytes/s", speedbps);
                                        }
                                        else
                                        {
                                            speed = "";
                                        }
                                    }

                                    downloadItem.Progress.Percent = progressPercentageInteger;

                                    downloadItem.Progress.Message = string.Format(
                                        "Downloading {1}: {0}% : {2}",
                                        progressPercentageInteger,
                                        urlToReadFileFrom,
                                        speed);

                                    sw1.Start();
                                }
                            }

                            // clean up the file stream
                            streamLocal.Close();
                        }
                    }

                    error = false;
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.ProtocolError)
                    {
                        if (urlToReadFileFrom.IndexOf("_thumb") > -1)
                        {
                            urlToReadFileFrom = urlToReadFileFrom.Replace("_thumb.jpg", ".jpg");
                        }
                        else
                        {
                            return;
                        }
                    }

                    downloadItem.Progress.Message = string.Format("Restarting Attempt: {0} - {1}", count + 1, urlToReadFileFrom);
                    downloadItem.Progress.Percent = 0;
                    error = true;

                    count++;
                }
            }while (error && count < 5);

            if (error)
            {
                try
                {
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                }
                catch
                {
                    return;
                }

                return;
            }

            downloadItem.Result = new BinaryResult {
                Success = true, Result = path
            };

            return;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the specified download item.
        /// </summary>
        /// <param name="downloadItem">The download item.</param>
        public void Get(DownloadItem downloadItem)
        {
            Log.WriteToLog(LogSeverity.Info, downloadItem.ThreadID, string.Format("Processing HTML Started"), string.Format("{0}", downloadItem.Url));

            downloadItem.Result = new HtmlResult();

            downloadItem.Progress.Message = "Downloading " + downloadItem.Url;

            var cachePath = WebCache.GetPathFromUrl(downloadItem.Url, downloadItem.Section);

            Folders.CheckExists(cachePath, true);

            if (!downloadItem.IgnoreCache && !downloadItem.Url.Contains("YNoCache"))
            {
                if (File.Exists(cachePath + ".txt.gz"))
                {
                    Log.WriteToLog(LogSeverity.Info, downloadItem.ThreadID, string.Format(CultureInfo.CurrentCulture, "Url Found in Cache"), string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));
                    downloadItem.Result = new HtmlResult
                    {
                        Result  = Gzip.Decompress(cachePath + ".txt.gz"),
                        Success = true
                    };

                    return;
                }
            }

            if (InternalHandlers.Check(downloadItem))
            {
                InternalHandlers.Process(downloadItem, cachePath);
                return;
            }

            try
            {
                Log.WriteToLog(
                    LogSeverity.Info,
                    downloadItem.ThreadID,
                    string.Format(CultureInfo.CurrentCulture, "Downloading"),
                    string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));

                downloadItem.Url = downloadItem.Url.Replace(" ", "+").Replace("%20", "+");

                var webClient = new WebClient
                {
                    Proxy = null
                };

                if (Settings.Get.Web.EnableProxy)
                {
                    webClient.Proxy = new WebProxy(Settings.Get.Web.ProxyUserName, (int)Settings.Get.Web.ProxyPort);
                }

                webClient.Headers.Add("user-agent", Settings.Get.Web.UserAgent);

                var encode = Encoding.GetEncoding(1252);

                foreach (var encoding in Settings.Get.Web.WebEncodings)
                {
                    if (downloadItem.Url.Contains(encoding.Key))
                    {
                        encode = encoding.Value;
                        break;
                    }
                }

                webClient.Encoding = encode;

                var outputString = webClient.DownloadString(downloadItem.Url);

                Log.WriteToLog(
                    LogSeverity.Info,
                    downloadItem.ThreadID,
                    string.Format(CultureInfo.CurrentCulture, "Download Complete. Saving to Cache"),
                    string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));

                if (encode != Encoding.UTF8)
                {
                    var origBytes = encode.GetBytes(outputString);
                    var newBytes  = Encoding.Convert(encode, Encoding.UTF8, origBytes);
                    outputString = Encoding.UTF8.GetString(newBytes);
                    encode       = Encoding.UTF8;
                }

                File.WriteAllText(cachePath + ".txt.tmp", outputString, encode);

                Gzip.Compress(cachePath + ".txt.tmp", cachePath + ".txt.gz");
                File.Delete(cachePath + ".txt.tmp");

                Log.WriteToLog(
                    LogSeverity.Info,
                    downloadItem.ThreadID,
                    string.Format(CultureInfo.CurrentCulture, "Process Complete."),
                    string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));


                downloadItem.Result.Result  = outputString;
                downloadItem.Result.Success = true;
                return;
            }
            catch (Exception ex)
            {
                Log.WriteToLog(LogSeverity.Error, LoggerName.GeneralLog, string.Format("Download Html {0}", downloadItem.Url), ex.Message);
                return;
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets the specified download item.
        /// </summary>
        /// <param name="downloadItem">The download item.</param>
        public void Get(DownloadItem downloadItem)
        {
            Log.WriteToLog(LogSeverity.Info, downloadItem.ThreadID, string.Format("Processing HTML Started"), string.Format("{0}", downloadItem.Url));

            downloadItem.Result = new HtmlResult();

            downloadItem.Progress.Message = "Downloading " + downloadItem.Url;

            var cachePath = WebCache.GetPathFromUrl(downloadItem.Url, downloadItem.Section);

            Folders.CheckExists(cachePath, true);

            if (!downloadItem.IgnoreCache && !downloadItem.Url.Contains("YNoCache"))
            {
                if (File.Exists(cachePath + ".txt.gz"))
                {
                    Log.WriteToLog(LogSeverity.Info, downloadItem.ThreadID, string.Format(CultureInfo.CurrentCulture, "Url Found in Cache"), string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));
                    downloadItem.Result = new HtmlResult
                    {
                        Result  = Gzip.Decompress(cachePath + ".txt.gz"),
                        Success = true
                    };

                    return;
                }
            }

            try
            {
                Log.WriteToLog(
                    LogSeverity.Info,
                    downloadItem.ThreadID,
                    string.Format(CultureInfo.CurrentCulture, "Downloading"),
                    string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));

                downloadItem.Url = downloadItem.Url.Replace(" ", "+").Replace("%20", "+");

                var webClient = new WebClient
                {
                    Proxy = null
                };

                webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

                var outputString = webClient.DownloadString(downloadItem.Url);

                var encode = Encoding.GetEncoding(1252);
                if (downloadItem.Url.IndexOf("ofdb", StringComparison.CurrentCultureIgnoreCase) != -1 || downloadItem.Url.Contains("sratim") || downloadItem.Url.Contains("filmweb") || downloadItem.Url.Contains("allocine"))
                {
                    encode = Encoding.GetEncoding("UTF-8");
                }

                if (downloadItem.Url.Contains("filmaffinity") || downloadItem.Url.Contains("filmdelta"))
                {
                    encode = Encoding.GetEncoding("ISO-8859-1");
                }

                if (downloadItem.Url.Contains("kinopoisk"))
                {
                    encode = Encoding.GetEncoding("windows-1251");
                }

                Log.WriteToLog(
                    LogSeverity.Info,
                    downloadItem.ThreadID,
                    string.Format(CultureInfo.CurrentCulture, "Download Complete. Saving to Cache"),
                    string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));

                File.WriteAllText(cachePath + ".txt.tmp", outputString, encode);

                Gzip.Compress(cachePath + ".txt.tmp", cachePath + ".txt.gz");
                File.Delete(cachePath + ".txt.tmp");

                Log.WriteToLog(
                    LogSeverity.Info,
                    downloadItem.ThreadID,
                    string.Format(CultureInfo.CurrentCulture, "Process Complete."),
                    string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));

                downloadItem.Result.Result  = outputString;
                downloadItem.Result.Success = true;
                return;
            }
            catch (Exception ex)
            {
                Log.WriteToLog(LogSeverity.Error, LoggerName.GeneralLog, string.Format("Download Html {0}", downloadItem.Url), ex.Message);
                return;
            }
        }