Пример #1
0
        public async Task CrawlAsync(SitePage sitePage)
        {
            try
            {
                OnPageDownloadBegin?.Invoke(this, sitePage);
                await Downloader.Execute(sitePage);

                OnPageDownloadComplete?.Invoke(this, sitePage);

                SitePageTemplateExtractor.FindAll(sitePage.Uri, sitePage.Contents, _filters)
                .ForEach(async i =>
                {
                    Interlocked.Increment(ref _numberOfLinksLeft);
                    await CrawlAsync(i);
                });
            }
            catch (Exception exc)
            {
                OnError?.Invoke(this, exc);
            }

            if (Interlocked.Decrement(ref _numberOfLinksLeft) == 0)
            {
                OnDownloadCompleted?.Invoke(this, EventArgs.Empty);
            }
        }
Пример #2
0
        public void Save(byte[] buffer)
        {
            if (!HasError)
            {
                try {
                    writer.Write(buffer);
                    WritenBytesCount += buffer.Length;

                    OnDownloadUpdateProgress?.Invoke(Convert.ToInt32((WritenBytesCount / (double)FileLength) * 100f));

                    if (WritenBytesCount >= FileLength)
                    {
                        IsOpen    = false;
                        Completed = true;
                        Close();

                        OnDownloadCompleted?.Invoke(Folder, FileName);
                    }
                }
                catch {
                    HasError = true;
                    IsOpen   = false;
                    OnDownloadFailed?.Invoke(Folder, FileName);
                }
            }
        }
Пример #3
0
        void DownloadCompleted(bool cancelled)
        {
            client.Dispose();
            client = null;

            if (cancelled)
            {
                try
                {
                    OnDownloadCancelled?.Invoke(this, EventArgs.Empty);
                }
                catch { }
                return;
            }

            setting.SendLog(string.Format("{0}", I18N("DownloadCompleted")));
            if (UpdateCore())
            {
                try
                {
                    OnDownloadCompleted?.Invoke(this, EventArgs.Empty);
                }
                catch { }
            }
            else
            {
                try
                {
                    OnDownloadFail?.Invoke(this, EventArgs.Empty);
                }
                catch { }
            }
        }
Пример #4
0
        private void FromReaderToFile(StreamReader src, FileStream dest,
                                      ref DownloaderMetric metric, ref Stopwatch stopwatch, CancellationToken ct)
        {
            var buffer = new byte[32 * 1024];
            int bytesRead;

            while ((bytesRead = src.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                if (ct.IsCancellationRequested)
                {
                    dest.Flush(true);
                    dest.Close();
                    dest.Dispose();
                    return;
                }

                dest.Write(buffer, 0, bytesRead);
                metric.DownloadedBytes += bytesRead;
                metric.ElapsedTime      = stopwatch.Elapsed;
                OnDownloading?.Invoke(metric);
            }

            OnDownloadCompleted?.Invoke(metric);

            dest.Flush(true);
            dest.Close();
            dest.Dispose();
        }
Пример #5
0
        private void Remote_OnDownloadCompleted(object sender, BlobCompleteEventArgs e)
        {
            if (downloadTokenSources.ContainsKey(e.Filename))
            {
                downloadTokenSources.Remove(e.Filename);
            }

            OnDownloadCompleted?.Invoke(this, e);
        }
Пример #6
0
        public FileDownloader(string downloadUrl, string savePath)
        {
            DownloadUrl = downloadUrl;
            SavePath    = savePath;

            _downloadClient.DownloadFileCompleted += (s, e) =>
            {
                OnDownloadCompleted?.Invoke(!e.Cancelled && e.Error == null);
            };
            _downloadClient.DownloadProgressChanged += (sender, args) =>
                                                       OnProgressChanged?.Invoke(args.ProgressPercentage, "Downloading...");
        }
Пример #7
0
        private async Task <byte[]> DownloadAsync(CloudBlockBlob blob, CancellationToken token)
        {
            string fullpath = blob.Container.Name + "/" + blob.Name;

            byte[]    buffer = null;
            Stopwatch watch  = new Stopwatch();

            using (MemoryStream stream = new MemoryStream())
            {
                watch.Start();
                double time             = watch.Elapsed.TotalMilliseconds;
                long   bytesTransferred = 0;
                IProgress <StorageProgress> progressHandler = new Progress <StorageProgress>(
                    progress =>
                {
                    bytesTransferred = bytesTransferred < progress.BytesTransferred ? progress.BytesTransferred : bytesTransferred;
                    if (watch.Elapsed.TotalMilliseconds > time + 1000.0 && bytesTransferred <= progress.BytesTransferred)
                    {
                        time = time + 1000.0;
                        OnDownloadBytesTransferred?.Invoke(this, new BytesTransferredEventArgs(fullpath, blob.Properties.Length, progress.BytesTransferred));
                    }
                });

                try
                {
                    await blob.DownloadToStreamAsync(stream, default(AccessCondition),
                                                     default(BlobRequestOptions), default(OperationContext),
                                                     progressHandler, token);

                    buffer = stream.ToArray();
                    watch.Stop();
                }
                catch (Exception ex)
                {
                    watch.Stop();
                    if (ex.InnerException is TaskCanceledException)
                    {
                        buffer = null;
                    }
                    else
                    {
                        throw ex;
                    }
                }

                OnDownloadCompleted?.Invoke(this, new BlobCompleteEventArgs(fullpath, token.IsCancellationRequested));
            }

            return(buffer);
        }
Пример #8
0
        void Download()
        {
            string fileName = _packageName;
            string tpl      = resData("DownloadLinkTpl");
            string url      = string.Format(tpl, _version, fileName);

            Lib.Utils.SupportProtocolTLS12();
            client = new WebClient();

            int preProgress = -100;

            client.DownloadProgressChanged += (s, a) =>
            {
                var percentage = a.ProgressPercentage;

                if (percentage >= 1)
                {
                    var e = new Model.Data.IntEvent(a.ProgressPercentage);
                    try
                    {
                        OnProgress?.Invoke(this, e);
                    }
                    catch { }
                }

                if (percentage - preProgress >= 20)
                {
                    preProgress = percentage;
                    setting.SendLog(string.Format("{0}: {1}%", I18N("DownloadProgress"), percentage));
                }
            };

            client.DownloadFileCompleted += (s, a) =>
            {
                if (a.Cancelled)
                {
                    OnDownloadCancelled?.Invoke(this, EventArgs.Empty);
                }
                else
                {
                    setting.SendLog(string.Format("{0}", I18N("DownloadCompleted")));
                    OnDownloadCompleted?.Invoke(this, EventArgs.Empty);
                }
                client.Dispose();
                client = null;
            };

            setting.SendLog(string.Format("{0}:{1}", I18N("Download"), url));
            client.DownloadFileAsync(new Uri(url), fileName);
        }
        public void DownloadEventDataAsync()
        {
            WebClient web = new WebClient();

            web.OpenReadCompleted += web_OpenReadCompleted;
            try
            {
                //web.OpenReadAsync(new Uri("http://mobile.formula1.com"));
                web.OpenReadAsync(new Uri("https://www.f1calendar.com/"));
            }
            catch (Exception)
            {
                OnDownloadCompleted.Invoke(new RacingEvent(), CompletionStatus.Failed);
            }
        }
Пример #10
0
 void NotifyDownloadResults(bool status)
 {
     try
     {
         if (status)
         {
             OnDownloadCompleted?.Invoke(this, EventArgs.Empty);
         }
         else
         {
             OnDownloadFail?.Invoke(this, EventArgs.Empty);
         }
     }
     catch { }
 }
Пример #11
0
    private IEnumerator DownloadUpdateAsync()
    {
        var initHandle = Addressables.InitializeAsync();

        yield return(initHandle);

        Addressables.Release(initHandle);
        var checkHandle = Addressables.CheckForCatalogUpdates(false);

        yield return(checkHandle);

        var catalogs = checkHandle.Result;

        Addressables.Release(checkHandle);
        if (catalogs == null || catalogs.Count <= 0)
        {
            yield break;
        }
        SaveOriginalCatalogs();
        yield return(StartCoroutine(UpdateCatalogsAsync(catalogs)));

        yield return(StartCoroutine(CalculateUpdateSizeAsync()));

        if (downloadSize <= 0)
        {
            yield break;
        }
        yield return(StartCoroutine(RequestDownloadHandle?.Invoke(downloadSize)));

        if (Result == RequestDownloadResult.Agree)
        {
            DeleteOriginalCatalogs();
        }
        else
        {
            CancelDownload();
        }
        yield return(StartCoroutine(UpdateResourceLocatorsAsync()));

        if (Result == RequestDownloadResult.Disagree)
        {
            yield break;
        }
        yield return(StartCoroutine(DownloadAsync()));

        OnDownloadCompleted?.Invoke(downloadSize);
        yield return(StartCoroutine(AfterDownloadHandle?.Invoke()));
    }
Пример #12
0
        void DownloadPackage()
        {
            string ver      = resData("Version");
            string fileName = pkgName;
            string tpl      = resData("DownloadLinkTpl");
            string url      = string.Format(tpl, ver, fileName);

            Lib.Utils.SupportProtocolTLS12();
            WebClient client = new WebClient();

            client.DownloadFileCompleted += (s, a) =>
            {
                OnDownloadCompleted?.Invoke(this, null);
            };

            Debug.WriteLine("Download: " + url);
            client.DownloadFileAsync(new Uri(url), fileName);
        }
Пример #13
0
        private IEnumerator DownloadingFile(string url, string filePath, ICancellationToken cancellationToken, OnDownloadCompleted onCompleted, OnDownloadProgressChanged onProgressChanged)
        {
            bool anyError = true;

            using (UnityWebRequest unityWebRequest = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET))
            {
                unityWebRequest.downloadHandler = new DownloadHandlerFile(filePath)
                {
                    removeFileOnAbort = true
                };
                UnityWebRequestAsyncOperation asyncOperation = unityWebRequest.SendWebRequest();

                int lastPercents = 0;
                while (!asyncOperation.isDone)
                {
                    yield return(new WaitForEndOfFrame());

                    if (cancellationToken.Canceled)
                    {
                        unityWebRequest.Abort();
                        break;
                    }

                    int currentPercents = Mathf.RoundToInt(asyncOperation.progress * 100f);
                    if (lastPercents < currentPercents)
                    {
                        onProgressChanged?.Invoke(currentPercents);
                        lastPercents = currentPercents;
                    }
                }

                anyError = unityWebRequest.isNetworkError || unityWebRequest.isHttpError;
                if (anyError)
                {
                    Debug.LogError(unityWebRequest.error);
                }
                else
                {
                    Debug.Log($"{nameof(HttpClient)}: File successfully downloaded and saved to " + filePath);
                }
            }
            onCompleted?.Invoke(!anyError);
        }
Пример #14
0
 private void Operations_OnDownloadCompleted(object sender, BlobCompleteEventArgs e)
 {
     OnDownloadCompleted?.Invoke(this, e);
 }
Пример #15
0
        public IEnumerator DownloadingFiles(List <UrlFilePath> urlFilePaths, ICancellationToken cancellationToken, OnDownloadCompleted onCompleted, OnDownloadProgressChanged onProgressChanged)
        {
            bool noError = true;

            for (int i = 0; i < urlFilePaths.Count; i++)
            {
                if (cancellationToken.Canceled)
                {
                    noError = false;
                    break;
                }
                int         currentI     = i;
                int         currentCount = urlFilePaths.Count;
                UrlFilePath item         = urlFilePaths[i];
                yield return(GameLogicUpdateSystem.StartCoroutine(DownloadingFile(item.Url, item.FilePath, cancellationToken,
                                                                                  (ne) => { if (!ne)
                                                                                            {
                                                                                                noError = ne;
                                                                                            }
                                                                                  },
                                                                                  (percents) =>
                {
                    int currentPercents = Mathf.RoundToInt((100 * currentI + percents) / (float)currentCount);
                    onProgressChanged?.Invoke(currentPercents);
                })));
            }
            onCompleted?.Invoke(noError);
        }
    void ProcessFileTransmissionInfo(byte[] data)
    {
        string datastr = encoding.GetString(data);

        byte[] filedatabuffer;

        if (datastr.StartsWith("Req:"))
        {
            ResourceData resource = ResourceManager.GetResource(datastr.Substring(4, datastr.IndexOf("$") - 4));
            if (!FTContainer.UploadQueue.Contains(resource.path))
            {
                UploadFile(resource);
            }
            return;
        }

        if (datastr.StartsWith("FT:"))
        {
            int    infodelim = datastr.IndexOf("$"), iddelim = datastr.IndexOf('|');
            string filename = datastr.Substring(3, iddelim - 3), Id = datastr.Substring(iddelim + 1, infodelim - iddelim - 1);
            if (ResourceManager.GetResource(Id) != null)
            {
                FTContainer.Transdata = ResourceManager.GetResource(Id);
                FTContainer.Transdata.ChangeResourcePath(Path.GetDirectoryName(FTContainer.Transdata.path) + filename, FTContainer.Transdata.URI.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative);
                FTContainer.IsReceivingUpdate = true;
            }
            else
            {
                FTContainer.Transdata = new ResourceData(ResourceManager.GetDLResourceFolderPath() + filename, Id, UriKind.Relative);
            }
            string cstr = datastr.Substring(0, infodelim + 1);

            FTContainer.filestream = new FileStream(FTContainer.Transdata.path, FileMode.Create);

            int clength = data.Length - encoding.GetByteCount(cstr);
            if (clength > 0)
            {
                filedatabuffer = new byte[clength];
                Buffer.BlockCopy(data, encoding.GetByteCount(cstr), filedatabuffer, 0, filedatabuffer.Length);
                FTContainer.filestream.Write(filedatabuffer, 0, filedatabuffer.Length);
            }
            else
            {
                filedatabuffer = new byte[0];
            }

            DebugLogging("File Receiving..." + FTContainer.Transdata.name + FTContainer.Transdata.extension + " Id : " + FTContainer.Transdata.Id);
            DebugLogging("Received File Data :" + FTContainer.filestream.Length + " ( +" + FTContainer.filestream.Length);

            if (datastr.EndsWith("$End"))
            {
                FTContainer.filestream?.Close();
                if (FTContainer.IsReceivingUpdate)
                {
                    FTContainer.Transdata.IssueFinishedUpdateNotify();
                }
                else
                {
                    ResourceManager.AddResource(FTContainer.Transdata);
                }
                OnDownloadCompleted?.Invoke(FTContainer);

                DebugLogging("File Received successfully " + FTContainer.Transdata.name + FTContainer.Transdata.extension + " Id : " + FTContainer.Transdata.Id);

                FTContainer.IsReceivingUpdate = false;
                FTContainer.Transdata         = null;
            }
        }
        else if (datastr.EndsWith("$End"))
        {
            int slength = data.Length - encoding.GetByteCount("$End");
            if (slength > 0)
            {
                byte[] fdata = new byte[data.Length - encoding.GetByteCount("$End")];
                Buffer.BlockCopy(data, 0, fdata, 0, fdata.Length);
                //FTContainer.filebuffer = FTContainer.filebuffer.Concat(fdata).ToArray();
                FTContainer.filestream.Write(fdata, 0, fdata.Length);

                DebugLogging("Received File Data :" + FTContainer.filestream.Length + " ( +" + fdata.Length);
            }
            try
            {
                FTContainer.filestream?.Close();
                if (FTContainer.IsReceivingUpdate)
                {
                    FTContainer.Transdata.IssueFinishedUpdateNotify();
                }
                else
                {
                    ResourceManager.AddResource(FTContainer.Transdata);
                }
                OnDownloadCompleted?.Invoke(FTContainer);

                DebugLogging("File Received successfully " + FTContainer.Transdata.name + FTContainer.Transdata.extension + " Id : " + FTContainer.Transdata.Id);

                FTContainer.IsReceivingUpdate = false;
                FTContainer.Transdata         = null;
            }
            catch (Exception ex)
            {
                DebugLogging(ex.Message + "\n" + ex.StackTrace);
            }
        }
        else
        {
            if (!FTContainer.filestream.CanWrite)
            {
                return;
            }

            FTContainer.filestream.Write(data, 0, data.Length);
            //FTContainer.filebuffer = FTContainer.filebuffer.Concat(data).ToArray();

            DebugLogging("Received File Data :" + FTContainer.filestream.Length + " ( +" + data.Length);
        }
    }
Пример #17
0
 public void DownloadFileAsync(string url, string filePath, ICancellationToken cancellationToken, OnDownloadCompleted onCompleted, OnDownloadProgressChanged onProgressChanged)
 => GameLogicUpdateSystem.StartCoroutine(DownloadingFile(url, filePath, cancellationToken, onCompleted, onProgressChanged));
Пример #18
0
 internal void TriggerDownloadCompletedEvent(DownloadDetails details)
 {
     details.UpdateStatus(DownloadDetails.Status.Downloaded);
     OnDownloadCompleted?.Invoke(this, details);
 }
Пример #19
0
 public void DownloadFileAsync(UrlFilePath urlFilePath, ICancellationToken cancellationToken, OnDownloadCompleted onCompleted, OnDownloadProgressChanged onProgressChanged)
 => DownloadFileAsync(urlFilePath.Url, urlFilePath.FilePath, cancellationToken, onCompleted, onProgressChanged);
Пример #20
0
        public async Task <byte[]> DownloadFileAsync(string path, string share, string filename, CancellationToken token = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            if (string.IsNullOrEmpty(share))
            {
                throw new ArgumentNullException("share");
            }

            if (string.IsNullOrEmpty("filename"))
            {
                throw new ArgumentNullException("filename");
            }

            Exception error = null;
            Stopwatch watch = new Stopwatch();

            watch.Start();
            double time             = watch.Elapsed.TotalMilliseconds;
            long   bytesTransferred = 0;

            byte[] buffer = null;

            try
            {
                CloudFileShare     choudShare = client.GetShareReference(share);
                CloudFileDirectory dir        = choudShare.GetRootDirectoryReference();
                CloudFile          file       = dir.GetFileReference(filename);

                IProgress <StorageProgress> progressHandler = new Progress <StorageProgress>(
                    progress =>
                {
                    bytesTransferred = bytesTransferred < progress.BytesTransferred ? progress.BytesTransferred : bytesTransferred;
                    if (watch.Elapsed.TotalMilliseconds > time + 1000.0 && bytesTransferred <= progress.BytesTransferred)
                    {
                        OnDownloadBytesTransferred?.Invoke(this, new BytesTransferredEventArgs(share, filename, bytesTransferred, file.Properties.Length));
                    }
                });

                buffer = new byte[file.Properties.Length];
                await file.DownloadRangeToByteArrayAsync(buffer, 0, 0, buffer.Length, default(AccessCondition), default(FileRequestOptions), default(OperationContext), progressHandler, token);
            }
            catch (Exception ex)
            {
                if (ex.InnerException is TaskCanceledException)
                {
                    buffer = null;
                }
                else
                {
                    error = ex;
                    throw ex;
                }
            }
            finally
            {
                watch.Stop();
                OnDownloadCompleted?.Invoke(this, new BlobCompleteEventArgs(share, filename, token.IsCancellationRequested, error));
            }

            return(buffer);
        }
Пример #21
0
 public void DownloadFilesAsync(List <UrlFilePath> urlFilePaths, ICancellationToken cancellationToken, OnDownloadCompleted onCompleted, OnDownloadProgressChanged onProgressChanged)
 => GameLogicUpdateSystem.StartCoroutine(DownloadingFiles(urlFilePaths, cancellationToken, onCompleted, onProgressChanged));
        void web_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            try
            {
                string page = "";

                using (StreamReader sr = new StreamReader(e.Result))
                {
                    page = sr.ReadToEnd();
                }

                e.Result.Close();

                RacingEvent f1Event = new RacingEvent();
                f1Event.Circuit = "";
                f1Event.Country = "unknown";

                List <SessionInfo> sessions = new List <SessionInfo>();

                Match m = Regex.Match(page, "<tbody .*? class=\".*?next-event.*?\">(.*?)</tbody>", RegexOptions.Singleline);
                if (m.Groups.Count > 1)
                {
                    string str;
                    Match  ms = Regex.Match(m.Groups[1].Value, "<tr class=\"first-practice.*?>(.*?)</tr>", RegexOptions.Singleline);
                    if (ms.Groups.Count > 1)
                    {
                        SessionInfo practice1 = new SessionInfo();
                        practice1.Type = "Practice 1";

                        str = ms.Groups[1].Value;
                        var date = Regex.Match(str, "<td class=\"date-column\">(.*?)</td>", RegexOptions.Singleline);
                        if (date.Groups.Count > 1)
                        {
                            var time = Regex.Match(date.Groups[1].Value, "<abbr .*? title=\"(.*?)\".*?</abbr>", RegexOptions.Singleline);
                            if (time.Groups.Count > 1)
                            {
                                practice1.Start = DateTime.Parse(time.Groups[1].Value);
                                practice1.Time  = SessionTimeSpan.Practice1;
                                sessions.Add(practice1);
                            }
                        }
                    }

                    ms = Regex.Match(m.Groups[1].Value, "<tr class=\"second-practice.*?>(.*?)</tr>", RegexOptions.Singleline);
                    if (ms.Groups.Count > 1)
                    {
                        SessionInfo practice2 = new SessionInfo();
                        practice2.Type = "Practice 2";

                        str = ms.Groups[1].Value;
                        var date = Regex.Match(str, "<td class=\"date-column\">(.*?)</td>", RegexOptions.Singleline);
                        if (date.Groups.Count > 1)
                        {
                            var time = Regex.Match(date.Groups[1].Value, "<abbr .*? title=\"(.*?)\".*?</abbr>", RegexOptions.Singleline);
                            if (time.Groups.Count > 1)
                            {
                                practice2.Start = DateTime.Parse(time.Groups[1].Value);
                                practice2.Time  = SessionTimeSpan.Practice2;
                                sessions.Add(practice2);
                            }
                        }
                    }

                    ms = Regex.Match(m.Groups[1].Value, "<tr class=\"third-practice.*?>(.*?)</tr>", RegexOptions.Singleline);
                    if (ms.Groups.Count > 1)
                    {
                        SessionInfo practice3 = new SessionInfo();
                        practice3.Type = "Practice 3";

                        str = ms.Groups[1].Value;
                        var date = Regex.Match(str, "<td class=\"date-column\">(.*?)</td>", RegexOptions.Singleline);
                        if (date.Groups.Count > 1)
                        {
                            var time = Regex.Match(date.Groups[1].Value, "<abbr .*? title=\"(.*?)\".*?</abbr>", RegexOptions.Singleline);
                            if (time.Groups.Count > 1)
                            {
                                practice3.Start = DateTime.Parse(time.Groups[1].Value);
                                practice3.Time  = SessionTimeSpan.Practice3;
                                sessions.Add(practice3);
                            }
                        }
                    }

                    ms = Regex.Match(m.Groups[1].Value, "<tr class=\"qualifying.*?>(.*?)</tr>", RegexOptions.Singleline);
                    if (ms.Groups.Count > 1)
                    {
                        SessionInfo quali = new SessionInfo();
                        quali.Type = "Qualifying";

                        str = ms.Groups[1].Value;
                        var date = Regex.Match(str, "<td class=\"date-column\">(.*?)</td>", RegexOptions.Singleline);
                        if (date.Groups.Count > 1)
                        {
                            var time = Regex.Match(date.Groups[1].Value, "<abbr .*? title=\"(.*?)\".*?</abbr>", RegexOptions.Singleline);
                            if (time.Groups.Count > 1)
                            {
                                quali.Start = DateTime.Parse(time.Groups[1].Value);
                                quali.Time  = SessionTimeSpan.Qualifying;
                                sessions.Add(quali);
                            }
                        }
                    }

                    ms = Regex.Match(m.Groups[1].Value, "<tr class=\"race.*?>(.*?)</tr>", RegexOptions.Singleline);
                    if (ms.Groups.Count > 1)
                    {
                        SessionInfo race = new SessionInfo();
                        race.Type = "Race";

                        str = ms.Groups[1].Value;
                        var date = Regex.Match(str, "<td class=\"date-column\">(.*?)</td>", RegexOptions.Singleline);
                        if (date.Groups.Count > 1)
                        {
                            var time = Regex.Match(date.Groups[1].Value, "<abbr.*?title=\"(.*?)\".*?</abbr>", RegexOptions.Singleline);
                            if (time.Groups.Count > 1)
                            {
                                race.Start = DateTime.Parse(time.Groups[1].Value);
                                race.Time  = SessionTimeSpan.Race;
                                sessions.Add(race);
                            }
                        }
                    }

                    ms = Regex.Match(m.Groups[1].Value, "<span class=\"location\">(.*?)</span>", RegexOptions.Singleline);
                    if (ms.Groups.Count > 1)
                    {
                        f1Event.Country = ms.Groups[1].Value.Trim();
                    }
                }

                SessionInfo selectedSession = null;

                var now = DateTime.Now;
                foreach (var session in sessions)
                {
                    selectedSession = session;
                    if (now < session.Start)
                    {
                        break;
                    }
                    else if (now <= session.Start + session.Time)
                    {
                        break;
                    }
                }

                if (selectedSession != null)
                {
                    f1Event.Session          = selectedSession.Type;
                    f1Event.SessionStartTime = selectedSession.Start;
                    f1Event.UpdateRemainingTime();
                }

                f1Event.DownloadTimestamp = DateTime.Now;

                OnDownloadCompleted.Invoke(f1Event, CompletionStatus.Completed);
            }
            catch (Exception)
            {
                OnDownloadCompleted.Invoke(new RacingEvent(), CompletionStatus.Failed);
            }
        }
Пример #23
0
 public void Method()
 {
     OnDownloadCompleted?.Invoke(this, EventArgs.Empty);
 }
Пример #24
0
        public async Task DownloadBlockBlobToFile(string filePath, string containerName, string blobFilename, CancellationToken token = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filenamePath");
            }

            if (string.IsNullOrEmpty(containerName))
            {
                throw new ArgumentNullException("containerName");
            }

            if (string.IsNullOrEmpty("blobFilename"))
            {
                throw new ArgumentNullException("blobFilename");
            }

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException(filePath);
            }

            Exception error = null;
            Stopwatch watch = new Stopwatch();

            watch.Start();
            double time             = watch.Elapsed.TotalMilliseconds;
            long   bytesTransferred = 0;

            IProgress <StorageProgress> progressHandler = new Progress <StorageProgress>(
                progress =>
            {
                bytesTransferred = bytesTransferred < progress.BytesTransferred ? progress.BytesTransferred : bytesTransferred;
                FileInfo info    = new FileInfo(filePath);
                if (watch.Elapsed.TotalMilliseconds > time + 1000.0 && bytesTransferred <= progress.BytesTransferred)
                {
                    OnDownloadBytesTransferred?.Invoke(this, new BytesTransferredEventArgs(containerName, blobFilename, bytesTransferred, info.Length));
                }
            });

            try
            {
                CloudBlobContainer container = await GetContainerReferenceAsync(containerName);

                CloudBlockBlob blob = container.GetBlockBlobReference(blobFilename);
                await blob.DownloadToFileAsync(filePath, FileMode.Create, default(AccessCondition), default(BlobRequestOptions), default(OperationContext), progressHandler, token);
            }
            catch (Exception ex)
            {
                if (!(ex.InnerException is TaskCanceledException))
                {
                    error = ex;
                    throw ex;
                }
            }
            finally
            {
                watch.Stop();
                OnDownloadCompleted?.Invoke(this, new BlobCompleteEventArgs(containerName, blobFilename, token.IsCancellationRequested, error));
            }
        }
Пример #25
0
        private async Task ProcessDownloadAsync(Uri uri, string filename, CancellationToken ct)
        {
            try
            {
                dynamic request = null;

                #region Get file size

                if (uri.Scheme == Uri.UriSchemeHttp)
                {
                    request          = WebRequest.Create(uri) as HttpWebRequest;
                    request !.Method = WebRequestMethods.Http.Head;
                }
                else if (uri.Scheme == Uri.UriSchemeFtp)
                {
                    request          = WebRequest.Create(uri) as FtpWebRequest;
                    request !.Method = WebRequestMethods.Ftp.GetFileSize;
                }

                var metric = new DownloaderMetric
                {
                    FileName = Path.GetFileName(filename)
                };

                using (var response = await request !.GetResponseAsync())
                {
                    metric.TotalBytes = response.ContentLength;
                }

                #endregion

                // Set start download position
                long startPos = 0;
                var  fi       = new FileInfo(filename);
                if (fi.Exists)
                {
                    if (metric.TotalBytes > fi.Length)
                    {
                        metric.DownloadedBytes = startPos = fi.Length - 1024;
                    }
                    else if (metric.TotalBytes == fi.Length)
                    {
                        return;
                    }
                }

                #region Read Content Stream Asynchronously

                if (uri.Scheme == Uri.UriSchemeHttp)
                {
                    request          = WebRequest.Create(uri) as HttpWebRequest;
                    request          = (HttpWebRequest)request;
                    request !.Method = WebRequestMethods.Http.Get;
                    request.AddRange(startPos, metric.TotalBytes);
                }
                else if (uri.Scheme == Uri.UriSchemeFtp)
                {
                    request               = WebRequest.Create(uri) as FtpWebRequest;
                    request !.Method      = WebRequestMethods.Ftp.DownloadFile;
                    request.ContentOffset = startPos;
                }

                var stopwatch = new Stopwatch();
                stopwatch.Start();
                if (!Directory.Exists(Path.GetDirectoryName(filename)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(filename) !);
                }
                var fileStream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite)
                {
                    Position = metric.DownloadedBytes
                };
                await Task.Run(async() =>
                {
                    if (ct.IsCancellationRequested)
                    {
                        return;
                    }

                    using var streamReader = new StreamReader((await request.GetResponseAsync()).GetResponseStream());
                    OnDownloadStart?.Invoke(metric);
                    FromReaderToFile(streamReader, fileStream, ref metric, ref stopwatch, ct);
                    OnDownloadCompleted?.Invoke(metric);
                }, ct);

                #endregion
            }
            catch (OperationCanceledException)
            {
                const string msg = "Download cancelled by user";
                OnError?.Invoke(new DownloaderClientException(msg));
            }
            catch (Exception ex)
            {
                const string msg = "An unexpected error occurred.";
                OnError?.Invoke(
                    new DownloaderClientException($"{msg}\n\nDownload failed. See inner exception for details.", ex));
            }
        }
        public void Download(IRelease release, string cookie, string tempDir)
        {
            if (release == null)
            {
                throw new ArgumentNullException(nameof(release));
            }

            if (string.IsNullOrWhiteSpace(cookie))
            {
                throw new ArgumentNullException(nameof(cookie));
            }

            if (release.DefaultDistribution == null)
            {
                throw new InvalidOperationException("Release contains no valid default distribution");
            }

            var url      = new Uri(release.DefaultDistribution.Downloads.First(x => x.StartsWith("http")));
            var filename = release.DefaultDistribution.FileNames.First(x => x.EndsWith("zip"));
            var filepath = Path.Combine(tempDir, filename);

            if (File.Exists(filepath))
            {
                // Release has already been downloaded previously; just report back with the filepath
                OnDownloadCompleted?.Invoke(new DownloadResult(filepath, release));
                return;
            }

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

            var downloadOptions = new DownloadOptions
            {
                Uri            = url,
                BlockOptions   = new BlocksCount(1),
                Cookies        = cookie,
                FilePath       = filepath,
                RequestTimeout = new TimeSpan(0, 24, 0, 0)
            };

            var context = new DownloadContext(downloadOptions);

            if (OnProgressChanged != null)
            {
                context.OnProgressChanged += OnProgressChanged;
            }

            if (OnDownloadCompleted != null)
            {
                context.OnDownloadCompleted += () => { OnDownloadCompleted?.Invoke(new DownloadResult(filepath, release)); };
            }

            if (OnErrorOccurred != null)
            {
                context.OnErrorOccurred += OnErrorOccurred;
            }

            context.Download();
        }
Пример #27
0
        //Obsluhuje čtení dat ze serveru a zápis na disk. Běží v samostatném vlákně.
        private void DownloadWorker()
        {
            HttpWebResponse response      = null;
            Stream          receiveStream = null;

            try
            {
                HttpWebRequest request = WebRequest.CreateHttp(Url);
                request.Proxy   = null;
                request.Timeout = requestTimeout;

                if (append)
                {
                    if (File.Exists(FullPath))
                    {
                        FileInfo file = new FileInfo(FullPath);
                        BytesDownloaded = file.Length;
                    }
                    else
                    {
                        BytesDownloaded = 0;
                    }
                }
                if (BytesDownloaded > 0)
                {
                    request.AddRange(BytesDownloaded);
                }

                response = (HttpWebResponse)request.GetResponse();

                //když je FileName prázdný, ještě nebyl získan název souboru ze serveru, jinak se použije stávající
                if (FileName == "")
                {
                    string header = response.Headers["Content-Disposition"];
                    if (header != null)
                    {
                        string s = header.Replace("attachment; ", "").Replace("attachment;", "").Replace("filename=", "").Replace("filename*=UTF-8''", "").Replace("\"", "");

                        // z:  form%c3%a1ln%c3%ad%20%c3%baprava%20podle%20norem.docx
                        // na: formální úprava podle norem.docx

                        FileName = WebUtility.UrlDecode(s);
                    }
                    else
                    {
                        Uri uri = new Uri(Url);
                        FileName = Path.GetFileName(uri.LocalPath);
                    }

                    //pokud již existuje soubor se stejným jménem, přidá se číslo
                    int    i        = 1;
                    string baseName = FileName;
                    while (File.Exists(FullPath) && i <= maxRenameCount)
                    {
                        FileName = $"{Path.GetFileNameWithoutExtension(baseName)} ({i}){Path.GetExtension(baseName)}";
                        i++;
                    }
                    if (File.Exists(FullPath)) //pokud existuje i 999. soubor, vyhodí se error
                    {
                        CallError(Lang.Translate("lang_file_exists"));
                        return;
                    }
                }

                receiveStream = response.GetResponseStream();

                //když se navazuje stahování, ale celková velikost souboru se liší - soubor na serveru byl změněn,
                //nebo odkaz přestal platit
                if (append && TotalBytes > 0 && TotalBytes != BytesDownloaded + response.ContentLength)
                {
                    CallError(Lang.Translate("lang_file_size_mismatch"));
                }
                else
                {
                    System.IO.Directory.CreateDirectory(Directory);
                    if (BytesDownloaded > 0)
                    {
                        fs = new FileStream(FullPath, FileMode.Append, FileAccess.Write, FileShare.Read);
                    }
                    else
                    {
                        fs = new FileStream(FullPath, FileMode.Create, FileAccess.Write, FileShare.Read);
                    }

                    TotalBytes = BytesDownloaded + response.ContentLength;

                    operation.Post(new SendOrPostCallback(delegate(object state)
                    {
                        OnDownloadInit?.Invoke(this);
                    }), null);

                    byte[] buffer = new byte[bufferSize];
                    int    receivedCount;
                    while ((receivedCount = receiveStream.Read(buffer, 0, bufferSize)) > 0 && State != States.Canceled) //dokud není přečten celý stream
                    {
                        fs.Write(buffer, 0, receivedCount);
                        BytesDownloaded += receivedCount;
                        processed       += receivedCount;

                        while (State == States.Paused || State == States.Queue)
                        {
                            Thread.Sleep(pauseSleep);
                        }
                        if (State == States.Starting)
                        {
                            State = States.Downloading;
                        }

                        if (SpeedLimit > 0 && processed >= SpeedLimit)
                        {
                            Thread.Sleep(speedlimitSleep);
                        }
                    }
                }
            }
            catch (WebException e) //server neexistuje; errory 400, 500;
            {
                CallError(e.Message);
            }
            catch (ArgumentException e)
            {
                CallError(e.Message);
            }
            catch (IOException e) //k souboru nelze přistupovat
            {
                CallError(e.Message);
            }
            catch (Exception e)
            {
                CallError(e.Message);
            }
            finally
            {
                response?.Close();
                receiveStream?.Close();
                fs?.Close();
                timer.Stop();
                BytesPerSecond = 0;
                processed      = 0;

                if (State == States.Canceled)
                {
                    File.Delete(FullPath);
                }
                else if (BytesDownloaded == TotalBytes && TotalBytes > 0)
                {
                    State = States.Completed;
                    operation.Post(new SendOrPostCallback(delegate(object state)
                    {
                        OnDownloadCompleted?.Invoke(this);
                    }), null);
                }
                else if (State != States.Error)
                {
                    Console.WriteLine("restarting");
                    State = States.Starting;
                    timer.Start();
                    DownloadWorker();
                }
            }
        }