Skip to content

Commit

Permalink
Fixed issues with special characters in the file name
Browse files Browse the repository at this point in the history
  • Loading branch information
bertyhell committed Dec 9, 2016
1 parent 43be085 commit 32cc4e7
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
3 changes: 3 additions & 0 deletions PlaylistDownloader/PlaylistDownloader/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
</dependentAssembly>
</assemblyBinding>
</runtime>
<appSettings>
<add key="debug" value="false" />
</appSettings>
</configuration>
27 changes: 14 additions & 13 deletions PlaylistDownloader/PlaylistDownloader/Downloader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand All @@ -14,13 +15,15 @@ public class Downloader : BackgroundWorker
{

private readonly ICollection<PlaylistItem> _playlist;
private readonly bool _isDebugMode = bool.Parse(ConfigurationManager.AppSettings.Get("debug"));

//[download] 0.9% of 3.45MiB at 553.57KiB/s ETA 00:06
private readonly Regex _extractDownloadProgress = new Regex(@"\[download\][\s]*([0-9\.]+)%");
//[download] 0.9% of 3.45MiB at 553.57KiB/s ETA 00:06
private readonly Regex _extractDownloadProgress = new Regex(@"\[download\][\s]*([0-9\.]+)%");
private int _progress;
private readonly int _totalSongs;
private readonly CancellationTokenSource _cts;


public Downloader(ICollection<PlaylistItem> playlist)
{
_playlist = playlist;
Expand Down Expand Up @@ -91,12 +94,9 @@ private void DownloadPlaylistItem(PlaylistItem item, ParallelOptions po)
StartInfo =
{
FileName = "youtube-dl.exe",
Arguments =
string.Format(
" --ffmpeg-location ./ffmpeg --extract-audio --audio-format mp3 -o \"{2}/{0}.%(ext)s\" {1}",
item.FileName, youtubeLink.Url, SettingsWindow.SONGS_FOLDER),
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = string.Format(" --ffmpeg-location ./ffmpeg --extract-audio --audio-format mp3 -o \"{2}\\{0}.%(ext)s\" {1}", item.FileName, youtubeLink.Url, SettingsWindow.SONGS_FOLDER),
CreateNoWindow = !_isDebugMode,
WindowStyle = _isDebugMode ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
UseShellExecute = false
}
Expand Down Expand Up @@ -142,10 +142,11 @@ private void DownloadPlaylistItem(PlaylistItem item, ParallelOptions po)

private static string MakeValidFileName(string name)
{
string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
string invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars);

return Regex.Replace(name, invalidRegStr, "_");
}
return Regex.Replace(
name,
"[\\W-]+", /*Matches any nonword character. Equivalent to '[^A-Za-z0-9_]'*/
"-",
RegexOptions.IgnoreCase);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
Expand Down
4 changes: 3 additions & 1 deletion PlaylistDownloader/PlaylistDownloader/SettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows.Input;
using PlaylistDownloader.Annotations;
using System;
using System.Configuration;

namespace PlaylistDownloader
{
Expand All @@ -33,6 +34,7 @@ public partial class SettingsWindow : INotifyPropertyChanged
private bool _isQueryValid;
private const string INSTRUCTIONS = "Enter songs (one per line)";
public static readonly string SONGS_FOLDER = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), "PlaylistDownloader");
private readonly bool _isDebugMode = bool.Parse(ConfigurationManager.AppSettings.Get("debug"));

public SettingsWindow()
{
Expand All @@ -53,7 +55,7 @@ public SettingsWindow()
FileName = "youtube-dl.exe",
Arguments = " -U",
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
WindowStyle = _isDebugMode ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
UseShellExecute = false
}
Expand Down
6 changes: 3 additions & 3 deletions PlaylistDownloader/PlaylistDownloader/YoutubeSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace PlaylistDownloader
{
public static class YoutubeSearcher
{
const string URL = "http://www.youtube.com/results?search_query={0}&page={1}";
private const string URL = "http://www.youtube.com/results?search_query={0}&page={1}";

public static IEnumerable<YoutubeLink> GetYoutubeLinks(string query, int numberOfResults = 1)
{
List<YoutubeLink> links = new List<YoutubeLink>();
int page = 1;
while (page < 20 && links.Count() < numberOfResults)
while (page < 20 && links.Count < numberOfResults)
{
string requestUrl = string.Format(URL, HttpUtility.UrlEncode(query)?.Replace("%20", "+"), page);
HtmlDocument doc = new HtmlDocument();
Expand All @@ -28,7 +28,7 @@ public static IEnumerable<YoutubeLink> GetYoutubeLinks(string query, int numberO
page++;
}
return links;
//TODO 040 make sure program correctly stops if page is not existent => message to user
// TODO 040 make sure program correctly stops if page is not existent => message to user
}

private static string GetWebPageCode(string url)
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 32cc4e7

Please sign in to comment.