Ejemplo n.º 1
0
        public static List <BeatsaverMap> FindMapsSingleSong(SongModel song)
        {
            List <BeatsaverMap> matches = new List <BeatsaverMap>();

            foreach (var m in mapDump)
            {
                if (MapMatchesSong(m, song))
                {
                    matches.Add(m);
                }
            }

            if (matches.Count > 0)
            {
                var matchesJson = JsonConvert.SerializeObject(matches);
                matchesJson = matchesJson.Replace("'", "''");
                SQLite.ExecuteNonQuery($"update songs set matches='{matchesJson}' where id='{song.SongID}';");
            }

            return(matches);
        }
Ejemplo n.º 2
0
        public static List <PlaylistModel> GetPlaylists(bool fromLocal = false)
        {
            List <PlaylistModel> playlistModels = new List <PlaylistModel>();

            if (!fromLocal)
            {
                dynamic playlistGet      = GetRequest("https://api.spotify.com/v1/me/playlists");
                dynamic spotifyPlaylists = playlistGet["items"];

                var localPlaylists = SQLite.ExecuteQuery("select * from playlists where platform='spotify';").Rows;
                int playlistDiff   = localPlaylists.Count - spotifyPlaylists.Count;

                /*
                 * if (playlistDiff > 0)
                 * {
                 *  foreach (DataRow lP in localPlaylists)
                 *  {
                 *      foreach (var sP in spotifyPlaylists)
                 *      {
                 *          if (lP["id"] == sP["id"])
                 *              break;
                 *      }
                 *
                 *      SQLite.ExecuteNonQuery($"delete from playlists where id='{lP["id"]}' and platform='spotify';");
                 *      SQLite.ExecuteNonQuery($"delete from songs where playlist_id='{lP["id"]}' and platform='spotify';");
                 *      break;
                 *  }
                 * }
                 */
                foreach (dynamic p in spotifyPlaylists)
                {
                    int numTracks = p["tracks"]["total"];

                    List <SongModel> songs = new List <SongModel>();

                    for (int i = 0; i < Math.Ceiling(numTracks / 100f); i++)
                    {
                        dynamic tracks = GetRequest($"https://api.spotify.com/v1/playlists/{p["id"]}/tracks?offset={i * 100}")["items"];
                        foreach (dynamic t in tracks)
                        {
                            if (t["track"] != null)
                            {
                                string        songName = t["track"]["name"];
                                string        songID   = t["track"]["id"];
                                List <string> artists  = new List <string>();
                                foreach (dynamic a in t["track"]["artists"])
                                {
                                    if (a["name"] != "Various Artists")
                                    {
                                        artists.Add((string)a["name"]);
                                    }
                                }
                                string artistsString = string.Join(", ", artists.ToArray());

                                int numSongs = SQLite.ExecuteQuery($"select * from songs where id='{songID}' and playlist_id='{p["id"]}' and platform='spotify';").Rows.Count;
                                if (numSongs == 0)
                                {
                                    Console.WriteLine($"{songName} - {p["name"]}");
                                    SQLite.ExecuteNonQuery($"insert into songs (title, artists, id, playlist_id, platform) values (\"{SQLite.EscapeString(songName)}\", \"{SQLite.EscapeString(artistsString)}\", '{songID}', \"{p["id"]}\", 'spotify');");
                                }

                                List <BeatsaverMap> matches = new List <BeatsaverMap>();
                                var matchesJson             = SQLite.ExecuteQuery($"select * from songs where id='{songID}' and platform='spotify';").Rows[0]["matches"];
                                if (matchesJson.GetType() != typeof(DBNull))
                                {
                                    matches = JsonConvert.DeserializeObject <List <BeatsaverMap> >((string)matchesJson);
                                }
                                songs.Add(new SongModel()
                                {
                                    SongTitle = songName, SongArtists = artists, SongID = songID, MapMatches = matches
                                });
                            }
                        }
                    }
                    if (SQLite.ExecuteQuery($"select * from playlists where id='{p["id"]}' and platform='spotify';").Rows.Count == 0)
                    {
                        SQLite.ExecuteNonQuery($"insert into playlists (id, name, view_order, platform) values ('{p["id"]}', \"{p["name"]}\", '{playlistModels.Count+1}', 'spotify');");
                    }
                    else
                    {
                        SQLite.ExecuteNonQuery($"update playlists set name=\"{p["name"]}\", view_order='{playlistModels.Count + 1}' where id='{p["id"]}' and platform='spotify';");
                    }

                    playlistModels.Add(new PlaylistModel()
                    {
                        PlaylistName = p["name"], Songs = songs
                    });
                }
            }
            else
            {
                var             playlistRows  = SQLite.ExecuteQuery("select * from playlists where platform='spotify';").Rows;
                PlaylistModel[] playlistArray = new PlaylistModel[playlistRows.Count];
                foreach (DataRow row in playlistRows)
                {
                    playlistArray[Convert.ToInt32(row["view_order"]) - 1] = new PlaylistModel()
                    {
                        PlaylistName = (string)row["name"], Songs = new List <SongModel>()
                    };
                }

                var songRows = SQLite.ExecuteQuery("select * from songs order by playlist_id;").Rows;
                foreach (DataRow row in songRows)
                {
                    List <BeatsaverMap> matches = new List <BeatsaverMap>();
                    if (row["matches"].GetType() != typeof(DBNull))
                    {
                        matches = JsonConvert.DeserializeObject <List <BeatsaverMap> >((string)row["matches"]);
                    }
                    SongModel s = new SongModel()
                    {
                        SongTitle = (string)row["title"], SongArtists = ((string)row["artists"]).Split(new string[] { ", " }, StringSplitOptions.None).ToList(), SongID = (string)row["id"], MapMatches = matches
                    };
                    int index = Convert.ToInt32(SQLite.ExecuteQuery($"select view_order from playlists where id='{row["playlist_id"]}'").Rows[0]["view_order"]) - 1;
                    playlistArray[index].Songs.Add(s);
                }

                playlistModels = playlistArray.ToList();
            }

            return(playlistModels);
        }