1
1
using System . Collections . ObjectModel ;
2
-
2
+ using System . Text . RegularExpressions ;
3
+ using ColorCode . Compilation . Languages ;
3
4
using Downloader ;
4
-
5
5
using Newtonsoft . Json ;
6
6
7
7
namespace AlAnvar . ViewModels ;
@@ -50,7 +50,7 @@ private async void OnPageLoaded()
50
50
await Task . Run ( async ( ) =>
51
51
{
52
52
using var db = new AlAnvarDBContext ( ) ;
53
- var data = await db . Audios . OrderBy ( x=> x . Name ) . ToListAsync ( ) ;
53
+ var data = await db . Audios . OrderBy ( x => x . Name ) . ToListAsync ( ) ;
54
54
QuranAudios = new ( data ) ;
55
55
dispatcherQueue . TryEnqueue ( ( ) =>
56
56
{
@@ -137,8 +137,18 @@ private async void DownloadQariAsync()
137
137
}
138
138
else
139
139
{
140
- var audioUrl = Path . Combine ( audioItem . Url , id ) ;
141
- var audioFilePath = Path . Combine ( dirPath , id ) ;
140
+ var audioUrlPath = id ;
141
+ if ( audioItem . Url . EndsWith ( "/" ) && audioUrlPath . StartsWith ( "/" ) )
142
+ {
143
+ audioUrlPath = audioUrlPath . Remove ( 0 , 1 ) ;
144
+ }
145
+ else if ( ! audioItem . Url . EndsWith ( "/" ) && ! audioUrlPath . StartsWith ( "/" ) )
146
+ {
147
+ audioUrlPath = audioUrlPath . Insert ( 0 , "/" ) ;
148
+ }
149
+
150
+ var finalAudioUrlPath = Path . Combine ( audioItem . Url , audioUrlPath ) ;
151
+ var audioFilePath = Path . Combine ( dirPath , finalAudioUrlPath ) ;
142
152
if ( File . Exists ( audioFilePath ) )
143
153
{
144
154
_downloadedtItemCount += 1 ;
@@ -149,7 +159,7 @@ private async void DownloadQariAsync()
149
159
downloadService = new DownloadService ( ) ;
150
160
downloadService . DownloadProgressChanged += Downloader_DownloadProgressChanged ;
151
161
downloadService . DownloadFileCompleted += Downloader_DownloadFileCompleted ;
152
- await downloadService . DownloadFileTaskAsync ( audioUrl , new DirectoryInfo ( dirPath ) ) ;
162
+ await downloadService . DownloadFileTaskAsync ( finalAudioUrlPath , new DirectoryInfo ( dirPath ) ) ;
153
163
}
154
164
}
155
165
}
@@ -191,18 +201,28 @@ private void Downloader_DownloadProgressChanged(object sender, DownloadProgressC
191
201
private List < string > GetAudioIds ( string fileName )
192
202
{
193
203
List < string > quranAudioIds = new List < string > ( ) ;
194
- using var streamReader = File . OpenText ( fileName ) ;
195
- string line = String . Empty ;
196
- while ( ( line = streamReader . ReadLine ( ) ) != null )
204
+
205
+ string content ;
206
+ using ( StreamReader sr = new StreamReader ( fileName ) )
197
207
{
198
- if ( line . Contains ( "href" ) )
199
- {
200
- var audioId = line . Replace ( "<a href=\" " , "" ) ;
201
- var lastIndex = audioId . IndexOf ( "\" >" ) ;
202
- audioId = audioId . Substring ( 0 , lastIndex ) ;
203
- quranAudioIds . Add ( audioId ) ;
204
- }
208
+ content = sr . ReadToEnd ( ) ;
205
209
}
210
+
211
+ if ( string . IsNullOrEmpty ( content ) )
212
+ {
213
+ return null ;
214
+ }
215
+
216
+ string pattern = "href=\" (.*?\\ .mp3)\" " ;
217
+ Regex regex = new Regex ( pattern ) ;
218
+
219
+ foreach ( Match match in regex . Matches ( content ) )
220
+ {
221
+ string mp3Link = match . Groups [ 1 ] . Value ;
222
+ mp3Link = Path . GetFileName ( mp3Link ) ;
223
+ quranAudioIds . Add ( mp3Link ) ;
224
+ }
225
+
206
226
return quranAudioIds ;
207
227
}
208
228
0 commit comments