Skip to content

Commit

Permalink
Refactored SourcedTorrentCreator into extension methods for TorrentCr…
Browse files Browse the repository at this point in the history
…eator
  • Loading branch information
flare561 committed Feb 1, 2017
1 parent 413e1eb commit ff79edf
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 46 deletions.
4 changes: 3 additions & 1 deletion Rippy/AlbumData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ public string FLAC

private string FormatFolderName(string format)
{
return $"{Artist} - {Album} ({Year}) {(!string.IsNullOrWhiteSpace(Publisher) || !string.IsNullOrWhiteSpace(Number) ? $"[{Publisher} {Number}] " : "")}[{(!string.IsNullOrWhiteSpace(Medium) ? $"{Medium} " : "")}{format}]";
var pubNum = !string.IsNullOrWhiteSpace(Publisher) || !string.IsNullOrWhiteSpace(Number) ? $"[{Publisher} {Number}] " : "";
var medium = !string.IsNullOrWhiteSpace(Medium) ? $"{Medium} " : "";
return $"{Artist} - {Album} ({Year}) {pubNum}[{medium}{format}]";
}
}
}
56 changes: 54 additions & 2 deletions Rippy/HelperMethods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using MonoTorrent.BEncoding;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand All @@ -7,7 +8,7 @@

namespace Rippy
{
class HelperMethods
public static class HelperMethods
{
public static Task RunProcessAsync(string fileName, string arguments)
{
Expand All @@ -30,5 +31,56 @@ public static Task RunProcessAsync(string fileName, string arguments)

return tcs.Task;
}
/// <summary>
/// Get custom value from the info dict of the torrent
/// </summary>
/// <param name="tc">TorrentCreator the extension method is bound to</param>
/// <param name="key">The dictionary key to get</param>
/// <returns>The value of the given key</returns>
public static BEncodedValue GetCustomInfo(this MonoTorrent.Common.TorrentCreator tc, BEncodedString key)
{
var dict = (BEncodedDictionary)typeof(MonoTorrent.Common.TorrentCreator).GetField("info", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(tc);

BEncodedValue val = ((BEncodedDictionary)dict).Get(key);
return val;
}

/// <summary>
/// Set custom value in the info dict of the torrent
/// </summary>
/// <param name="tc">TorrentCreator the extension method is bound to</param>
/// <param name="key">The dictionary key to set</param>
/// <param name="value">The desired value of the key</param>
public static void AddCustomInfo(this MonoTorrent.Common.TorrentCreator tc, BEncodedString key, BEncodedValue value)
{
var dict = (BEncodedDictionary)typeof(MonoTorrent.Common.TorrentCreator).GetField("info", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(tc);

((BEncodedDictionary)dict).Set(key, value);
}

/// <summary>
/// Get key from BEncodedDictionary
/// </summary>
/// <param name="dictionary">BEncodedDictionary the extension method is bound to</param>
/// <param name="key">The dictionary key to get</param>
/// <returns>the value of the given key</returns>
public static BEncodedValue Get(this BEncodedDictionary dictionary, BEncodedString key)
{
return dictionary.ContainsKey(key) ? dictionary[key] : null;
}

/// <summary>
/// Set key in BEncodedDictionary
/// </summary>
/// <param name="dictionary">BEncodedDictionary the extension method is bound to</param>
/// <param name="key">The dictionary key to set</param>
/// <param name="value">The desired value of the key</param>
public static void Set(this BEncodedDictionary dictionary, BEncodedString key, BEncodedValue value)
{
if (dictionary.ContainsKey(key))
dictionary[key] = value;
else
dictionary.Add(key, value);
}
}
}
4 changes: 2 additions & 2 deletions Rippy/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ private bool TranscodeFiles(string directory, string dbargs, int totalFiles)

private void CreateTorrent(string directory, string name)
{
var tc = new SourcedTorrentCreator();
var tc = new TorrentCreator();
tc.Private = true;
tc.Announces.Add(new List<string>() { _albumData.Tracker });
tc.Source = "PTH";
tc.AddCustomInfo(new MonoTorrent.BEncoding.BEncodedString("source"), new MonoTorrent.BEncoding.BEncodedString("PTH"));
tc.Create(new TorrentFileSource(directory), Path.Combine(Rippy.Properties.Settings.Default.TorrentDirectory, $"{name}.torrent"));
}

Expand Down
1 change: 0 additions & 1 deletion Rippy/Rippy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="HelperMethods.cs" />
<Compile Include="SourcedTorrentCreator.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
40 changes: 0 additions & 40 deletions Rippy/SourcedTorrentCreator.cs

This file was deleted.

0 comments on commit ff79edf

Please sign in to comment.