2020-09-01 10:41:29 +00:00
using System ;
using System.Globalization ;
2020-09-03 16:09:19 +00:00
using System.Text.RegularExpressions ;
using System.Collections.Generic ;
using System.Text.Json ;
2020-09-01 10:41:29 +00:00
namespace Buypeeb {
class Listing {
2020-09-03 16:09:19 +00:00
public string url {
get {
return $"https://page.auctions.yahoo.co.jp/jp/auction/{this.id}" ;
}
}
2020-09-04 07:23:32 +00:00
public string buyee_url {
get {
return $"https://buyee.jp/item/yahoo/auction/{this.id}" ;
}
}
2020-09-02 04:12:56 +00:00
public string id { get ; set ; }
public string name { get ; set ; }
2020-09-03 06:02:56 +00:00
public int price = 0 ;
2020-09-01 10:41:29 +00:00
public int win_price ;
public string original_name ;
2020-09-02 04:12:56 +00:00
public bool favourite { get ; set ; } = false ;
2020-09-01 10:41:29 +00:00
// start_date, end_date
public int bids ;
public bool auto_extension ;
2020-09-03 05:23:23 +00:00
public bool ready ;
2020-09-01 10:41:29 +00:00
2020-09-03 16:09:19 +00:00
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 ) {
2020-09-01 10:41:29 +00:00
this . id = id ;
this . name = name ;
2020-09-03 05:23:23 +00:00
this . ready = false ;
2020-09-01 10:41:29 +00:00
}
2020-09-03 13:47:36 +00:00
public Listing ( ) {
2020-09-03 16:09:19 +00:00
// parameterless constructor for deserialisation
2020-09-03 13:47:36 +00:00
}
2020-09-03 16:09:19 +00:00
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 = = "あり" ) ;
}
2020-09-01 10:41:29 +00:00
}
2020-09-04 07:23:32 +00:00
public string PriceAUD ( bool win = false ) {
double aud = win ? this . win_price / 75.0 : this . price / 75.0 ;
2020-09-01 10:41:29 +00:00
return $"${aud:f2}" ;
}
2020-09-04 07:23:32 +00:00
public string PriceJPY ( bool win = false ) {
return win ? $"¥{this.win_price}" : $"¥{this.price}" ;
2020-09-01 10:41:29 +00:00
}
}
}