1: StringBuilder _errors = new StringBuilder();
2: private string _destinationPath = string.Empty;
3:
4: private const string FILENAME_FORMATSTRING = "{0}\\{1}_{2}.wmv"; 5: private const string DATA_FILE_URI = "http://www.franksworld.com/Downloads/PDC09Sessions.xml";
6:
7:
8: public Window1()
9: { 10: InitializeComponent();
11: }
12:
13: private void Window_Loaded(object sender, RoutedEventArgs e)
14: { 15: this._destinationPath = PickDownloadDirectory();
16:
17: if (this._destinationPath == null)
18: { 19: Application.Current.Shutdown();
20: }
21:
22:
23: }
24:
25:
26: private void btnStart_Click(object sender, RoutedEventArgs e)
27: { 28: this.lblStatus.Content = "Downloading Data File";
29:
30: XDocument dataFile = XDocument.Load(DATA_FILE_URI);
31:
32: var sessionNodes = dataFile.Descendants("session"); 33:
34:
35: DownLoadFiles(sessionNodes);
36:
37:
38:
39: }
40:
41:
42: private string PickDownloadDirectory()
43: { 44:
45: string returnDirectory = null;
46:
47:
48: Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
49: dlg.FileName = "";
50: dlg.DefaultExt = ".*";
51: dlg.Filter = "All documents (*.*)|*.*";
52:
53: // Show open file dialog box
54: Nullable<bool> result = dlg.ShowDialog();
55:
56: // Process open file dialog box results
57: if (result == true)
58: { 59: // Open document
60: string filename = dlg.FileName;
61:
62:
63: returnDirectory = System.IO.Path.GetDirectoryName(filename);
64:
65:
66: }
67:
68: return returnDirectory;
69:
70: }
71:
72:
73: private void DownLoadFiles(IEnumerable<XElement> sessionNodes)
74: { 75: var sessionList = from session in sessionNodes
76: select new Session
77: { 78: SessionId = (string)session.Element("code"), 79: WMVHigh = GetUri( session.Descendants("resources") , "WMVHigh" ), 80: Name = GetSessionName(session)
81: };
82:
83: sessionList.ToList().ForEach(x => DownLoadFile(x));
84:
85:
86:
87: MessageBox.Show(_errors.ToString());
88:
89:
90:
91:
92:
93:
94: }
95:
96: private string GetSessionName(XElement session)
97: { 98:
99: // THIS is *UGLY* LINQ code. I need to fix
100: var resources = session.Descendants("resources"); 101:
102: var webResource = resources.Where(x => x.Attribute("ContentType").Value == "Web"); 103:
104: string name = string.Empty;
105:
106: webResource.ToList().ForEach(x => name = x.Value);
107:
108: return name;
109:
110: }
111:
112: private void DownLoadFile(Session session)
113: { 114:
115: try
116: { 117: WebClient wc = new WebClient();
118:
119: string destinationFileName = string.Format(FILENAME_FORMATSTRING, _destinationPath, session.SessionId, session.Name);
120:
121: if (!File.Exists(destinationFileName))
122: { 123: this.lblStatus.Content = "Copying " + destinationFileName;
124: wc.DownloadFile(session.WMVHigh, destinationFileName);
125: }
126: }
127: catch (Exception ex)
128: { 129:
130: _errors.AppendLine("session: " + session.SessionId + ": error: " + ex.Message); 131: }
132: }
133:
134: private string GetUri(IEnumerable<XElement> iEnumerable, string p)
135: { 136:
137: var wmvnode = iEnumerable.Where(node => node.Attribute("ContentType").Value == p); 138:
139: return wmvnode.Attributes("uri").First().Value; 140:
141: }