Skip to content
This repository has been archived by the owner on Jun 23, 2021. It is now read-only.

Commit

Permalink
Updated to v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
b1sergiu committed May 11, 2020
1 parent 2c8db64 commit a4ce4c1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
19 changes: 11 additions & 8 deletions DeezerAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.InteropServices;

using Newtonsoft.Json;
using System.Net.Http;

namespace Deezcord
{
Expand All @@ -19,12 +20,12 @@ public static class DeezerAPI
private const string userEndpoint = "https://api.deezer.com/user/me";
private const string tokenPath = "token.txt";

private static string token;
public static string token;

public static async Task Authenticate()
{
Console.WriteLine ($"Redirect URI: {redirectUri}");

HttpListener http = new HttpListener();
http.Prefixes.Add(redirectUri);
Console.WriteLine ("Listening...");
Expand Down Expand Up @@ -96,10 +97,10 @@ await responseOutput.WriteAsync (buffer, 0, buffer.Length).ContinueWith ((tasl)

try
{
WebResponse tokenResponse = await tokenRequest.GetResponseAsync ();
using (StreamReader reader = new StreamReader(tokenResponse.GetResponseStream () ?? throw new Exception ()))
WebResponse tokenResponse = await tokenRequest.GetResponseAsync();
using (StreamReader reader = new StreamReader(tokenResponse.GetResponseStream() ?? throw new Exception()))
{
string responseText = await reader.ReadToEndAsync ();
string responseText = await reader.ReadToEndAsync();

token = responseText.Split('&')[0].Remove(0, 13);

Expand All @@ -114,10 +115,10 @@ await responseOutput.WriteAsync (buffer, 0, buffer.Length).ContinueWith ((tasl)
if (e.Response is HttpWebResponse r)
{
Console.WriteLine ($"HTTP: {response.StatusCode}");
using (StreamReader reader = new StreamReader (r.GetResponseStream () ?? throw new Exception ()))
using (StreamReader reader = new StreamReader(r.GetResponseStream() ?? throw new Exception()))
{
string responseText = await reader.ReadToEndAsync();
Console.WriteLine (responseText);
Console.WriteLine(responseText);
}
}
}
Expand All @@ -126,6 +127,7 @@ await responseOutput.WriteAsync (buffer, 0, buffer.Length).ContinueWith ((tasl)
public static async Task<Track> LastTrack()
{
string requestUri = $"{lastTrackEndpoint}?access_token={token}";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

HttpWebRequest userinfoRequest = (HttpWebRequest)WebRequest.Create(requestUri);
userinfoRequest.Method = "GET";
Expand All @@ -139,12 +141,12 @@ public static async Task<Track> LastTrack()

if (userinfoResponseText.Contains ("Invalid OAuth access token."))
{
Console.WriteLine("Invalid OAuth access token.");
await Authenticate();
return await LastTrack();
}

History tracks = JsonConvert.DeserializeObject<History> (userinfoResponseText);

return tracks.Tracks [0];
}
}
Expand All @@ -166,6 +168,7 @@ public static async Task<User> User()

if (userinfoResponseText.Contains ("Invalid OAuth access token."))
{
Console.WriteLine("Invalid OAuth access token.");
await Authenticate();
return await User();
}
Expand Down
11 changes: 9 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Net;
using System.Net.Security;
using System.Threading.Tasks;
using DiscordRPC;

Expand All @@ -7,6 +9,7 @@ namespace Deezcord
public class Program
{
static DiscordRpcClient client = new DiscordRpcClient(APIKeys.DiscordApplicationId);
public static long currentlyPlaying;
private static void Main()
{
Console.WriteLine("Deezcord Plus - https://github.com/b1sergiu/deezcord-plus/");
Expand All @@ -32,8 +35,12 @@ public static async Task StartPresence()
while (true)
{
Track track = await DeezerAPI.LastTrack();
UpdatePresence(track);
Console.WriteLine($"Currently playing: {track.Title} by {track.Artist.Name} - {track.Album.Title}");
if (currentlyPlaying != track.Id)
{
UpdatePresence(track);
Console.WriteLine($"Currently playing: {track.Title} by {track.Artist.Name} - {track.Album.Title}");
currentlyPlaying = track.Id;
}
await Task.Delay(30000);
}
}
Expand Down

0 comments on commit a4ce4c1

Please sign in to comment.