PIE
PIE
CC#
Created by PIE on 6/28/2024 in #help
✅ Strange parsing error with docker instance
So I have a program that scrapes prices and parses them into longs with these lines of code
HttpResponseMessage page = await client.GetAsync({the link});
HttpResponseMessage page = await client.GetAsync({the link});
long itemCost = long.Parse(itemCostElement.TextContent, NumberStyles.AllowThousands | NumberStyles.AllowCurrencySymbol);
long itemCost = long.Parse(itemCostElement.TextContent, NumberStyles.AllowThousands | NumberStyles.AllowCurrencySymbol);
however I got this error
The input string '$79,156' was not in a correct format.
The input string '$79,156' was not in a correct format.
Is this because of unicode differences in linux that doesn't work well with docker images made in windows? The above code seems to work fine on my windows machine.
10 replies
CC#
Created by PIE on 6/21/2024 in #help
Optimization problem for matching two lists of objects with same property
So I currently am running a database of ef core that needs to update userdata based on api calls and am faced with a speed problem So I have two lists: 1. List<Trader> (tracked by ef core) 2. List<User> that both have a property with the same value (trader id/user id) They both have a property "online" and I am attempting to set the list of traders "online" property to the same of the user The two lists are of random order but they have the same size and unique ids currently I am doing something like this (trader changes are auto tracked even in foreach loop):
foreach (Trader trader in traders)
{
User user = users
.First(user => user.UserId == trader.TraderId);
trader.Online = user.Online;
}
foreach (Trader trader in traders)
{
User user = users
.First(user => user.UserId == trader.TraderId);
trader.Online = user.Online;
}
would there be a linq or faster way to do the above?
10 replies
CC#
Created by PIE on 6/9/2024 in #help
✅ List<KeyValuePair<Trader, long>> requires primary key to be defined
This is my main model i'm trying to pass
public class Item
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public List<KeyValuePair<Trader, long>> TraderPrices { get; set; }
}
public class Item
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public List<KeyValuePair<Trader, long>> TraderPrices { get; set; }
}
with Trader as
[Keyless]
public class Trader
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsTeTrader { get; set; }
public bool IsAwTrader { get; set; }
}
[Keyless]
public class Trader
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsTeTrader { get; set; }
public bool IsAwTrader { get; set; }
}
with a basic database
class ItemDb : DbContext
{
public ItemDb(DbContextOptions<ItemDb> options)
: base(options) { }

public DbSet<Item> Items => Set<Item>();
}
class ItemDb : DbContext
{
public ItemDb(DbContextOptions<ItemDb> options)
: base(options) { }

public DbSet<Item> Items => Set<Item>();
}
for some reason, passing a Item class through this this method
db.Items.Add(itemInput);
await db.SaveChangesAsync();
return Results.Created($"/items/{itemInput.Id}", itemInput);
db.Items.Add(itemInput);
await db.SaveChangesAsync();
return Results.Created($"/items/{itemInput.Id}", itemInput);
throws the error in the title is there a way to set List<KeyValuePair<Trader, long>> to be keyless?
15 replies
CC#
Created by PIE on 5/30/2024 in #help
Can't figure out how to apply attributes to values in json property dictionary (System.Text.Json)
This is my model that I'm working with
//TODO convert to DateTime
[JsonPropertyName("peace")]
[JsonConverter(typeof(UnixDateTimeConverter))]
public Dictionary<int, DateTime> Peace { get; set; }
//TODO convert to DateTime
[JsonPropertyName("peace")]
[JsonConverter(typeof(UnixDateTimeConverter))]
public Dictionary<int, DateTime> Peace { get; set; }
The converter I'm using can convert integers to date time like so:
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.UnixEpoch.AddMilliseconds(reader.GetInt64());
}
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.UnixEpoch.AddMilliseconds(reader.GetInt64());
}
is there a way to change the converter to support dictionarys/lists? or apply the attribute to all values in a dictionarys/list?
1 replies
CC#
Created by PIE on 4/1/2024 in #help
Attempting to get value from different JSON files with variable key
Hello o/ I am attempting to deserilize and obtain the value from a variety of JSON files that contain similar values, but different keys with System.Text.Json. This was the current classes that I used in order to extract the values for one JSON file:
public class Info
{
[JsonPropertyName("user")]
public User User { get; }
[JsonPropertyName("galleryCount")]
public int GalleryCount { get; }
}

public class User
{
[JsonPropertyName("name")]
public string Name { get; }
[JsonPropertyName("account")]
public int Account { get; }
}
public class Info
{
[JsonPropertyName("user")]
public User User { get; }
[JsonPropertyName("galleryCount")]
public int GalleryCount { get; }
}

public class User
{
[JsonPropertyName("name")]
public string Name { get; }
[JsonPropertyName("account")]
public int Account { get; }
}
I know it's not possible to directly set attributes with a variable, but is there another way of dynamically searching for a value besides creating a class for every case? Thanks!
8 replies
CC#
Created by PIE on 11/23/2023 in #help
Attempting to get the file name from a REST api download link
I have a method that that recive a zip file from a REST api download link. However, the method that I use only manages to get the id of the file that I originally sent out. This was my method to extract the filename from the api:
public static string GetFilenameFromUrl(String url)
{
Uri uri = new Uri(url);
return System.IO.Path.GetFileName(uri.AbsolutePath);
}
public static string GetFilenameFromUrl(String url)
{
Uri uri = new Uri(url);
return System.IO.Path.GetFileName(uri.AbsolutePath);
}
and my method to download the zipped file form the api
public void DownloadBeatmapById(int id, string path)
{
string fileName = GetFilenameFromUrl($"https://api.chimu.moe/v1/download/{id}");

Task<Stream> stream = s_httpClient.GetStreamAsync($"https://api.chimu.moe/v1/download/{id}");

Console.WriteLine(path);
Console.WriteLine(fileName);
//.osz is another version of .zip
FileStream fileStream = new FileStream(string.Concat(path + fileName + ".osz"), FileMode.OpenOrCreate);

stream.Result.CopyTo(fileStream);
}
public void DownloadBeatmapById(int id, string path)
{
string fileName = GetFilenameFromUrl($"https://api.chimu.moe/v1/download/{id}");

Task<Stream> stream = s_httpClient.GetStreamAsync($"https://api.chimu.moe/v1/download/{id}");

Console.WriteLine(path);
Console.WriteLine(fileName);
//.osz is another version of .zip
FileStream fileStream = new FileStream(string.Concat(path + fileName + ".osz"), FileMode.OpenOrCreate);

stream.Result.CopyTo(fileStream);
}
So for example if I called the method with GetFilenameFromUrl(1627149); it would return the id (1627149) instead if the default filename (ex. the filename received when putting the link https://api.chimu.moe/v1/download/1627149 in the browser). Is there an alternative way to get the zipped file similar to a browser?
3 replies