Skip to content

Commit

Permalink
Speed-up menu parsing by ignoring lines starting with a previous or f…
Browse files Browse the repository at this point in the history
…uture date
  • Loading branch information
Lukas Wolfsteiner committed Aug 15, 2020
1 parent 7896397 commit d4de440
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Helper/DateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace WhatTheFuckShouldLukasHaveForLunch.Helper
{
public class DateHelper
{
private const string Format = "dd.MM.yyyy";
public const string Format = "dd.MM.yyyy";

public static int TodaysDayOfYear = DateTime.Today.DayOfYear;

Expand Down
98 changes: 66 additions & 32 deletions Models/MealModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,51 +56,85 @@ private void SplitRaw()
// Split raw string into an array of lines
var lines = _rawFoods.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

// For each line in the raw string
var firstLine = true;
foreach (var line in lines)
try
{
// Skip CSV header line
if (firstLine)
{
firstLine = false;
continue;
}
// Check lines against current date string
var nowDate = DateTime.Now.ToString(DateHelper.Format);

// Parse CSV line
var foodValues = line.Split(";", StringSplitOptions.RemoveEmptyEntries);

var foodName = foodValues[3];
var foodDate = foodValues[0];

// Both name and date have to be not empty or null
if (string.IsNullOrEmpty(foodName) && string.IsNullOrEmpty(foodDate))
// For each line in the raw string
var firstLine = true;
foreach (var line in lines)
{
throw new MensaClosedException("yea");
// Skip CSV header line
if (firstLine)
{
firstLine = false;
continue;
}

// Stop processing the line if it doesn't start with the current date string
if (!line.StartsWith(nowDate, System.StringComparison.CurrentCultureIgnoreCase))
{
Console.WriteLine($"Line doesn't start with our date: ignoring. nowDate={nowDate} line={line}");
continue;
}

// Parse CSV line
var foodValues = line.Split(";", StringSplitOptions.RemoveEmptyEntries);

var foodName = foodValues[3];
var foodDate = foodValues[0];

// Both name and date have to be not empty or null
if (string.IsNullOrEmpty(foodName) && string.IsNullOrEmpty(foodDate))
{
throw new MensaClosedException("Invalid line (NullOrEmpty foodName & NullOrEmpty foodDate");
}


// If date of current lines matches today, add to list
var parsedFoodDate = DateHelper.ParseDate(foodDate);
if (DateHelper.IsSameDay(DateTime.Today, parsedFoodDate))
{
_foods.Add(new FoodModel(foodName, parsedFoodDate));
}
}
}
catch (FormatException)
{
throw new MensaClosedException("Can't format the current moment into a short date string!");
}

Console.WriteLine($"Size of foodsArray={_foods.Count} after splitting up the lines");

// If date of current lines matches today, add to list
var parsedFoodDate = DateHelper.ParseDate(foodDate);
if (DateHelper.IsSameDay(DateTime.Today, parsedFoodDate))
{
_foods.Add(new FoodModel(foodName, parsedFoodDate));
}
if (_foods.Count == 0)
{
throw new MensaClosedException($"After parsing {lines.Length} menu lines, not even one entry matches the current date string!");
}

}

private string Endpoint => $"http://www.stwno.de/infomax/daten-extern/csv/UNI-R/{Weeknumber}.csv";

public async Task<FoodModel> GetRandomFoodAsync() {
await FetchRaw();
SplitRaw();
public async Task<FoodModel> GetRandomFoodAsync()
{
try
{
await FetchRaw();
SplitRaw();

if (!IsEmpty) return _foods[new Random().Next(0, _foods.Count)];
else return null;
}
if (_foods.Count > 0)
{
return _foods[new Random().Next(0, _foods.Count)];
}
}
catch (MensaClosedException e)
{
Console.WriteLine("Error: {0}", e);
throw;
}

public bool IsEmpty => _foods.Count == 0;
return null;
}

public override string ToString()
{
Expand Down

0 comments on commit d4de440

Please sign in to comment.