-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
119 lines (112 loc) · 5.5 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.Threading;
class Program
{
static void Main(string[] args)
{
Console.Title = "last.rpc";
var clientID = "879203754992349284";
Discord.Discord discord = new Discord.Discord(Int64.Parse(clientID), (UInt64)Discord.CreateFlags.Default);
/* ok somehow this was causing memory corruption so
// ahahahahhahahah nvm it was discord being fucking stupid
discord.SetLogHook(Discord.LogLevel.Debug, (level, message) =>
{
Console.WriteLine("[Discord][{0}] {1}", level, message);
});*/
var applicationManager = discord.GetApplicationManager();
Logger.LogDiscord("Current Locale: " + applicationManager.GetCurrentLocale().ToString());
Logger.LogDiscord("Current Branch: " + applicationManager.GetCurrentBranch().ToString());
Discord.ActivityManager activityManager = discord.GetActivityManager();
Dictionary<string, string>? config = ConfigManager.GetConfig();
string api_key = config["api_key"];
string lastfm_user = config["lastfm_user"];
long poll_time = (config["poll_time"] != null ? Int64.Parse(config["poll_time"]) : (long)5) * 1000;
Logger.LogLast("Fetching user \"" + lastfm_user + "\" on last.fm");
lastapi lastfmapi = new lastapi(api_key);
Discord.ActivityManager.UpdateActivityHandler callback = result =>
{
Logger.LogDiscord("Update presence " + result.ToString().ToUpper());
};
try
{
string lastScrobble = "somethingRAndoMjuStInCassse8938981-0++=noSongsLLnamEdlikeThisAFoLikesCockUwu12309-==";
while (true)
{
try
{
discord.RunCallbacks();
Dictionary<string, string> nowplaying = lastfmapi.GetNowPlaying(lastfm_user);
if (nowplaying.Count != 0)
{
if (nowplaying["name"] != lastScrobble)
{
string nowscrobbling = "Now scrobbling: " + nowplaying["name"] + " by " + nowplaying["artist"];
Console.Title = "last.rpc | Connected as " + lastfm_user + " | " + nowscrobbling;
Logger.LogLast(nowscrobbling + (nowplaying["album"] != "" ? " (on " + nowplaying["album"] + ")" : ""));
lastScrobble = nowplaying["name"];
Logger.LogDiscord("Updating presence");
Dictionary<string, dynamic> extraData = lastfmapi.GetTrackInfo(nowplaying["name"], nowplaying["artist"]);
int endOffset = 0;
bool isFavorite = false;
if (extraData.Count > 0)
{
endOffset += extraData["duration"];
isFavorite = extraData["isFavorite"];
};
var albumFormatted = "";
if (nowplaying["album"] != "" && nowplaying["album"] != nowplaying["name"])
{
albumFormatted = " - " + nowplaying["album"];
};
var activity = new Discord.Activity
{
Type = Discord.ActivityType.Listening,
Details = nowplaying["name"],
State = nowplaying["artist"] + albumFormatted,
Assets =
{
LargeImage = "lastfm",
LargeText = nowplaying["userName"] + " has " + nowplaying["totalScrobbles"] + " total scrobbles",
},
Instance = true,
};
if (isFavorite)
{
activity.Assets.SmallImage = "loved";
activity.Assets.SmallText = "Loved Track";
};
if (endOffset > 0)
{
activity.Timestamps.End = DateTimeOffset.Now.ToUnixTimeMilliseconds() + endOffset;
}
activityManager.UpdateActivity(activity, callback);
}
}
else if (lastScrobble != "")
{
Console.Title = "last.rpc | Connected as " + lastfm_user + " | Scrobbling paused";
Logger.LogLast("Scrobbling stopped");
Logger.LogDiscord("Clearing presence");
activityManager.ClearActivity(result =>
{
Logger.LogDiscord("Clear presence " + result.ToString().ToUpper());
});
lastScrobble = "";
}
}
catch (Exception err)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(err.ToString());
Console.ForegroundColor = ConsoleColor.Gray;
}
Thread.Sleep(5000);
};
}
finally
{
discord.Dispose();
};
}
}