예제 #1
0
        public OnelineVideoInfo SetParameters(params Tuple <string, string>[] parameters)
        {
            Dictionary <string, string> paramMap;
            string tail;

            ExtractParameters(out paramMap, out tail);

            foreach (var item in parameters.Reverse())
            {
                var name  = item.Item1.ToLower();
                var value = item.Item2;
                paramMap[name] = value;
            }

            var newParamsSec = paramMap.Keys.Select(x => string.Format(";<{0}>{1}", x, paramMap[x]));
            var newParams    = string.Join(string.Empty, newParamsSec);

            string beforeComment = Regex.Match(line, WATCH_PATTERN).Groups["beforeComment"].Value;
            string newLine;

            if (tail == null)
            {
                newLine = string.Format("{0}{1}", beforeComment, newParams);
            }
            else
            {
                newLine = string.Format("{0}{1};{2}", beforeComment, newParams, tail);
            }

            return(OnelineVideoInfo.FromString(newLine));
        }
        private void StartDownload()
        {
            string nextUrl = null;

            if (!downloadWorker.Timer.CanAccess("urn:uuid:45755c6f-01e2-4a38-6f5c-7545e201384a"))
            {
                return;
            }

            var lines = WithEditQueueingUrls(currentLines => {
                List <OnelineVideoInfo> newLines = new List <OnelineVideoInfo>();

                var query = currentLines
                            .SkipWhile(x => string.IsNullOrEmpty(x))
                            .Reverse()
                            .SkipWhile(x => string.IsNullOrEmpty(x))
                            .Select(x => OnelineVideoInfo.FromString(x));

                int index = 0;
                foreach (var line in query)
                {
                    if (nextUrl == null)
                    {
                        if (line.IsWatchUrl || line.IsMylistUrl)
                        {
                            nextUrl = line.ToString();
                        }
                        else
                        {
                            index++;
                            newLines.Insert(0, line);
                        }
                    }
                    else
                    {
                        newLines.Insert(index, line);
                    }
                }

                return(newLines
                       .Select(x => x.ToString()));
            });

            if (nextUrl != null)
            {
                if (OnelineVideoInfo.FromString(nextUrl).IsMylistUrl)
                {
                    ExtractMylist(nextUrl);
                    return;
                }
                else
                {
                    workingUrl = OnelineVideoInfo.FromString(nextUrl);
                }
            }
            else
            {
                queueingUrls.ReadOnly    = false;
                onlineController.Enabled = true;
                return;
            }

            smng.RemoveAll <string>(SettingsConstants.URL);
            foreach (string line in lines)
            {
                smng.AddItem(SettingsConstants.URL, line);
            }

            reloadSettingsButton.Enabled = false;
            downloadWorker.WatchUrl      = workingUrl.Url;
            downloadWorker.DownloadAsync(null);
            label1.Text             = workingUrl.Id;
            progressBar1.Value      = 0;
            interceptButton.Enabled = true;
            etaSource.Clear();
            downloadStartTime = DateTime.MinValue;
        }
        private void downloadWorker1_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            Contract.Requires(workingUrl.IsWatchUrl);

            if (downloadStartTime == DateTime.MinValue)
            {
                downloadStartTime = DateTime.Now;
            }

            if (progressBar1.Maximum != e.TotalBytesToReceive)
            {
                progressBar1.Maximum = (int)e.TotalBytesToReceive;
            }
            progressBar1.Value = (int)e.BytesReceived;
            string id = workingUrl.Id;

            if (e.WillWait > 0)
            {
                label1.Text = string.Format("{0} (waiting {1}s)", id, e.WillWait / 1000.0);
            }
            else if (e.Title != null)
            {
                label1.Text = string.Format("{0} ({1}/{2}) {3}", id, e.BytesReceived, e.TotalBytesToReceive, e.Title);
            }
            else
            {
                label1.Text = string.Format("{0} ({1}/{2})", id, e.BytesReceived, e.TotalBytesToReceive);
            }
            if (string.IsNullOrEmpty(workingUrl.GetCommentTail()) && e.Title != null)
            {
                workingUrl = OnelineVideoInfo.FromString(string.Format("{0};{1}", workingUrl.ToString(), e.Title));
            }

            var now = DateTime.Now;

            if ((now - downloadStartTime >= TimeSpan.FromSeconds(10)) && (e.BytesReceived > 0))
            {
                if (etaSource.First != null)
                {
                    var etaUpdateInterval = TimeSpan.FromMilliseconds(500);
                    if (now - etaSource.First.Value.Item1 >= etaUpdateInterval)
                    {
                        LinkedListNode <Tuple <DateTime, long> > lastNode = null;
                        var averageSpan = TimeSpan.FromSeconds(30);
                        foreach (var node in EnumerateLinkedListNodes(etaSource))
                        {
                            lastNode = node;
                            if (now - node.Value.Item1 >= averageSpan)
                            {
                                break;
                            }
                        }
                        while (etaSource.Last != lastNode)
                        {
                            etaSource.Remove(etaSource.Last);
                        }
                        var bytesDiff = e.BytesReceived - lastNode.Value.Item2;
                        if (bytesDiff > 0)
                        {
                            var timeDiff       = (now - lastNode.Value.Item1).Ticks;
                            var bytesRemaining = e.TotalBytesToReceive - e.BytesReceived;
                            try {
                                var etaTicks  = timeDiff * bytesRemaining / bytesDiff;
                                var eta       = TimeSpan.FromTicks(etaTicks);
                                var etaString = string.Format(" (-{0}:{1,2:d2})", Math.Floor(eta.TotalMinutes), eta.Seconds);
                                etaLabel.Visible = true;
                                etaLabel.Text    = etaString;
                            }
                            catch (OverflowException overflowError) {
                                Debug.WriteLine(overflowError);
                            }
                        }
                        etaSource.AddFirst(Tuple.Create(now, e.BytesReceived));
                    }
                    else
                    {
                        // do nothing
                    }
                }
                else
                {
                    etaSource.AddFirst(Tuple.Create(now, e.BytesReceived));
                }
            }
        }