Skip to content

Instantly share code, notes, and snippets.

@kddlb kddlb/Program.cs
Created Jun 19, 2019

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
using Newtonsoft.Json;
using SpotifyLibraryExporter.SpotifyAPI;
namespace SpotifyLibraryExporter
{
class Program
{
static async Task Main(string[] args)
{
var token = "<secret>";
var lib = new List<SpotifyAPI.Track>();
var spot = Library.FromJson(await "https://api.spotify.com/v1"
.AppendPathSegments("me", "tracks")
.SetQueryParam("offset", 0)
.SetQueryParam("limit", 50)
.WithOAuthBearerToken(token)
.GetStringAsync());
lib.AddRange(spot.Items.Select(x=>x.Track));
Console.WriteLine($"Added offset {spot.Offset}...");
while (spot.Next != null)
{
spot = Library.FromJson(await spot.Next.ToString()
.WithOAuthBearerToken(token)
.GetStringAsync());
lib.AddRange(spot.Items.Select(x=>x.Track));
Console.WriteLine($"Added offset {spot.Offset}...");
}
System.IO.File.WriteAllText(@"C:\Src\library.json", JsonConvert.SerializeObject(lib,Formatting.Indented));
}
}
}
// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using SpotifyLibraryExporter.SpotifyAPI;
//
// var library = Library.FromJson(jsonString);
using System.Net.Mime;
namespace SpotifyLibraryExporter.SpotifyAPI
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class Library
{
[JsonProperty("href")]
public Uri Href { get; set; }
[JsonProperty("items")]
public Item[] Items { get; set; }
[JsonProperty("limit")]
public long Limit { get; set; }
[JsonProperty("next")]
public Uri Next { get; set; }
[JsonProperty("offset")]
public long Offset { get; set; }
[JsonProperty("previous")]
public Uri Previous { get; set; }
[JsonProperty("total")]
public long Total { get; set; }
public override string ToString()
{
return $"Spotify Library response with {Total} items";
}
}
public partial class Item
{
[JsonProperty("added_at")]
public DateTimeOffset AddedAt { get; set; }
[JsonProperty("track")]
public Track Track { get; set; }
public override string ToString()
{
return $"Track \"{Track}\" added on {AddedAt:g}";
}
}
public partial class Track
{
[JsonProperty("album")]
public Album Album { get; set; }
[JsonProperty("artists")]
public Artist[] Artists { get; set; }
[JsonProperty("available_markets")]
public string[] AvailableMarkets { get; set; }
[JsonProperty("disc_number")]
public long DiscNumber { get; set; }
[JsonProperty("duration_ms")]
public long DurationMs { get; set; }
[JsonProperty("explicit")]
public bool Explicit { get; set; }
[JsonProperty("external_ids")]
public ExternalIds ExternalIds { get; set; }
[JsonProperty("external_urls")]
public ExternalUrls ExternalUrls { get; set; }
[JsonProperty("href")]
public Uri Href { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("is_local")]
public bool IsLocal { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("popularity")]
public long Popularity { get; set; }
[JsonProperty("preview_url")]
public Uri PreviewUrl { get; set; }
[JsonProperty("track_number")]
public long TrackNumber { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("uri")]
public string Uri { get; set; }
public override string ToString()
{
return $"{Name} by {String.Join(", ", Artists.Select(x => x.Name).ToArray())}";
}
}
public partial class Album
{
[JsonProperty("album_type")]
public string AlbumType { get; set; }
[JsonProperty("artists")]
public Artist[] Artists { get; set; }
[JsonProperty("available_markets")]
public string[] AvailableMarkets { get; set; }
[JsonProperty("external_urls")]
public ExternalUrls ExternalUrls { get; set; }
[JsonProperty("href")]
public Uri Href { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("images")]
public Image[] Images { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("release_date")]
public string ReleaseDate { get; set; }
[JsonProperty("release_date_precision")]
public string ReleaseDatePrecision { get; set; }
[JsonProperty("total_tracks")]
public long TotalTracks { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("uri")]
public string Uri { get; set; }
public override string ToString()
{
return $"{Name} by {String.Join(", ", Artists.Select(x => x.Name).ToArray())}";
}
}
public partial class Artist
{
[JsonProperty("external_urls")]
public ExternalUrls ExternalUrls { get; set; }
[JsonProperty("href")]
public Uri Href { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("uri")]
public string Uri { get; set; }
public override string ToString()
{
return Name;
}
}
public partial class ExternalUrls
{
[JsonProperty("spotify")]
public Uri Spotify { get; set; }
public override string ToString()
{
return this.Spotify.ToString();
}
}
public partial class Image
{
[JsonProperty("height")]
public long Height { get; set; }
[JsonProperty("url")]
public Uri Url { get; set; }
[JsonProperty("width")]
public long Width { get; set; }
public override string ToString()
{
return Url.ToString();
}
}
public partial class ExternalIds
{
[JsonProperty("isrc")]
public string Isrc { get; set; }
}
public partial class Library
{
public static Library FromJson(string json) => JsonConvert.DeserializeObject<Library>(json, SpotifyLibraryExporter.SpotifyAPI.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Library self) => JsonConvert.SerializeObject(self, SpotifyLibraryExporter.SpotifyAPI.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.