Refactor task.

This commit is contained in:
2019-11-02 21:39:19 +00:00
parent 49823cc89d
commit 595f2bd6fd

View File

@@ -41,7 +41,7 @@ using Microsoft.EntityFrameworkCore;
namespace DiscImageChef.Server.Task
{
class Program
internal class Program
{
public static void Main(string[] args)
{
@@ -49,7 +49,7 @@ namespace DiscImageChef.Server.Task
start = DateTime.UtcNow;
System.Console.WriteLine("{0}: Connecting to database...", DateTime.UtcNow);
DicServerContext ctx = new DicServerContext();
var ctx = new DicServerContext();
end = DateTime.UtcNow;
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
@@ -66,16 +66,16 @@ namespace DiscImageChef.Server.Task
System.Console.WriteLine("{0}: Retrieving USB IDs from Linux USB...", DateTime.UtcNow);
start = DateTime.UtcNow;
client = new WebClient();
StringReader sr = new StringReader(client.DownloadString("http://www.linux-usb.org/usb.ids"));
var sr = new StringReader(client.DownloadString("http://www.linux-usb.org/usb.ids"));
end = DateTime.UtcNow;
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
UsbVendor vendor = null;
int newVendors = 0;
int newProducts = 0;
int modifiedVendors = 0;
int modifiedProducts = 0;
int counter = 0;
var newVendors = 0;
var newProducts = 0;
var modifiedVendors = 0;
var modifiedProducts = 0;
var counter = 0;
start = DateTime.UtcNow;
System.Console.WriteLine("{0}: Adding and updating database entries...", DateTime.UtcNow);
@@ -83,7 +83,7 @@ namespace DiscImageChef.Server.Task
{
if (counter == 1000)
{
DateTime start2 = DateTime.UtcNow;
var start2 = DateTime.UtcNow;
System.Console.WriteLine("{0}: Saving changes", start2);
ctx.SaveChanges();
end = DateTime.UtcNow;
@@ -91,7 +91,7 @@ namespace DiscImageChef.Server.Task
counter = 0;
}
string line = sr.ReadLine();
var line = sr.ReadLine();
if (line is null) break;
@@ -101,14 +101,20 @@ namespace DiscImageChef.Server.Task
string name;
if (line[0] == '\t')
{
try { number = Convert.ToUInt16(line.Substring(1, 4), 16); }
catch(FormatException) { continue; }
try
{
number = Convert.ToUInt16(line.Substring(1, 4), 16);
}
catch (FormatException)
{
continue;
}
if (number == 0) continue;
name = line.Substring(7);
UsbProduct product =
var product =
ctx.UsbProducts.FirstOrDefault(p => p.ProductId == number && p.Vendor != null &&
p.Vendor.VendorId == vendor.VendorId);
@@ -125,7 +131,8 @@ namespace DiscImageChef.Server.Task
else if (name != product.Product)
{
System.Console
.WriteLine("{0}: Will modify product with ID {1:X4} and vendor {2} ({3:X4}) from \"{4}\" to \"{5}\"",
.WriteLine(
"{0}: Will modify product with ID {1:X4} and vendor {2} ({3:X4}) from \"{4}\" to \"{5}\"",
DateTime.UtcNow, product.ProductId, product.Vendor?.Vendor ?? "null",
product.Vendor?.VendorId ?? 0,
product.Product, name);
@@ -138,8 +145,14 @@ namespace DiscImageChef.Server.Task
continue;
}
try { number = Convert.ToUInt16(line.Substring(0, 4), 16); }
catch(FormatException) { continue; }
try
{
number = Convert.ToUInt16(line.Substring(0, 4), 16);
}
catch (FormatException)
{
continue;
}
if (number == 0) continue;
@@ -151,7 +164,8 @@ namespace DiscImageChef.Server.Task
{
vendor = new UsbVendor(number, name);
ctx.UsbVendors.Add(vendor);
System.Console.WriteLine("{0}: Will add vendor {1} with ID {2:X4}", DateTime.UtcNow, vendor.Vendor,
System.Console.WriteLine("{0}: Will add vendor {1} with ID {2:X4}", DateTime.UtcNow,
vendor.Vendor,
vendor.VendorId);
newVendors++;
counter++;
@@ -165,8 +179,7 @@ namespace DiscImageChef.Server.Task
modifiedVendors++;
counter++;
}
}
while(true);
} while (true);
end = DateTime.UtcNow;
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
@@ -194,7 +207,7 @@ namespace DiscImageChef.Server.Task
System.Console.WriteLine("{0}: Looking up a product", DateTime.UtcNow);
start = DateTime.UtcNow;
UsbProduct prd =
var prd =
ctx.UsbProducts.FirstOrDefault(p => p.ProductId == 0x0001 && p.Vendor.VendorId == 0x8086);
if (prd is null) System.Console.WriteLine("{0}: Error, could not find product.", DateTime.UtcNow);
else System.Console.WriteLine("{0}: Found {1}.", DateTime.UtcNow, prd.Product);
@@ -211,7 +224,7 @@ namespace DiscImageChef.Server.Task
System.Console.WriteLine("{0}: Fixing all devices without modification time...", DateTime.UtcNow);
start = DateTime.UtcNow;
foreach(Device device in ctx.Devices.Where(d => d.ModifiedWhen == null))
foreach (var device in ctx.Devices.Where(d => d.ModifiedWhen == null))
device.ModifiedWhen = device.AddedWhen;
end = DateTime.UtcNow;
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
@@ -224,11 +237,12 @@ namespace DiscImageChef.Server.Task
try
{
System.Console.WriteLine("{0}: Retrieving CompactDisc read offsets from AccurateRip...", DateTime.UtcNow);
System.Console.WriteLine("{0}: Retrieving CompactDisc read offsets from AccurateRip...",
DateTime.UtcNow);
start = DateTime.UtcNow;
client = new WebClient();
string html = client.DownloadString("http://www.accuraterip.com/driveoffsets.htm");
var html = client.DownloadString("http://www.accuraterip.com/driveoffsets.htm");
end = DateTime.UtcNow;
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
@@ -236,31 +250,32 @@ namespace DiscImageChef.Server.Task
html = "<html><body><table><tr>" +
html.Substring(html.IndexOf("<td bgcolor=\"#000000\">", StringComparison.Ordinal));
HtmlDocument doc = new HtmlDocument();
var doc = new HtmlDocument();
doc.LoadHtml(html);
HtmlNode firstTable = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/table[1]");
var firstTable = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/table[1]");
bool firstRow = true;
var firstRow = true;
int addedOffsets = 0;
int modifiedOffsets = 0;
var addedOffsets = 0;
var modifiedOffsets = 0;
System.Console.WriteLine("{0}: Processing offsets...", DateTime.UtcNow);
start = DateTime.UtcNow;
foreach(HtmlNode row in firstTable.Descendants("tr"))
foreach (var row in firstTable.Descendants("tr"))
{
HtmlNode[] columns = row.Descendants("td").ToArray();
var columns = row.Descendants("td").ToArray();
if (columns.Length != 4)
{
System.Console.WriteLine("{0}: Row does not have correct number of columns...", DateTime.UtcNow);
System.Console.WriteLine("{0}: Row does not have correct number of columns...",
DateTime.UtcNow);
continue;
}
string column0 = columns[0].InnerText;
string column1 = columns[1].InnerText;
string column2 = columns[2].InnerText;
string column3 = columns[3].InnerText;
var column0 = columns[0].InnerText;
var column1 = columns[1].InnerText;
var column2 = columns[2].InnerText;
var column3 = columns[3].InnerText;
if (firstRow)
{
@@ -306,7 +321,7 @@ namespace DiscImageChef.Server.Task
}
else
{
int cutOffset = column0.IndexOf(" - ", StringComparison.Ordinal);
var cutOffset = column0.IndexOf(" - ", StringComparison.Ordinal);
if (cutOffset == -1)
{
@@ -333,7 +348,7 @@ namespace DiscImageChef.Server.Task
break;
}
CompactDiscOffset cdOffset =
var cdOffset =
ctx.CdOffsets.FirstOrDefault(o => o.Manufacturer == manufacturer && o.Model == model);
if (column1.ToLowerInvariant() == "[purged]")
@@ -342,14 +357,14 @@ namespace DiscImageChef.Server.Task
continue;
}
if(!short.TryParse(column1, out short offset)) continue;
if(!int.TryParse(column2, out int submissions)) continue;
if (!short.TryParse(column1, out var offset)) continue;
if (!int.TryParse(column2, out var submissions)) continue;
if (column3[column3.Length - 1] != '%') continue;
column3 = column3.Substring(0, column3.Length - 1);
if(!float.TryParse(column3, out float percentage)) continue;
if (!float.TryParse(column3, out var percentage)) continue;
percentage /= 100;
@@ -393,7 +408,7 @@ namespace DiscImageChef.Server.Task
cdOffset.Submissions != submissions) modifiedOffsets++;
}
foreach(Device device in ctx
foreach (var device in ctx
.Devices
.Where(d => d.Manufacturer == null && d.Model != null &&
d.Model.Trim() == model)