Skip to content

Commit

Permalink
Merge pull request #13 from NguyenDanPhuong/dev
Browse files Browse the repository at this point in the history
Support for CBZ output
UI changes
Fix mangafox.me
  • Loading branch information
NguyenDanPhuong authored Dec 5, 2016
2 parents f21703a + b865c02 commit d08146a
Show file tree
Hide file tree
Showing 22 changed files with 400 additions and 131 deletions.
18 changes: 11 additions & 7 deletions MangaRipper.Core/Chapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MangaRipper.Core;
namespace MangaRipper.Core
{
[Serializable]
public class Chapter
{
public string Name { get; private set; }
public string Link { get; private set; }
public bool IsBusy { get; internal set; }
public Chapter(string name, string link)
/// <summary>
/// Chapter name which safe for using as folder name.
/// </summary>
public string NomalizeName => Name.RemoveFileNameInvalidChar();

public string Url { get; private set; }

public Chapter(string name, string url)
{
Name = name;
Link = link;
Url = url;
}

public void AddPrefix(int prefix)
{
Name = string.Format("[{0:000}] - {1}", prefix, Name);
Name = $"[{prefix:000}] - {Name}";
}
}
}
39 changes: 39 additions & 0 deletions MangaRipper.Core/DownloadChapterTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MangaRipper.Core
{
/// <summary>
/// When chapters added to download list. They are added as tasks.
/// A task include a chapter, and download information (percent downloaded, save to location, output format...)
/// </summary>
public class DownloadChapterTask
{
public string Name => Chapter.Name;
public string Url => Chapter.Url;
public Chapter Chapter { get; private set; }
public string SaveToFolder { get; private set; }
public IEnumerable<OutputFormat> Formats { get; private set; }

public string PropFormats
{
get
{
var s = Formats.Select(format => format.ToString()).ToList();
return string.Join(", ", s);
}
}

public bool IsBusy { get; set; }
public int Percent { get; set; }
public DownloadChapterTask(Chapter chapter, string saveToFolder, IEnumerable<OutputFormat> formats)
{
Chapter = chapter;
SaveToFolder = saveToFolder;
Formats = formats;
}
}
}
13 changes: 7 additions & 6 deletions MangaRipper.Core/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public class Downloader
private HttpWebRequest CreateRequest(string url)
{
var uri = new Uri(url);
HttpWebRequest request = WebRequest.CreateHttp(uri);
var request = WebRequest.CreateHttp(uri);
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.Credentials = CredentialCache.DefaultCredentials;
request.Referer = "mangafox.me";
return request;
}

Expand All @@ -36,9 +37,9 @@ public async Task<string> DownloadStringAsync(string url)
{
logger.Info("> DownloadStringAsync: {0}", url);
var request = CreateRequest(url);
using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
using (var response = (HttpWebResponse)await request.GetResponseAsync())
{
using (Stream responseStream = response.GetResponseStream())
using (var responseStream = response.GetResponseStream())
{
var streamReader = new StreamReader(responseStream, Encoding.UTF8);
return await streamReader.ReadToEndAsync();
Expand All @@ -57,7 +58,7 @@ internal async Task<string> DownloadStringAsync(IEnumerable<string> urls, IProgr
{
logger.Info("> DownloadStringAsync - Total: {0}", urls.Count());
var sb = new StringBuilder();
int count = 0;
var count = 0;
progress.Report(count);
foreach (var url in urls)
{
Expand All @@ -81,9 +82,9 @@ public async Task DownloadFileAsync(string url, string fileName, CancellationTok
{
logger.Info("> DownloadFileAsync: {0} - {1}", url, fileName);
var request = CreateRequest(url);
using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
using (var response = (HttpWebResponse)await request.GetResponseAsync())
{
using (Stream responseStream = response.GetResponseStream())
using (var responseStream = response.GetResponseStream())
using (var streamReader = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
await responseStream.CopyToAsync(streamReader, 81920, cancellationToken);
Expand Down
1 change: 1 addition & 0 deletions MangaRipper.Core/Framework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static void Init()
services.Add(new MangaHereService());
services.Add(new MangaReaderService());
services.Add(new MangaShareService());
//services.Add(new KissMangaService());
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions MangaRipper.Core/IMangaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IMangaService
/// <param name="progress"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<IList<Chapter>> FindChapters(string manga, IProgress<int> progress, CancellationToken cancellationToken);
Task<IEnumerable<Chapter>> FindChapters(string manga, IProgress<int> progress, CancellationToken cancellationToken);

/// <summary>
/// Find all images inside a chapter.
Expand All @@ -42,6 +42,6 @@ public interface IMangaService
/// <param name="progress"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<IList<string>> FindImanges(Chapter chapter, IProgress<int> progress, CancellationToken cancellationToken);
Task<IEnumerable<string>> FindImanges(Chapter chapter, IProgress<int> progress, CancellationToken cancellationToken);
}
}
68 changes: 68 additions & 0 deletions MangaRipper.Core/KissMangaService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using NLog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MangaRipper.Core
{
/// <summary>
/// Support find chapters, images from MangaFox
/// </summary>
class KissMangaService : IMangaService
{
private static Logger logger = LogManager.GetCurrentClassLogger();

public SiteInformation GetInformation()
{
return new SiteInformation("KissManga", "http://kissmanga.com/", "English");
}

public bool Of(string link)
{
var uri = new Uri(link);
return uri.Host.Equals("kissmanga.com");
}

public async Task<IEnumerable<Chapter>> FindChapters(string manga, IProgress<int> progress, CancellationToken cancellationToken)
{
progress.Report(0);
var downloader = new Downloader();
var parser = new Parser();

// find all chapters in a manga
string input = await downloader.DownloadStringAsync(manga);
var chaps = parser.ParseGroup("<a href=\"(?<Value>/Manga/[^\"]+)\" title=\"[^\"]+\">\r(?<Name>[^<]+)</a>", input, "Name", "Value");
chaps = chaps.Select(c =>
{
var uri = new Uri(new Uri("http://kissmanga.com"), c.Url);
return new Chapter(c.Name, uri.AbsoluteUri);
});
progress.Report(100);
return chaps;
}

public async Task<IEnumerable<string>> FindImanges(Chapter chapter, IProgress<int> progress, CancellationToken cancellationToken)
{
progress.Report(0);
var downloader = new Downloader();
var parser = new Parser();

// find all pages in a chapter
string input = await downloader.DownloadStringAsync(chapter.Url);
var images = parser.Parse(@"<option value=""(?<Value>[^""]+)"" (|selected=""selected"")>\d+</option>", input, "Value");
// transform pages link
images = images.Select(p =>
{
var value = new Uri(new Uri(chapter.Url), (p + ".html")).AbsoluteUri;
return value;
}).ToList();

progress.Report(100);
return images;
}
}
}
14 changes: 7 additions & 7 deletions MangaRipper.Core/MangaFoxService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace MangaRipper.Core
/// </summary>
class MangaFoxService : IMangaService
{
private static Logger logger = LogManager.GetCurrentClassLogger();
private static Logger _logger = LogManager.GetCurrentClassLogger();

public SiteInformation GetInformation()
{
Expand All @@ -27,7 +27,7 @@ public bool Of(string link)
return uri.Host.Equals("mangafox.me");
}

public async Task<IList<Chapter>> FindChapters(string manga, IProgress<int> progress, CancellationToken cancellationToken)
public async Task<IEnumerable<Chapter>> FindChapters(string manga, IProgress<int> progress, CancellationToken cancellationToken)
{
progress.Report(0);
var downloader = new Downloader();
Expand All @@ -40,19 +40,19 @@ public async Task<IList<Chapter>> FindChapters(string manga, IProgress<int> prog
return chaps;
}

public async Task<IList<string>> FindImanges(Chapter chapter, IProgress<int> progress, CancellationToken cancellationToken)
public async Task<IEnumerable<string>> FindImanges(Chapter chapter, IProgress<int> progress, CancellationToken cancellationToken)
{
progress.Report(0);
var downloader = new Downloader();
var parser = new Parser();

// find all pages in a chapter
string input = await downloader.DownloadStringAsync(chapter.Link);
var input = await downloader.DownloadStringAsync(chapter.Url);
var pages = parser.Parse(@"<option value=""(?<Value>[^""]+)"" (|selected=""selected"")>\d+</option>", input, "Value");
// transform pages link
pages = pages.Select(p =>
{
var value = new Uri(new Uri(chapter.Link), (p + ".html")).AbsoluteUri;
var value = new Uri(new Uri(chapter.Url), (p + ".html")).AbsoluteUri;
return value;
}).ToList();

Expand All @@ -61,8 +61,8 @@ public async Task<IList<string>> FindImanges(Chapter chapter, IProgress<int> pro
pages,
new Progress<int>((count) =>
{
var f = (float) count / pages.Count;
int i = Convert.ToInt32(f * 100);
var f = (float) count / pages.Count();
var i = Convert.ToInt32(f * 100);
progress.Report(i);
}),
cancellationToken);
Expand Down
10 changes: 5 additions & 5 deletions MangaRipper.Core/MangaHereService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MangaHereService : IMangaService
{
private static Logger logger = LogManager.GetCurrentClassLogger();

public async Task<IList<Chapter>> FindChapters(string manga, IProgress<int> progress, CancellationToken cancellationToken)
public async Task<IEnumerable<Chapter>> FindChapters(string manga, IProgress<int> progress, CancellationToken cancellationToken)
{
var downloader = new Downloader();
var parser = new Parser();
Expand All @@ -28,26 +28,26 @@ public async Task<IList<Chapter>> FindChapters(string manga, IProgress<int> prog
return chaps;
}

public async Task<IList<string>> FindImanges(Chapter chapter, IProgress<int> progress, CancellationToken cancellationToken)
public async Task<IEnumerable<string>> FindImanges(Chapter chapter, IProgress<int> progress, CancellationToken cancellationToken)
{
var downloader = new Downloader();
var parser = new Parser();

// find all pages in a chapter
string input = await downloader.DownloadStringAsync(chapter.Link);
string input = await downloader.DownloadStringAsync(chapter.Url);
var pages = parser.Parse(@"<option value=""(?<Value>[^""]+)"" (|selected=""selected"")>\d+</option>", input, "Value");

// transform pages link
pages = pages.Select(p =>
{
var value = new Uri(new Uri(chapter.Link), p).AbsoluteUri;
var value = new Uri(new Uri(chapter.Url), p).AbsoluteUri;
return value;
}).ToList();

// find all images in pages
var pageData = await downloader.DownloadStringAsync(pages, new Progress<int>((count) =>
{
var f = (float)count / pages.Count;
var f = (float)count / pages.Count();
int i = Convert.ToInt32(f * 100);
progress.Report(i);
}), cancellationToken);
Expand Down
14 changes: 7 additions & 7 deletions MangaRipper.Core/MangaReaderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MangaReaderService : IMangaService
{
private static Logger logger = LogManager.GetCurrentClassLogger();

public async Task<IList<Chapter>> FindChapters(string manga, IProgress<int> progress, CancellationToken cancellationToken)
public async Task<IEnumerable<Chapter>> FindChapters(string manga, IProgress<int> progress, CancellationToken cancellationToken)
{
var downloader = new Downloader();
var parser = new Parser();
Expand All @@ -24,36 +24,36 @@ public async Task<IList<Chapter>> FindChapters(string manga, IProgress<int> prog
string input = await downloader.DownloadStringAsync(manga);
var chaps = parser.ParseGroup("<a href=\"(?<Value>[^\"]+)\">(?<Name>[^<]+)</a> :", input, "Name", "Value");
// reverse chapters order and remove duplicated chapters in latest setion
chaps = chaps.Reverse().GroupBy(x => x.Link).Select(g => g.First()).ToList();
chaps = chaps.Reverse().GroupBy(x => x.Url).Select(g => g.First()).ToList();
// transform pages link
chaps = chaps.Select(c =>
{
return new Chapter(c.Name, new Uri(new Uri(manga), c.Link).AbsoluteUri);
return new Chapter(c.Name, new Uri(new Uri(manga), c.Url).AbsoluteUri);
}).ToList();
progress.Report(100);
return chaps;
}

public async Task<IList<string>> FindImanges(Chapter chapter, IProgress<int> progress, CancellationToken cancellationToken)
public async Task<IEnumerable<string>> FindImanges(Chapter chapter, IProgress<int> progress, CancellationToken cancellationToken)
{
var downloader = new Downloader();
var parser = new Parser();

// find all pages in a chapter
string input = await downloader.DownloadStringAsync(chapter.Link);
string input = await downloader.DownloadStringAsync(chapter.Url);
var pages = parser.Parse(@"<option value=""(?<Value>[^""]+)""(| selected=""selected"")>\d+</option>", input, "Value");

// transform pages link
pages = pages.Select(p =>
{
var value = new Uri(new Uri(chapter.Link), p).AbsoluteUri;
var value = new Uri(new Uri(chapter.Url), p).AbsoluteUri;
return value;
}).ToList();

// find all images in pages
var pageData = await downloader.DownloadStringAsync(pages, new Progress<int>((count) =>
{
var f = (float)count / pages.Count;
var f = (float)count / pages.Count();
int i = Convert.ToInt32(f * 100);
progress.Report(i);
}), cancellationToken);
Expand Down
5 changes: 5 additions & 0 deletions MangaRipper.Core/MangaRipper.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -52,14 +53,18 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Chapter.cs" />
<Compile Include="DownloadChapterTask.cs" />
<Compile Include="Framework.cs" />
<Compile Include="IMangaService.cs" />
<Compile Include="Downloader.cs" />
<Compile Include="Extension.cs" />
<Compile Include="KissMangaService.cs" />
<Compile Include="MangaFoxService.cs" />
<Compile Include="MangaHereService.cs" />
<Compile Include="MangaReaderService.cs" />
<Compile Include="MangaShareService.cs" />
<Compile Include="OutputFormat.cs" />
<Compile Include="PackageCbz.cs" />
<Compile Include="Parser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SiteInformation.cs" />
Expand Down
Loading

0 comments on commit d08146a

Please sign in to comment.