From aa6e02e731f73abf2b7d47df678fe822ca4dd3ef Mon Sep 17 00:00:00 2001 From: Victor Date: Tue, 29 Aug 2017 09:27:24 +0300 Subject: [PATCH] Add config properties and worker thread --- config.properties | 3 ++ dub.json | 5 +- dub.selections.json | 6 +++ source/aimpremote/aimpremote.d | 41 ---------------- source/app.d | 86 ++++++++++++++++++++++++++++++---- 5 files changed, 89 insertions(+), 52 deletions(-) create mode 100644 config.properties create mode 100644 dub.selections.json diff --git a/config.properties b/config.properties new file mode 100644 index 0000000..748b198 --- /dev/null +++ b/config.properties @@ -0,0 +1,3 @@ +login=test +token=token +update_time=1 diff --git a/dub.json b/dub.json index 4ea9a01..1d0dd01 100644 --- a/dub.json +++ b/dub.json @@ -5,5 +5,8 @@ ], "description" : "aNMusic", "copyright" : "Copyright © 2017, aNNiMON", - "license" : "free" + "license" : "free", + "dependencies" : { + "variantconfig": "~>1.1.5" + } } diff --git a/dub.selections.json b/dub.selections.json new file mode 100644 index 0000000..32949ad --- /dev/null +++ b/dub.selections.json @@ -0,0 +1,6 @@ +{ + "fileVersion": 1, + "versions": { + "variantconfig": "1.1.5" + } +} diff --git a/source/aimpremote/aimpremote.d b/source/aimpremote/aimpremote.d index a119a4b..9031228 100644 --- a/source/aimpremote/aimpremote.d +++ b/source/aimpremote/aimpremote.d @@ -136,44 +136,3 @@ public static Nullable!TrackInfo getInfo() { private static HWND findPlayerWindow() { return FindWindow(AIMPRemoteAccessClass.ptr, null); } - -/*void main() { - import std.stdio : writeln, write; - - auto haimp = OpenFileMapping(FILE_MAP_READ, 0, AIMPRemoteAccessClass.ptr); - auto hfilemap = MapViewOfFile(haimp, FILE_MAP_READ, 0, 0, AIMPRemoteAccessMapFileSize); - - AIMPRemoteFileInfo* info = cast(AIMPRemoteFileInfo*) hfilemap; - write("Bitrate: "); - writeln(info.bitRate); - write("Samlerate: "); - writeln(info.sampleRate); - write("fileSize: "); - writeln(info.fileSize); - - auto bytes = (cast(ubyte*) hfilemap)[0 .. AIMPRemoteAccessMapFileSize]; - writeln(bytes); - - - const wcharsize = wchar.sizeof; - auto pbuff = AIMPRemoteFileInfo.sizeof; - - DWORD[TagItem] lengthInfo; - wstring[TagItem] trackInfo; - - lengthInfo[TagItem.Album] = info.albumLength; - lengthInfo[TagItem.Artist] = info.artistLength; - lengthInfo[TagItem.Date] = info.dateLength; - lengthInfo[TagItem.FileName] = info.fileNameLength; - lengthInfo[TagItem.Genre] = info.genreLength; - lengthInfo[TagItem.Title] = info.titleLength; - foreach (key; TagItem.min .. TagItem.max) { - auto length = lengthInfo[key]; - trackInfo[key] = cast(wstring)(bytes[pbuff .. (pbuff + length * wcharsize)]); - pbuff += length * wcharsize; - } - - foreach (key, value; trackInfo) { - writeln(value); - } -}*/ diff --git a/source/app.d b/source/app.d index 611c6a4..2bcf059 100644 --- a/source/app.d +++ b/source/app.d @@ -1,25 +1,91 @@ import std.stdio; import std.net.curl; +import std.datetime; +import std.typecons; +import std.algorithm.searching : startsWith; +import std.conv : to; +import std.file; +import core.thread; import aimpremote; +import variantconfig; +shared string login; +shared string token; +shared int update_time; -void main() -{ +bool readConfig() { + if (!exists("config.properties")) { + writeln("Unable to open config.properties file"); + return false; + } + auto config = VariantConfig("config.properties"); + login = config["login"].toStr; + if (login.length == 0) { + writeln("Unable to find login"); + return false; + } + token = config["token"].toStr; + if (token.length == 0) { + writeln("Unable to find token"); + return false; + } + update_time = config["update_time"].toInt; + if (update_time <= 0) { + update_time = 2; + } + return true; +} + +void process() { + final switch (getPlayerState()) { + case PlayerState.Off: + case PlayerState.Stopped: + case PlayerState.Paused: + Thread.sleep(dur!"seconds"(10)); + break; + + case PlayerState.Playing: + auto track = getInfo(); + if (!track.isNull) { + auto time = Clock.currTime(); + writefln("[%02d:%02d] %s - %s", + time.hour, time.minute, + track.artist, track.title); + sendTrackInfo(track); + } + Thread.sleep(dur!"minutes"(update_time)); + break; + } +} + +void sendTrackInfo(Nullable!TrackInfo track) { auto http = HTTP(); // http.caInfo("cacert.pem"); http.handle.set(CurlOption.ssl_verifypeer, 0); - auto track = getInfo(); - if (track.isNull) { - writeln("No data"); - return; - } - auto content = post("https://annimon.com/json/nowplay", [ - "login" : "test", - "token" : "5token", + "login" : login, + "token" : token, "artist" : to!string(track.artist), "title" : to!string(track.title), "genre" : to!string(track.genre) ], http); + if (content.startsWith("{\"error")) { + writeln(content); + } +} + +int main() +{ + writeln("Starting aNMusic"); + if (!readConfig()) { + return 1; + } + + new Thread({ + while (true) { + process(); + } + }).start(); + return 0; }