File size: 3,196 Bytes
e26fba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Text;
using MovieTube.Client.Scraper;
using System.Net;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace MovieTube.Viewer
{
    public class DownloaderService : IScraperServiceCallback
    {
        private WebClient client = new WebClient();
        private IDownloadProgress dpClient;
        private DownloadInfo di;
        private bool stop;
        public void Download(DownloadInfo di, IDownloadProgress dpClient)
        {
            stop = false;
            this.dpClient = dpClient;
            this.di = di;
             if (di.Link.ScrapState == LinkScrapState.FullyLoaded)
                OnScrapVideoDetailsCompleted(di.Link);
            else
                new ScraperService().ScrapVideosDetailsAsync(this, di.Link);
        }

        public void Stop()
        {
            lock (this.client)
            {
                try
                {
                    this.stop = true;
                    this.client.CancelAsync();
                }
                catch { }
            }
        }

        public void OnScrapVideoDetailsCompleted(MovieLink link)
        {
            di.FilePath = CreateFilename(link);
            DoDownload(link, di.FilePath);
        }
        
        private string CreateFilename(MovieLink link)
        {
            string fileName = link.Parent.Name + "_" + Path.GetFileName(new Uri(link.PlayUrl).AbsolutePath);
            foreach (char c in System.IO.Path.GetInvalidFileNameChars())
                fileName = fileName.Replace(c, '_');

            var f = fileName;
            for(var i = 0 ; i < 1000; i++)
            {

                f = Path.Combine(AppSettings.MovieDownloadFolder, f);
                if (File.Exists(f))
                {
                    var f1 = Path.GetFileNameWithoutExtension(fileName);
                    var f2 = Path.GetExtension(fileName);
                    f = String.Format("{0}({1}){2}", f1, i, f2);
                    continue;
                }

            }
            return f;
        }

        public void OnScrapError(MovieLink link, string message)
        {
            
        }

        private void DoDownload(MovieLink movieLink, string filePath)
        {
            lock (client)
            {
                if (this.stop)
                    return;
                client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(client_DownloadFileCompleted);
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadFileAsync(new Uri(movieLink.PlayUrl), filePath);
            }
        }

        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            this.dpClient.OnProgress(this.di, e);
        }

        void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            this.dpClient.OnCompletion(this.di, e);
        }
    }
}