Skip to content

Commit

Permalink
Add initial support for lnmtl #17
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitch528 committed Jun 3, 2016
1 parent 5d66083 commit d1305ae
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 41 deletions.
10 changes: 3 additions & 7 deletions WebNovelConverter/App.config
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="WebNovelConverter.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
<section name="WebNovelConverter.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<appSettings>
<add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
<userSettings>
<WebNovelConverter.Properties.Settings>
Expand All @@ -20,10 +20,6 @@
</userSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="AngleSharp" publicKeyToken="e83494dcdc6d31ea" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-0.9.4.42449" newVersion="0.9.4.42449"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
47 changes: 40 additions & 7 deletions WebNovelConverter/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 20 additions & 6 deletions WebNovelConverter/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private void MainForm_Load(object sender, EventArgs e)
_sources.Add(new BakaTsukiSource());
_sources.Add(new BlogspotSource());
_sources.Add(new NovelsNaoSource());
_sources.Add(new LNMTLSource());

websiteTypeComboBox.SelectedIndex = 0;
modeComboBox.SelectedIndex = 0;
Expand Down Expand Up @@ -152,12 +153,14 @@ private void convertButton_Click(object sender, EventArgs e)

private async void convertBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
var items = new List<object>();
string type = string.Empty;
string mode = string.Empty;
Invoke((MethodInvoker)delegate
{
type = ((string)websiteTypeComboBox.SelectedItem).ToLower();
mode = ((string)modeComboBox.SelectedItem).ToLower();
items.AddRange(chaptersListBox.Items.Cast<object>());
});

EBook book = new EBook
Expand All @@ -166,12 +169,6 @@ private async void convertBackgroundWorker_DoWork(object sender, DoWorkEventArgs
CoverImage = coverTextBox.Text
};

var items = new List<object>();
Invoke((MethodInvoker)delegate
{
items.AddRange(chaptersListBox.Items.Cast<object>());
});

foreach (object obj in items)
{
if (obj is ChapterLink)
Expand Down Expand Up @@ -237,12 +234,14 @@ private async void retrieveBackgroundWorker_DoWork(object sender, DoWorkEventArg
string type = string.Empty;
string mode = string.Empty;
string modeSelectedText = string.Empty;
int amount = 0;

Invoke((MethodInvoker)delegate
{
type = ((string)websiteTypeComboBox.SelectedItem).ToLower();
mode = ((string)modeComboBox.SelectedItem).ToLower();
modeSelectedText = modeSelectedTextBox.Text;
amount = (int)amountNumericUpDown.Value;
});

if (!(modeSelectedText.StartsWith("http://") || modeSelectedText.StartsWith("https://")))
Expand Down Expand Up @@ -296,6 +295,7 @@ private async void retrieveBackgroundWorker_DoWork(object sender, DoWorkEventArg
ChapterLink firstChapter = new ChapterLink { Url = modeSelectedText };
ChapterLink current = firstChapter;

int ctr = 1;
var chapters = new List<WebNovelChapter>();
while (true)
{
Expand Down Expand Up @@ -323,6 +323,11 @@ private async void retrieveBackgroundWorker_DoWork(object sender, DoWorkEventArg
break;

current = new ChapterLink { Url = chapter.NextChapterUrl };

if (ctr == amount)
break;

ctr++;
}

Invoke((MethodInvoker)delegate
Expand Down Expand Up @@ -422,6 +427,9 @@ private void websiteTypeComboBox_SelectedIndexChanged(object sender, EventArgs e
case "baka-tsuki":
modeComboBox.Items.Add("Table of Contents");
break;
case "lnmtl":
modeComboBox.Items.Add("Next Chapter Link");
break;
default:
modeComboBox.Items.Add("Table of Contents");
break;
Expand All @@ -438,9 +446,15 @@ private void modeComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
case "table of contents":
modeSelectedLabel.Text = "TOC URL";

amountLabel.Visible = false;
amountNumericUpDown.Visible = false;
break;
case "next chapter link":
modeSelectedLabel.Text = "Starting Chapter URL";

amountLabel.Visible = true;
amountNumericUpDown.Visible = true;
break;
}

Expand Down
3 changes: 3 additions & 0 deletions WebNovelConverter/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,7 @@
<metadata name="mainMenu1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>542, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>43</value>
</metadata>
</root>
6 changes: 4 additions & 2 deletions WebNovelConverter/Sources/BakaTsukiSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class BakaTsukiSource : WebNovelSource
"interlude"
};

private static readonly Regex WidthRegex = new Regex(@"width\=([0-9]+)", RegexOptions.Compiled);

public BakaTsukiSource() : base("BakaTsuki")
{
}
Expand Down Expand Up @@ -101,7 +103,7 @@ protected override IEnumerable<ChapterLink> CollectChapterLinks(string baseUrl,
if(coverUrl.Contains("width=") && coverUrlEl.HasAttribute("data-file-width"))
{
var width = Math.Min(int.Parse(coverUrlEl.Attributes["data-file-width"].Value)-1, 500);
coverUrl = Regex.Replace(coverUrl, @"width\=([0-9]+)", "width=" + width);
coverUrl = WidthRegex.Replace(coverUrl, "width=" + width);
}

// Make URL absolute
Expand All @@ -111,7 +113,7 @@ protected override IEnumerable<ChapterLink> CollectChapterLinks(string baseUrl,
}
}

return new WebNovelInfo()
return new WebNovelInfo
{
Title = title,
CoverUrl = coverUrl
Expand Down
61 changes: 61 additions & 0 deletions WebNovelConverter/Sources/LNMTLSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AngleSharp.Dom;
using AngleSharp.Dom.Html;
using WebNovelConverter.Sources.Models;
using AngleSharp.Extensions;
using WebNovelConverter.Extensions;

namespace WebNovelConverter.Sources
{
public class LNMTLSource : WebNovelSource
{
public override string BaseUrl => "http://lnmtl.com";

private static readonly List<string> ChapterTitleClasses = new List<string>
{
"chapter-title"
};

private static readonly List<string> ChapterClasses = new List<string>
{
"chapter-body"
};

private static readonly List<string> ChapterContentClasses = new List<string>
{
"translated"
};

public LNMTLSource() : base("LNMTL")
{
}

public override async Task<WebNovelChapter> GetChapterAsync(ChapterLink link, ChapterRetrievalOptions options = default(ChapterRetrievalOptions),
CancellationToken token = default(CancellationToken))
{
string content = await GetWebPageAsync(link.Url, token);

IHtmlDocument doc = await Parser.ParseAsync(content, token);

IElement titleElement = doc.DocumentElement.FirstWhereHasClass(ChapterTitleClasses);
IElement chapterElement = doc.DocumentElement.FirstWhereHasClass(ChapterClasses);

var chContentElements = chapterElement.WhereHasClass(ChapterContentClasses, element => element.LocalName == "sentence");

string contents = string.Join("<br/><br/>", chContentElements.Select(p => p.InnerHtml));
string nextChapter = doc.QuerySelector("ul.pager > li.next > a")?.GetAttribute("href");

return new WebNovelChapter
{
ChapterName = titleElement?.TextContent,
Content = contents,
NextChapterUrl = nextChapter
};
}
}
}
Loading

0 comments on commit d1305ae

Please sign in to comment.