Pārlūkot izejas kodu

initial progress

Karathan 6 gadus atpakaļ
vecāks
revīzija
ca7dd5660e

+ 20
- 0
AuctionTracker/AuctionTracker.csproj Parādīt failu

@@ -0,0 +1,20 @@
1
+<Project Sdk="Microsoft.NET.Sdk">
2
+
3
+  <PropertyGroup>
4
+    <OutputType>Exe</OutputType>
5
+    <TargetFramework>netcoreapp2.0</TargetFramework>
6
+    <LangVersion>7.1</LangVersion> 
7
+  </PropertyGroup>
8
+
9
+  <ItemGroup>
10
+    <ProjectReference Include="..\BlizzSharp\BlizzSharp.csproj" />
11
+  </ItemGroup>
12
+
13
+  <ItemGroup>
14
+    <PackageReference Include="log4net" Version="2.0.8" />
15
+    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
16
+    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
17
+    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
18
+  </ItemGroup>
19
+
20
+</Project>

+ 3
- 0
AuctionTracker/AuctionTracker.log Parādīt failu

@@ -0,0 +1,3 @@
1
+2018-08-21 11:53:50,014 [1] INFO AuctionTracker.Program - Started AuctionTracker, initializing configuration
2
+2018-08-21 11:53:50,084 [1] INFO AuctionTracker.Program - Creating Clients
3
+2018-08-21 11:53:50,095 [1] INFO AuctionTracker.Program - AuctionTracker started, entering loop.

+ 104
- 0
AuctionTracker/Program.cs Parādīt failu

@@ -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
+}

+ 34
- 0
AuctionTracker/Push/PushClient.cs Parādīt failu

@@ -0,0 +1,34 @@
1
+using System.Collections.Generic;
2
+using System.Net.Http;
3
+using System.Threading.Tasks;
4
+
5
+namespace AuctionTracker.Push
6
+{
7
+    public class PushClient 
8
+    {
9
+        private string _apiKey;
10
+        private string _userKey;
11
+
12
+        private HttpClient _client;
13
+        public PushClient(string apiKey, string userKey)
14
+        {
15
+            _apiKey = apiKey;
16
+            _userKey = userKey;
17
+            _client = new HttpClient();
18
+        }
19
+
20
+        public async Task<string> SendNotification(string message, string sound)
21
+        {
22
+            Dictionary<string,string> values = new Dictionary<string, string>
23
+            {
24
+                {"token", _apiKey},
25
+                {"user", _userKey},
26
+                {"sound", sound},
27
+                {"message", message},
28
+            };
29
+            FormUrlEncodedContent content = new FormUrlEncodedContent(values);
30
+            HttpResponseMessage response = await _client.PostAsync("https://api.pushover.net/1/messages.json", content);
31
+            return await response.Content.ReadAsStringAsync();
32
+        }
33
+    }
34
+}

+ 5
- 0
AuctionTracker/appsettings.json.example Parādīt failu

@@ -0,0 +1,5 @@
1
+{
2
+  "blizzardApiKey": "your_api_key",
3
+  "pushoverSecretKey": "your_secret_key",
4
+  "pushoverUserKey": "your_public_key"
5
+}

+ 68778
- 0
AuctionTracker/auctions.json
Failā izmaiņas netiks attēlotas, jo tās ir par lielu
Parādīt failu


+ 23
- 0
AuctionTracker/log4net.config Parādīt failu

@@ -0,0 +1,23 @@
1
+ <log4net>
2
+    <root>
3
+      <level value="ALL" />
4
+      <appender-ref ref="console" />
5
+      <appender-ref ref="file" />
6
+    </root>
7
+    <appender name="console" type="log4net.Appender.ConsoleAppender">
8
+      <layout type="log4net.Layout.PatternLayout">
9
+        <conversionPattern value="%date %level %logger - %message%newline" />
10
+      </layout>
11
+    </appender>
12
+    <appender name="file" type="log4net.Appender.RollingFileAppender">
13
+      <file value="AuctionTracker.log" />
14
+      <appendToFile value="true" />
15
+      <rollingStyle value="Size" />
16
+      <maxSizeRollBackups value="5" />
17
+      <maximumFileSize value="10MB" />
18
+      <staticLogFileName value="true" />
19
+      <layout type="log4net.Layout.PatternLayout">
20
+        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
21
+      </layout>
22
+    </appender>
23
+  </log4net>