Skip to content

Commit

Permalink
A few fixes + Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitch528 committed Feb 21, 2016
1 parent 9090066 commit 7ac754d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 10 deletions.
10 changes: 7 additions & 3 deletions WebNovelConverter/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ namespace WebNovelConverter
{
public partial class MainForm : Form
{
private readonly WordPressSource _wordpress = new WordPressSource();

private readonly NovelSourceCollection _sources = new NovelSourceCollection();

public MainForm()
Expand All @@ -36,7 +34,8 @@ private void MainForm_Load(object sender, EventArgs e)
Version ver = Assembly.GetExecutingAssembly().GetName().Version;
this.Text += $" {ver.Major}.{ver.Minor}.{ver.Build}";

_sources.Add(new RoyalRoadL());
_sources.Add(new WordPressSource());
_sources.Add(new RoyalRoadLSource());
_sources.Add(new BakaTsukiSource());
_sources.Add(new WuxiaWorldSource());
_sources.Add(new NovelsNaoSource());
Expand Down Expand Up @@ -239,6 +238,11 @@ private async void retrieveBackgroundWorker_DoWork(object sender, DoWorkEventArg
modeSelectedText = modeSelectedTextBox.Text;
});

if (!(modeSelectedText.StartsWith("http://") || modeSelectedText.StartsWith("https://")))
modeSelectedText = "http://" + modeSelectedText;

Console.WriteLine(modeSelectedText);

WebNovelSource source = GetSource(modeSelectedText, type);
string coverUrl = await source.GetNovelCoverAsync(modeSelectedText);
coverUrl = coverUrl.StartsWith("//") ? coverUrl.Substring(2) : coverUrl;
Expand Down
11 changes: 8 additions & 3 deletions WebNovelConverter/Sources/NovelSourceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,19 @@ public bool Contains(WebNovelSource item)

public WebNovelSource Get(string sourceUrl)
{
Uri uri = new Uri(new UriBuilder(sourceUrl.Replace("www.", string.Empty)).Uri.GetLeftPart(UriPartial.Authority));
Uri uri;
if (!Uri.TryCreate(sourceUrl.Replace("www.", string.Empty), UriKind.Absolute, out uri))
return null;

return _sources.SingleOrDefault(p => new UriBuilder(p.BaseUrl.Replace("www.", string.Empty)).Uri == uri);
uri = new Uri(uri.GetLeftPart(UriPartial.Authority));

return _sources.Where(p => !string.IsNullOrEmpty(p.BaseUrl))
.SingleOrDefault(p => new UriBuilder(p.BaseUrl.Replace("www.", string.Empty)).Uri == uri);
}

public WebNovelSource GetByName(string name)
{
return _sources.SingleOrDefault(p => p.SourceName.Equals(name, StringComparison.OrdinalIgnoreCase));
return _sources.FirstOrDefault(p => p.SourceName.Equals(name, StringComparison.OrdinalIgnoreCase));
}

public void CopyTo(WebNovelSource[] array, int arrayIndex)
Expand Down
2 changes: 1 addition & 1 deletion WebNovelConverter/Sources/NovelsNaoSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class NovelsNaoSource : WordPressSource
"Table of Contents"
};

public NovelsNaoSource()
public NovelsNaoSource() : base("NovelsNao")
{
PostClasses.AddRange(_postClasses);
PageClasses.AddRange(_pageClasses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

namespace WebNovelConverter.Sources
{
public class RoyalRoadL : WebNovelSource
public class RoyalRoadLSource : WebNovelSource
{
public override string BaseUrl => "http://royalroadl.com/";

public RoyalRoadL() : base("RoyalRoadL")
public RoyalRoadLSource() : base("RoyalRoadL")
{
}

Expand Down
4 changes: 4 additions & 0 deletions WebNovelConverter/Sources/WordPressSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public WordPressSource() : base("WordPress")
{
}

public WordPressSource(string type) : base(type)
{
}

public override async Task<IEnumerable<ChapterLink>> GetChapterLinksAsync(string baseUrl, CancellationToken token = default(CancellationToken))
{
string baseContent = await GetWebPageAsync(baseUrl, token);
Expand Down
4 changes: 4 additions & 0 deletions WebNovelConverter/Sources/WuxiaWorldSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class WuxiaWorldSource : WordPressSource
"Previous Chapter"
};

public WuxiaWorldSource() : base("WuxiaWorld")
{
}

protected override void RemoveBloat(IElement element)
{
base.RemoveBloat(element);
Expand Down
2 changes: 1 addition & 1 deletion WebNovelConverter/WebNovelConverter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<Compile Include="Sources\Models\ChapterRetrievalOptions.cs" />
<Compile Include="Sources\NovelsNaoSource.cs" />
<Compile Include="Sources\NovelSourceCollection.cs" />
<Compile Include="Sources\RoyalRoadL.cs" />
<Compile Include="Sources\RoyalRoadLSource.cs" />
<Compile Include="Sources\WebNovelSource.cs" />
<Compile Include="Sources\WordPressSource.cs" />
<Compile Include="Sources\WuxiaWorldSource.cs" />
Expand Down

0 comments on commit 7ac754d

Please sign in to comment.