HiddenSounds/HiddenUpdater/Program.cs

99 lines
3.8 KiB
C#
Raw Normal View History

2019-05-23 21:27:46 +00:00
using System;
using System.IO;
2019-05-23 23:06:17 +00:00
using System.Linq;
2019-05-23 21:27:46 +00:00
using System.Threading.Tasks;
2019-05-23 23:06:17 +00:00
using Newtonsoft.Json;
2019-05-23 21:27:46 +00:00
using Newtonsoft.Json.Linq;
using SpotifyAPI.Web;
using SpotifyAPI.Web.Auth;
namespace HiddenUpdater
{
internal class Program
{
private const string PlaylistId = "2YDWLcBzTpDNQDfcQyy76b";
public static async Task Main(string[] args)
{
2019-07-23 23:18:15 +00:00
Console.Write("Download (1) or lyrics (2): ");
if (int.Parse(Console.ReadLine()) != 1)
{
StoreLyrics.Store();
return;
}
2019-05-23 21:27:46 +00:00
var auth = new CredentialsAuth("ce11c54b88cf41149e528de5ec73aa69", File.ReadAllText("secret.txt"));
var token = await auth.GetToken();
var spotify = new SpotifyWebAPI
{
AccessToken = token.AccessToken,
TokenType = token.TokenType
};
var playlist = spotify.GetPlaylist(PlaylistId);
Console.WriteLine(playlist);
var playlistJson = new JArray();
var tracks = playlist.Tracks;
Console.WriteLine("Total: " + tracks.Total);
int C = 0;
2019-05-23 23:06:17 +00:00
var artistJson = new JObject();
for (int i = 0; i < tracks.Total; i += 100)
2019-05-23 21:27:46 +00:00
{
if (i > 0)
tracks = spotify.GetPlaylistTracks(PlaylistId, offset: i);
2019-05-23 23:06:17 +00:00
foreach (var track in tracks.Items.Select(tr => tr.Track))
2019-05-23 21:27:46 +00:00
{
2019-05-23 23:06:17 +00:00
var obj = new JObject();
2019-06-07 01:20:54 +00:00
obj["sname"] = track.Name;
2019-05-23 23:06:17 +00:00
/*obj["artists"] = new JArray(track.Artists.Select(artist => new JObject
{
{"name", artist.Name},
{"url", artist.ExternalUrls["spotify"]}
}));*/
var artJson = new JArray();
foreach (var artist in track.Artists)
{
if (!artistJson.ContainsKey(artist.Name))
artistJson[artist.Name] = new JObject
{
2019-06-07 01:20:54 +00:00
{"sname", artist.Name},
{"surl", artist.ExternalUrls["spotify"]},
{"sid", artist.Id}
2019-05-23 23:06:17 +00:00
};
artJson.Add(artist.Name);
}
obj["artists"] = artJson;
obj["popularity"] = track.Popularity;
obj["durationMs"] = track.DurationMs;
2019-06-07 01:20:54 +00:00
obj["surl"] = track.ExternUrls["spotify"];
obj["sid"] = track.Id;
2019-05-23 23:06:17 +00:00
playlistJson.Add(obj);
2019-05-23 21:27:46 +00:00
C++;
}
}
Console.WriteLine(C + " / " + tracks.Total);
2019-05-23 23:06:17 +00:00
Console.WriteLine("Getting artists...");
2019-07-02 19:26:53 +00:00
C = 0;
int max = artistJson.Count;
2019-05-23 23:06:17 +00:00
foreach (var kv in artistJson)
{
2019-06-07 01:20:54 +00:00
var artist = spotify.GetArtist((string) kv.Value["sid"]);
2019-05-23 23:06:17 +00:00
var artJson = kv.Value;
artJson["followers"] = artist?.Followers?.Total;
artJson["popularity"] = artist?.Popularity;
artJson["genres"] = new JArray(artist?.Genres);
2019-07-02 19:26:53 +00:00
C++;
if (C % 10 == 0) Console.Write("\r" + C + "/" + max + " - " + (C / (float) max) * 100 + "%");
2019-05-23 23:06:17 +00:00
}
2019-07-02 19:26:53 +00:00
Console.WriteLine("\r" + C + "/" + max + " - " + (C / (float) max) * 100 + "%");
2019-05-23 23:06:17 +00:00
//Console.WriteLine(artistJson.ToString(Formatting.None));
File.WriteAllText("songs.json", new JArray(playlistJson.OrderBy(song=>song["sid"])).ToString());
File.WriteAllText("artists.json", new JArray(artistJson.Children().Select(tk=>tk.Last)
.OrderBy(artist=>artist["sname"])).ToString());
2019-05-23 21:27:46 +00:00
}
}
}