-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
223 lines (193 loc) · 7.74 KB
/
Form1.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using SpotifyAPI.Web;
using SpotifyAPI.Web.Auth;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
namespace ItunesToSpotifyForm
{
public partial class ItunesToSpotifyForm : Form
{
SpotifyWebAPI SpotifyAPI = null;
public ItunesToSpotifyForm()
{
InitializeComponent();
}
private void Form1_Load(object senderForm, EventArgs e)
{
}
private void loadConfigBtn_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\dev\spotifyConfig\config.xml");
clientIdTB.Text = doc.SelectNodes("//spotifyConfig/clientId")[0].InnerText;
clientSecretTB.Text = doc.SelectNodes("//spotifyConfig/clientSecret")[0].InnerText;
}
private void connectBtn_Click(object sender2, EventArgs e)
{
String clientId = clientIdTB.Text;
String clientSecret = clientSecretTB.Text;
if (clientId.Length > 0 && clientSecret.Length > 0)
{
AuthorizationCodeAuth auth = new AuthorizationCodeAuth(
clientId,
clientSecret,
"http://localhost:4002",
"http://localhost:4002",
Scope.PlaylistModifyPrivate | Scope.PlaylistModifyPublic | Scope.UserLibraryModify
);
auth.AuthReceived += async (sender, payload) =>
{
auth.Stop();
Token token = await auth.ExchangeCode(payload.Code);
SpotifyAPI = new SpotifyWebAPI()
{
TokenType = token.TokenType,
AccessToken = token.AccessToken
};
};
auth.Start(); // Starts an internal HTTP Server
auth.OpenBrowser();
} else
{
messageLbl.Text = "Need both client secret and client Id. These can be found on your spotify developer page (free to create account).";
}
}
private void convertBtn_Click(object sender, EventArgs e)
{
if (SpotifyAPI != null)
{
createSpotifyPlaylists();
} else
{
messageLbl.Text = "Not Ready... Not Ready.. Stop That! Did you connect to Spotify?";
}
}
private void createSpotifyPlaylists()
{
String fileName = fileNameTB.Text;
if (fileName != null && fileName.Length > 0)
{
messageLbl.Text = "";
XDocument doc = XDocument.Load(fileName); ;
List<XElement> tracksXml = doc.Root.Element("dict").Element("dict").Elements("dict").ToList(); ;
List<FullTrack> spotifyTracks = new List<FullTrack>();
for (int i = 0; i < tracksXml.Count; i++)
{
XElement track = tracksXml[i];
String name = getNode(track, "Name").Value;
String artist = getNode(track, "Artist").Value;
String album = getNode(track, "Album").Value;
ProgressLbl.Text = String.Format("Searching for ({0}/{1} -> Name: {2} Artist: {3} Album: {4} ", i + 1, tracksXml.Count, name, artist, album);
FullTrack spotifyTrack = searchSpotify(name, artist, album);
if (spotifyTrack != null)
{
spotifyTracks.Add(spotifyTrack);
}
else
{
AddTrackToErrorLog(name, artist, album);
}
}
PrivateProfile profile = SpotifyAPI.GetPrivateProfile();
if (!profile.HasError())
{
String playlistName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
FullPlaylist playlist = SpotifyAPI.CreatePlaylist(profile.Id, playlistName);
if (!playlist.HasError())
{
messageLbl.Text += "\n\nPlaylist-URI: " + playlist.Uri;
foreach(FullTrack spotifyTrack in spotifyTracks)
{
SpotifyAPI.AddPlaylistTrack(playlist.Id, spotifyTrack.Uri);
}
} else
{
messageLbl.Text += "\n\nERROR: " + playlist.Error.Message;
}
}
else
{
messageLbl.Text += "\n\nERROR: " + profile.Error.Message;
}
}
else
{
messageLbl.Text += "Please Choose a file";
}
}
private FullTrack searchSpotify(String name, String artist, String album)
{
FullTrack closestMatch = null;
SearchItem items = SpotifyAPI.SearchItems(name + " " + artist, SearchType.Track, 50);
try
{
foreach(FullTrack item in items.Tracks.Items)
{
String itunesFormattedArtists = concatArtists(item.Artists).Replace(",", "");
String itunesArtists = artist.Replace(",", "").Replace("& ", "");
if (itunesFormattedArtists.Equals(itunesArtists))
{
if (closestMatch == null)
{
closestMatch = item;
}
if(item.Album.Name.Equals(album))
{
closestMatch = item;
}
}
}
} catch (NullReferenceException e)
{
if (items.Error.Status == 429)
{
Thread.Sleep(1000);
closestMatch = searchSpotify(name, artist, album);
} else
{
AddTrackToErrorLog(name, artist, album);
}
}
return closestMatch;
}
private void AddTrackToErrorLog(String name, String artist, String album)
{
string[] lines = { String.Format("ItunesName: {0}", name), String.Format("ItunesArtist: {0}", artist), String.Format("ItunesAlbum: {0}", album) };
System.IO.File.WriteAllLines(@"C:\dev\spotifyConfig\FailedConverts.txt", lines);
}
private String concatArtists(List<SimpleArtist> Artists)
{
String itunesArtistFormatStr = "";
foreach (SimpleArtist artist in Artists)
{
itunesArtistFormatStr += String.Format("{0}, ", artist.Name);
}
return itunesArtistFormatStr.Substring(0, itunesArtistFormatStr.Length - 2);
}
private XElement getNode(XElement track, String key)
{
XElement node = track.Descendants("key").Where(x => (string)x.Value == key).FirstOrDefault();
return node != null ? (XElement)node.NextNode : new XElement("string", "");
}
private void browseBtn_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
fileNameTB.Text = openFileDialog1.FileName;
}
}
}