Skip to content

Commit 07420b6

Browse files
committed
Lots of refactoring.
1 parent 7c827a4 commit 07420b6

File tree

12 files changed

+805
-0
lines changed

12 files changed

+805
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using wikia.Api;
5+
using wikia.Models.Article.AlphabeticalList;
6+
using wikia.Models.Article.Simple;
7+
using ygo_scheduled_tasks.domain.ETL.ArticleList.Processor.Model;
8+
using ygo_scheduled_tasks.domain.ETL.Tips.Model;
9+
using ygo_scheduled_tasks.domain.Services;
10+
11+
namespace ygo_scheduled_tasks.domain.ETL.ArticleList.Processor.Item
12+
{
13+
public class CardTriviaItemProcessor : IArticleItemProcessor
14+
{
15+
private readonly IWikiArticle _wikiArticle;
16+
private readonly ICardService _cardService;
17+
private readonly ICardTriviaService _cardTriviaService;
18+
19+
public CardTriviaItemProcessor
20+
(
21+
IWikiArticle wikiArticle,
22+
ICardService cardService,
23+
ICardTriviaService cardTriviaService
24+
)
25+
{
26+
_wikiArticle = wikiArticle;
27+
_cardService = cardService;
28+
_cardTriviaService = cardTriviaService;
29+
}
30+
31+
public async Task<ArticleTaskResult> ProcessItem(UnexpandedArticle item)
32+
{
33+
var response = new ArticleTaskResult { Article = item };
34+
35+
var card = await _cardService.CardByName(item.Title);
36+
37+
if (card != null)
38+
{
39+
var triviaSections = new List<CardTriviaSection>();
40+
41+
var articleCardTrivia = await _wikiArticle.Simple(item.Id);
42+
43+
foreach (var cardTriviaSection in articleCardTrivia.Sections)
44+
{
45+
if(cardTriviaSection.Title.Equals("References", StringComparison.OrdinalIgnoreCase))
46+
continue;
47+
48+
var rulingSection = new CardTriviaSection
49+
{
50+
Name = cardTriviaSection.Title,
51+
Trivia = GetSectionContentList(cardTriviaSection)
52+
};
53+
54+
triviaSections.Add(rulingSection);
55+
}
56+
57+
await _cardTriviaService.Update(card.Id, triviaSections);
58+
}
59+
60+
return response;
61+
}
62+
63+
private List<string> GetSectionContentList(Section cardTipSection)
64+
{
65+
var content = new List<string>();
66+
67+
foreach (var c in cardTipSection.Content)
68+
{
69+
GetContentList(c.Elements, content);
70+
}
71+
72+
return content;
73+
}
74+
75+
public void GetContentList(IEnumerable<ListElement> elementList, List<string> contentlist)
76+
{
77+
if (elementList != null)
78+
{
79+
foreach (var e in elementList)
80+
{
81+
if (e != null)
82+
{
83+
if (!string.IsNullOrEmpty(e.Text))
84+
contentlist.Add(e.Text);
85+
86+
GetContentList(e.Elements, contentlist);
87+
}
88+
}
89+
}
90+
}
91+
92+
public bool Handles(string category)
93+
{
94+
return category == ArticleCategory.CardTrivia;
95+
}
96+
}
97+
}

src/Domain/ygo-scheduled-tasks.domain/ygo-scheduled-tasks.domain.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<Compile Include="Command\UpdateTipsCommand.cs" />
4242
<Compile Include="Command\UpdateTriviaCommand.cs" />
4343
<Compile Include="ETL\ArticleList\Processor\Item\ArchetypeItemProcessor.cs" />
44+
<Compile Include="ETL\ArticleList\Processor\Item\CardTriviaItemProcessor.cs" />
4445
<Compile Include="ETL\ArticleList\Processor\Item\CardsByArchetypeItemProcessor.cs" />
4546
<Compile Include="ETL\ArticleList\Processor\Item\BanlistItemProcessor.cs" />
4647
<Compile Include="ETL\ArticleList\Processor\Item\CardsByArchetypeSupportItemProcessor.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using MediatR;
2+
using Quartz;
3+
using ygo_scheduled_tasks.application.ScheduledTasks.CardTrivia;
4+
5+
namespace ygo_scheduled_tasks.trivia
6+
{
7+
public class CardTriviaJob : IJob
8+
{
9+
private readonly IMediator _mediator;
10+
11+
public CardTriviaJob(IMediator mediator)
12+
{
13+
_mediator = mediator;
14+
}
15+
16+
public async void Execute(IJobExecutionContext context)
17+
{
18+
const int pageSize = 500;
19+
const string category = "Card Trivia";
20+
21+
await _mediator.Send(new CardTriviaTask { Category = category, PageSize = pageSize });
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace ygo_scheduled_tasks.trivia
4+
{
5+
public class CardTriviaService
6+
{
7+
public void OnStart()
8+
{
9+
Console.WriteLine("On Start");
10+
}
11+
12+
public void OnStop()
13+
{
14+
Console.WriteLine("On Stop");
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using StructureMap;
2+
using StructureMap.Graph.Scanning;
3+
4+
namespace ygo_scheduled_tasks.trivia
5+
{
6+
public static class Ioc
7+
{
8+
public static Container Initialize()
9+
{
10+
11+
var container = new Container(cfg =>
12+
{
13+
cfg.Scan
14+
(
15+
scan =>
16+
{
17+
scan.AssembliesFromApplicationBaseDirectory();
18+
scan.WithDefaultConventions();
19+
scan.LookForRegistries();
20+
}
21+
);
22+
});
23+
24+
// Should throw an exception if any error occurs if loading dlls, which silently fail.
25+
TypeRepository.AssertNoTypeScanningFailures();
26+
27+
return container;
28+
}
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Configuration;
3+
using Quartz;
4+
using Topshelf;
5+
using Topshelf.Quartz.StructureMap;
6+
using Topshelf.StructureMap;
7+
8+
namespace ygo_scheduled_tasks.trivia
9+
{
10+
class Program
11+
{
12+
private static readonly string CronExpression = ConfigurationManager.AppSettings["CronExpression"]; // Every Day at 4:00am
13+
14+
static void Main(string[] args)
15+
{
16+
HostFactory.Run(x =>
17+
{
18+
var container = Ioc.Initialize();
19+
x.UseStructureMap(container);
20+
x.UseNLog();
21+
22+
x.Service<CardTriviaService>(s =>
23+
{
24+
s.ConstructUsingStructureMap();
25+
26+
s.BeforeStartingService(_ => Console.WriteLine("Before Start"));
27+
s.BeforeStoppingService(_ => Console.WriteLine("Before Stop"));
28+
29+
s.WhenStarted(service => service.OnStart());
30+
s.WhenStopped(service => service.OnStop());
31+
32+
s.UseQuartzStructureMap();
33+
34+
s.ScheduleQuartzJob(q =>
35+
q.WithJob(() =>
36+
JobBuilder.Create<CardTriviaJob>().Build())
37+
.AddTrigger(() => TriggerBuilder.Create()
38+
//.WithCronSchedule(CronExpression)
39+
.StartNow()
40+
.Build()));
41+
});
42+
43+
x.RunAsLocalSystem()
44+
.DependsOnEventLog()
45+
.StartAutomatically()
46+
.EnableServiceRecovery(rc => rc.RestartService(1));
47+
48+
var ygoCardTips = "YgoCardTrivia";
49+
50+
x.SetServiceName(ygoCardTips);
51+
x.SetDisplayName(ygoCardTips);
52+
x.SetDescription("Amalgamate card trivia data, for all Yugioh cards.");
53+
});
54+
}
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("ygo-scheduled-tasks.trivia")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ygo-scheduled-tasks.trivia")]
13+
[assembly: AssemblyCopyright("Copyright © 2018")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("ce494656-57a7-4b1a-a762-fdeee0cbc1a9")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<configSections>
4+
<section name="ygo-settings" type="ygo_scheduled_tasks.application.Config, ygo-scheduled-tasks.application" />
5+
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
6+
</configSections>
7+
<appSettings>
8+
<add key="CronExpression" value="0 58 23 ? * FRI *" />
9+
</appSettings>
10+
<startup>
11+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
12+
</startup>
13+
14+
<!-- Wiki configuration-->
15+
<ygo-settings WikiaDomainUrl="http://yugioh.wikia.com" ApiUrl="http://localhost:56531" OAuthEmail="[email protected]" OAuthPassword="CardInformation-Scheduled-Task-42" />
16+
17+
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
18+
<variable name="consolebrief" value="${level} | ${message}" />
19+
<targets>
20+
<target name="file" xsi:type="File" layout="${longdate} ${logger} ${message}" fileName="${basedir}/logs/${shortdate}/${level}.log" keepFileOpen="false" encoding="iso-8859-2" />
21+
<target name="Console" xsi:type="ColoredConsole" layout="${consolebrief}" />
22+
</targets>
23+
24+
<rules>
25+
<logger name="*" levels="Info, Warn, Error, Fatal" writeTo="file" />
26+
<logger name="*" levels="Trace, Debug, Info, Warn, Error, Fatal" writeTo="Console" />
27+
</rules>
28+
</nlog>
29+
30+
<runtime>
31+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
32+
<dependentAssembly>
33+
<assemblyIdentity name="Quartz" publicKeyToken="f6b8c98a402cc8a4" culture="neutral" />
34+
<bindingRedirect oldVersion="0.0.0.0-2.6.1.0" newVersion="2.6.1.0" />
35+
</dependentAssembly>
36+
<dependentAssembly>
37+
<assemblyIdentity name="Topshelf" publicKeyToken="b800c4cfcdeea87b" culture="neutral" />
38+
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
39+
</dependentAssembly>
40+
<dependentAssembly>
41+
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
42+
<bindingRedirect oldVersion="0.0.0.0-4.1.1.2" newVersion="4.1.1.2" />
43+
</dependentAssembly>
44+
<dependentAssembly>
45+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
46+
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
47+
</dependentAssembly>
48+
</assemblyBinding>
49+
</runtime>
50+
</configuration>

0 commit comments

Comments
 (0)