buypeeb-cs/Listing.cs

91 lines
2.8 KiB
C#

using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text.Json;
namespace Buypeeb {
class Listing {
public string url {
get {
return $"https://page.auctions.yahoo.co.jp/jp/auction/{this.id}";
}
}
public string buyee_url {
get {
return $"https://buyee.jp/item/yahoo/auction/{this.id}";
}
}
public string id { get; set; }
public string name { get; set; }
public int price = 0;
public int win_price;
public string original_name;
public bool favourite { get; set; } = false;
// start_date, end_date
public int bids;
public bool auto_extension;
public bool ready;
private bool success { get; set; } // TODO: custom setter that throws an exception if set to false or something idk
public Listing(string id, string name) {
this.id = id;
this.name = name;
this.ready = false;
}
public Listing() {
// parameterless constructor for deserialisation
}
public void Update(string html) {
var rx = new Regex(@"var pageData ?= ?(\{.+?\});", RegexOptions.Singleline); // TODO: maybe compile and match the regex in another thread
var m = rx.Match(html);
if (m == null) {
Console.WriteLine("no sir i don't like it");
return;
}
Dictionary<string, Dictionary<string, string>> j_full;
try {
// master forgive me, but i must go all out, just this once...
j_full = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(m.Groups[1].Value);
}
catch (Exception e) {
throw e;
}
var j = j_full["items"];
this.original_name = j["productName"];
this.success = int.TryParse(j["price"], out this.price);
this.success = int.TryParse(j["winPrice"], out this.win_price);
this.success = int.TryParse(j["bids"], out this.bids);
if (String.IsNullOrWhiteSpace(this.name)) {
this.name = this.original_name;
}
// as far as i can tell, neither the `pageData` nor the `conf` variables in the html seem to store whether or not the auction uses automatic extension
// the `conf` variable *does* store whether or not the auction has the "early end" feature enabled, in the key `earlyed`.
// unfortunately, it seems like the only way to get the auto extension info is to scrape the page for the info column that displays the auto ext status
// and check whether or not it's equal to "ari" (japanese for "yes").
var autoExtensionCheck = new Regex(@"自動延長.+\n.+>(.+)<");
m = rx.Match(html);
if (m.Groups[1].Value != null) {
this.auto_extension = (m.Groups[1].Value == "あり");
}
}
public string PriceAUD(bool win = false) {
double aud = win ? this.win_price / 75.0 : this.price / 75.0;
return $"${aud:f2}";
}
public string PriceJPY(bool win = false) {
return win ? $"¥{this.win_price}" : $"¥{this.price}";
}
}
}