Skip to content

Commit eded931

Browse files
committed
Fix issues with pdf not being loaded due to cloudfare stopping requestmessages without useragent
1 parent 06502a4 commit eded931

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

Diff for: Arctic.Puzzlers.Parsers/CompetitionParsers/SpeedPuzzlingParser.cs

+24-15
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ public async Task Parse(string url)
5050
{
5151
continue;
5252
}
53-
var response = await m_httpClient.GetAsync(competitionUrl);
53+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, competitionUrl);
54+
httpRequestMessage.Headers.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue(@"ArcticPuzzler","1.0.0.0"));
55+
var response = await m_httpClient.SendAsync(httpRequestMessage);
56+
if (!response.IsSuccessStatusCode)
57+
{
58+
m_logger.LogInformation($"Could not get pdf for {competitionUrl} with response code {response.StatusCode}");
59+
}
5460
using (var stream = await response.Content.ReadAsStreamAsync())
5561
{
5662
var competition = new Competition();
@@ -69,7 +75,6 @@ public async Task Parse(string url)
6975
catch (Exception ex)
7076
{
7177
m_logger.LogInformation(ex, $"Error parsing {competitionUrl}");
72-
7378
}
7479
}
7580
}
@@ -89,10 +94,7 @@ public async Task<CompetitionGroup> ParsePdf(Stream stream)
8994

9095
var firstLine = textLines.First();
9196
competitionRound.RoundName = firstLine.Replace("Results", "").TrimEnd();
92-
string puzzleName = textLines.Last();
93-
string puzzleBrand = textLines[textLines.Length - 2];
94-
puzzleBrand = WashPuzzleBrandName(puzzleBrand);
95-
BrandName puzzleBrandEnum = puzzleBrand.GetEnumFromString<BrandName>();
97+
9698

9799
var topRow = rows.First().ToArray();
98100
var timeHeader = Array.FindIndex(topRow, t => t.GetText().ToLower().Contains("time"));
@@ -122,17 +124,24 @@ public async Task<CompetitionGroup> ParsePdf(Stream stream)
122124
await AddResultForIndividual(competitionRound, rows, timeHeader, nameHeader, countryHeader);
123125
break;
124126
}
125-
126-
competitionGroup.Rounds.ForEach(t =>
127-
{
128-
t.Participants.ForEach(k => k.Results.ForEach(m => { m.Puzzle.BrandName = puzzleBrandEnum; m.Puzzle.Name = puzzleName; }));
129-
t.Puzzles.Add(new Puzzle { BrandName = puzzleBrandEnum, Name = puzzleName });
130-
});
131-
132-
127+
try
128+
{
129+
string puzzleName = textLines.Last();
130+
string puzzleBrand = textLines[textLines.Length - 2];
131+
puzzleBrand = WashPuzzleBrandName(puzzleBrand);
132+
BrandName puzzleBrandEnum = puzzleBrand.GetEnumFromString<BrandName>();
133+
competitionGroup.Rounds.ForEach(t =>
134+
{
135+
t.Participants.ForEach(k => k.Results.ForEach(m => { m.Puzzle.BrandName = puzzleBrandEnum; m.Puzzle.Name = puzzleName; }));
136+
t.Puzzles.Add(new Puzzle { BrandName = puzzleBrandEnum, Name = puzzleName });
137+
});
138+
}
139+
catch (Exception ex)
140+
{
141+
m_logger.LogInformation(ex, $"Error parsing puzzle brandname/puzzle name ");
142+
}
133143
}
134144
}
135-
136145
competitionGroup.Rounds.Add(competitionRound);
137146

138147
return competitionGroup;

0 commit comments

Comments
 (0)