Add config properties and worker thread

This commit is contained in:
Victor 2017-08-29 09:27:24 +03:00
parent d149f8625f
commit aa6e02e731
5 changed files with 89 additions and 52 deletions

3
config.properties Normal file
View File

@ -0,0 +1,3 @@
login=test
token=token
update_time=1

View File

@ -5,5 +5,8 @@
],
"description" : "aNMusic",
"copyright" : "Copyright © 2017, aNNiMON",
"license" : "free"
"license" : "free",
"dependencies" : {
"variantconfig": "~>1.1.5"
}
}

6
dub.selections.json Normal file
View File

@ -0,0 +1,6 @@
{
"fileVersion": 1,
"versions": {
"variantconfig": "1.1.5"
}
}

View File

@ -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);
}
}*/

View File

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