-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNaoOuvoManager.cs
130 lines (105 loc) · 4.73 KB
/
NaoOuvoManager.cs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using PodcastManager.Enums;
using PodcastManager.Helpers;
using PodcastManager.Interfaces;
using PodcastManager.Models.NaoOuvo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace PodcastManager.PodcastManagers
{
internal class NaoOuvoManager : IManager
{
public PodcastType Type => PodcastType.NaoOuvo;
private const string NAOOUVO_FEED = "http://feeds.feedburner.com/naoouvo/";
private List<string> currentFilesDownloading = new List<string>();
private readonly IFileHelper _fileHelper;
private WebApiClient WebClient;
public NaoOuvoManager(IFileHelper fileHelper)
{
_fileHelper = fileHelper;
var handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
WebClient = new WebApiClient(new HttpClient(handler), _fileHelper);
}
public async Task<List<IPodcastEpisode>> GetPodcastListAsync()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
var xmlString = await WebClient.GetResponseStringAsync(NAOOUVO_FEED);
var feedXmlDocument = XDocument.Parse(xmlString.Replace("itunes:","itunes").Replace("media:", "media"));
var episodeList = feedXmlDocument.Descendants()
.Where(e=>e.Name.LocalName.Equals("item", StringComparison.OrdinalIgnoreCase));
return episodeList.Select(e =>
{
var reader = new StringReader(e.ToString());
var xmlSerializer = new XmlSerializer(typeof(Episode));
return (IPodcastEpisode)xmlSerializer.Deserialize(reader);
}).ToList();
}
return null;
}
public async Task<bool> DownloadPodcastAsync(
IPodcastEpisode episode, string directoryOut, AsyncCompletedEventHandler completedDownloadEvent = null,
DownloadProgressChangedEventHandler downloadProgressChangedEvent = null, bool downloadInsertions = false)
{
try
{
if (string.IsNullOrEmpty(directoryOut))
return false;
if (!_fileHelper.DirectoryExists(directoryOut))
_fileHelper.CreateDirectory(directoryOut);
if (episode is Episode naoOuvoEpisode && _fileHelper.DirectoryExists(directoryOut))
{
var archiveName = naoOuvoEpisode.Url.Substring(naoOuvoEpisode.Url.LastIndexOf("/") + 1);
if (currentFilesDownloading.Any(name => name.Equals(archiveName, StringComparison.CurrentCultureIgnoreCase)))
return false;
var httpclient = new HttpClient()
{
Timeout = (new TimeSpan(5, 0, 0))
};
var client = new WebApiClient(httpclient, _fileHelper);
currentFilesDownloading.Add(archiveName);
directoryOut = directoryOut.EndsWith("\\") ? directoryOut : directoryOut + "\\";
if (downloadInsertions)
await DownloadNaoOuvoInsertions(naoOuvoEpisode, directoryOut, archiveName);
var filePath = string.Concat(directoryOut, archiveName);
await client.GetAsync(naoOuvoEpisode.Url, filePath);
currentFilesDownloading.Remove(archiveName);
completedDownloadEvent?.Invoke(null, null);
return true;
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error on NaoOuvo download.\n\nDetails{ex.Message}");
return false;
}
return false;
}
private async Task DownloadNaoOuvoInsertions(
Episode naoOuvoEpisode, string directoryOut, string archiveName)
{
var naoOuvoDirectory = string.Concat(
directoryOut,
Path.GetFileNameWithoutExtension(archiveName),
"\\");
if (!_fileHelper.DirectoryExists(naoOuvoDirectory))
_fileHelper.CreateDirectory(naoOuvoDirectory);
using (var client = new WebClient())
{
await client.DownloadFileTaskAsync(new Uri(naoOuvoEpisode.ImageObject.Href), $"{naoOuvoDirectory}Cover.jpg");
}
}
}
}