|
@@ -0,0 +1,104 @@
|
|
1
|
+using System;
|
|
2
|
+using BlizzSharp;
|
|
3
|
+using AuctionTracker.Push;
|
|
4
|
+using BlizzSharp.Data;
|
|
5
|
+using System.Linq;
|
|
6
|
+using System.Collections.Generic;
|
|
7
|
+using System.Threading.Tasks;
|
|
8
|
+using System.Text;
|
|
9
|
+using System.Reflection;
|
|
10
|
+using log4net;
|
|
11
|
+using System.IO;
|
|
12
|
+using log4net.Config;
|
|
13
|
+using Microsoft.Extensions.Configuration;
|
|
14
|
+using Microsoft.Extensions.Configuration.FileExtensions;
|
|
15
|
+using Microsoft.Extensions.Configuration.Json;
|
|
16
|
+
|
|
17
|
+namespace AuctionTracker
|
|
18
|
+{
|
|
19
|
+ class Program
|
|
20
|
+ {
|
|
21
|
+ private static BlizzSharpClient _client;
|
|
22
|
+ private static PushClient _pushClient;
|
|
23
|
+ private static IEnumerable<Auction> lastAuctions;
|
|
24
|
+ private static IConfiguration _configuration;
|
|
25
|
+ private static ILog _logger;
|
|
26
|
+
|
|
27
|
+ private static string FormatCurrency(long full)
|
|
28
|
+ {
|
|
29
|
+ long gold = full / 10000;
|
|
30
|
+ long silver = (full - (gold*10000)) / 100;
|
|
31
|
+ long copper = (full - (gold*10000) - (silver * 100));
|
|
32
|
+ return $"{gold}G {silver}S {copper}C";
|
|
33
|
+ }
|
|
34
|
+ private static async Task RunUpdate()
|
|
35
|
+ {
|
|
36
|
+ AuctionResponse response = await _client.GetAuctionsAsync("Khaz'goroth", "en_GB");
|
|
37
|
+ IEnumerable<Auction> auctions = response.Auctions.Where(a => a.Owner == "Karathan");
|
|
38
|
+ if(lastAuctions != null)
|
|
39
|
+ {
|
|
40
|
+ HashSet<Auction> expiredAuctions = new HashSet<Auction>();
|
|
41
|
+ bool removedFound = false;
|
|
42
|
+ foreach(Auction a in lastAuctions)
|
|
43
|
+ {
|
|
44
|
+ if(!auctions.Any(b => b.Id == a.Id))
|
|
45
|
+ {
|
|
46
|
+ //the auction is now gone, but why?
|
|
47
|
+ Item item = await _client.GetItemInfoAsync(a.Item, "en_GB");
|
|
48
|
+ if(a.TimeLeft == "SHORT")
|
|
49
|
+ {
|
|
50
|
+ //the auction probably expired
|
|
51
|
+ expiredAuctions.Add(a);
|
|
52
|
+ }
|
|
53
|
+ else
|
|
54
|
+ {
|
|
55
|
+ //the auction was sold
|
|
56
|
+ await _pushClient.SendNotification($"Your Auction of {a.quantity}x {item.Name} was sold for {FormatCurrency(a.Buyout)}.", "cashregister");
|
|
57
|
+ }
|
|
58
|
+ removedFound = true;
|
|
59
|
+ }
|
|
60
|
+ }
|
|
61
|
+ if(!removedFound)
|
|
62
|
+ {
|
|
63
|
+ _logger.Info("No Expired/Sold Auctions found");
|
|
64
|
+ Console.WriteLine("No Expired/Sold Auctions found");
|
|
65
|
+ }
|
|
66
|
+ if(expiredAuctions.Any())
|
|
67
|
+ {
|
|
68
|
+ StringBuilder sb = new StringBuilder();
|
|
69
|
+ foreach(Auction a in expiredAuctions)
|
|
70
|
+ {
|
|
71
|
+ string name = (await _client.GetItemInfoAsync(a.Item, "en_GB")).Name;
|
|
72
|
+ sb.AppendLine($"{a.quantity}x {name}");
|
|
73
|
+ }
|
|
74
|
+ await _pushClient.SendNotification($"Your Auctions have expired:\n{sb.ToString()}", "none");
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+ lastAuctions = auctions;
|
|
78
|
+ }
|
|
79
|
+ static async Task Main(string[] args)
|
|
80
|
+ {
|
|
81
|
+ //configure logging
|
|
82
|
+ var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
|
|
83
|
+ XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
|
|
84
|
+ _logger = LogManager.GetLogger(typeof(Program));
|
|
85
|
+
|
|
86
|
+ _logger.Info("Started AuctionTracker, initializing configuration");
|
|
87
|
+ var builder = new ConfigurationBuilder()
|
|
88
|
+ .SetBasePath(Directory.GetCurrentDirectory())
|
|
89
|
+ .AddJsonFile("appsettings.json");
|
|
90
|
+ _configuration = builder.Build();
|
|
91
|
+ _logger.Info("Creating Clients");
|
|
92
|
+ _client = new BlizzSharpClient($"{_configuration["blizzardApiKey"]}");
|
|
93
|
+ _pushClient = new PushClient($"{_configuration["pushoverSecretKey"]}", $"{_configuration["pushoverUserKey"]}");
|
|
94
|
+ _logger.Info("AuctionTracker started, entering loop");
|
|
95
|
+
|
|
96
|
+ while(true)
|
|
97
|
+ {
|
|
98
|
+ await RunUpdate();
|
|
99
|
+ System.Threading.Thread.Sleep(TimeSpan.FromMinutes(15d));
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ }
|
|
103
|
+ }
|
|
104
|
+}
|