Browse Source

Сделал функцию получения данных с ethermine #EBT-2 Состояние Проверена

Veloe 4 years ago
parent
commit
ad3bb99bdc

+ 1 - 0
EthermineBotTelegram/EthermineBotTelegram.csproj

@@ -51,6 +51,7 @@
     </Reference>
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="jsonCurrentStats.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

+ 47 - 2
EthermineBotTelegram/Program.cs

@@ -7,10 +7,12 @@ using Telegram.Bot;
 using Telegram.Bot.Args;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
+using Newtonsoft.Json;
+using System.Net;
 
 namespace EthermineBotTelegram
 {
-    class Program
+    class  Program
     {
         static ITelegramBotClient botClient;
         static void Main(string[] args)
@@ -53,7 +55,12 @@ namespace EthermineBotTelegram
 
                         // get actual data from ethermine
                         case "/actual":
-                            //await GetActualData();
+                            GetActualData(e);
+                            break;
+                        
+                        // send help
+                        case "/help":
+                            //await SendHelp();
                             break;
         
                         default:
@@ -66,5 +73,43 @@ namespace EthermineBotTelegram
 
             }
         }
+
+        static async void GetActualData(MessageEventArgs e)
+        {
+            try
+            {
+                var url = "https://api.ethermine.org/miner/" + e.Message.Text.Substring(8) + "/currentStats";
+                var currnentStats = _download_serialized_json_data<jsonCurrnentStats>(url);
+                await botClient.SendTextMessageAsync(
+                    chatId: e.Message.Chat,
+                    text: "Updated: " + DateTimeOffset.FromUnixTimeSeconds(currnentStats.data.time).LocalDateTime.ToString("f") + "\n"
+                          + "Reported Hashrate: " + Math.Round(currnentStats.data.reportedHashrate / 1000000D, 3) +
+                          " MH/s\n"
+                          + "Current Hashrate: " + Math.Round(currnentStats.data.currentHashrate / 1000000D, 3) +
+                          " MH/s\n"
+                          + "Valid Shares: " + currnentStats.data.validShares + "\n"
+                          + "Stale Shares: " + currnentStats.data.staleShares + "\n"
+                          + "Unpaid Balance: " + Math.Round(currnentStats.data.unpaid / Math.Pow(10, 18), 5) + " ETH\n");
+            }
+            catch (Exception)
+            {
+                await botClient.SendTextMessageAsync(
+                    chatId: e.Message.Chat,
+                    text:   "Something got wrong! Check entered wallet or try later.");
+            }
+        }
+        private static T _download_serialized_json_data<T>(string url) where T : new() {
+            using (var w = new WebClient()) {
+                var jsonData = string.Empty;
+                // attempt to download JSON data as a string
+                try
+                {
+                    jsonData = w.DownloadString(url);
+                }
+                catch (Exception) { }
+                // if string with JSON data is not empty, deserialize it to class and return its instance 
+                return !string.IsNullOrEmpty(jsonData) ? JsonConvert.DeserializeObject<T>(jsonData) : new T();
+            }
+        }
     }
 }

+ 26 - 0
EthermineBotTelegram/jsonCurrentStats.cs

@@ -0,0 +1,26 @@
+namespace EthermineBotTelegram
+{
+    public class Root
+    {
+        public int time { get; set; }
+        public int lastSeen { get; set; }
+        public int reportedHashrate { get; set; }
+        public double currentHashrate { get; set; }
+        public int validShares { get; set; }
+        public int invalidShares { get; set; }
+        public int staleShares { get; set; }
+        public double averageHashrate { get; set; }
+        public int activeWorkers { get; set; }
+        public long unpaid { get; set; }
+        public object unconfirmed { get; set; }
+        public double coinsPerMin { get; set; }
+        public double usdPerMin { get; set; }
+        public double btcPerMin { get; set; }
+    }
+
+    public class jsonCurrnentStats
+    {
+        public string status { get; set; }
+        public Root data { get; set; }
+    }
+}