Skip to content

Commit

Permalink
Fix Some Bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost1372 committed Nov 1, 2023
1 parent 56e2438 commit 18d8027
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions dev/ViewModels/Qari/DownloadQariViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.ObjectModel;

using System.Text.RegularExpressions;
using ColorCode.Compilation.Languages;
using Downloader;

using Newtonsoft.Json;

namespace AlAnvar.ViewModels;
Expand Down Expand Up @@ -50,7 +50,7 @@ private async void OnPageLoaded()
await Task.Run(async () =>
{
using var db = new AlAnvarDBContext();
var data = await db.Audios.OrderBy(x=>x.Name).ToListAsync();
var data = await db.Audios.OrderBy(x => x.Name).ToListAsync();
QuranAudios = new(data);
dispatcherQueue.TryEnqueue(() =>
{
Expand Down Expand Up @@ -137,8 +137,18 @@ private async void DownloadQariAsync()
}
else
{
var audioUrl = Path.Combine(audioItem.Url, id);
var audioFilePath = Path.Combine(dirPath, id);
var audioUrlPath = id;
if (audioItem.Url.EndsWith("/") && audioUrlPath.StartsWith("/"))
{
audioUrlPath = audioUrlPath.Remove(0, 1);
}
else if (!audioItem.Url.EndsWith("/") && !audioUrlPath.StartsWith("/"))
{
audioUrlPath = audioUrlPath.Insert(0, "/");
}

var finalAudioUrlPath = Path.Combine(audioItem.Url, audioUrlPath);
var audioFilePath = Path.Combine(dirPath, finalAudioUrlPath);
if (File.Exists(audioFilePath))
{
_downloadedtItemCount += 1;
Expand All @@ -149,7 +159,7 @@ private async void DownloadQariAsync()
downloadService = new DownloadService();
downloadService.DownloadProgressChanged += Downloader_DownloadProgressChanged;
downloadService.DownloadFileCompleted += Downloader_DownloadFileCompleted;
await downloadService.DownloadFileTaskAsync(audioUrl, new DirectoryInfo(dirPath));
await downloadService.DownloadFileTaskAsync(finalAudioUrlPath, new DirectoryInfo(dirPath));
}
}
}
Expand Down Expand Up @@ -191,18 +201,28 @@ private void Downloader_DownloadProgressChanged(object sender, DownloadProgressC
private List<string> GetAudioIds(string fileName)
{
List<string> quranAudioIds = new List<string>();
using var streamReader = File.OpenText(fileName);
string line = String.Empty;
while ((line = streamReader.ReadLine()) != null)

string content;
using (StreamReader sr = new StreamReader(fileName))
{
if (line.Contains("href"))
{
var audioId = line.Replace("<a href=\"", "");
var lastIndex = audioId.IndexOf("\">");
audioId = audioId.Substring(0, lastIndex);
quranAudioIds.Add(audioId);
}
content = sr.ReadToEnd();
}

if (string.IsNullOrEmpty(content))
{
return null;
}

string pattern = "href=\"(.*?\\.mp3)\"";
Regex regex = new Regex(pattern);

foreach (Match match in regex.Matches(content))
{
string mp3Link = match.Groups[1].Value;
mp3Link = Path.GetFileName(mp3Link);
quranAudioIds.Add(mp3Link);
}

return quranAudioIds;
}

Expand Down

0 comments on commit 18d8027

Please sign in to comment.