Exemplo n.º 1
0
        public static async Task <InsightData[]> GetInsightDataAsync(Spotify sp, SpotterAzure_dbContext dbContext)
        {
            IQueryable <Listen> listens = dbContext.Listens.Where(x => x.SpotId == sp.SpotId).OrderByDescending(x => x.ListenAt).Take(100);

            Listen[] _listens = listens.ToArray();
            Track[]  _tracks  = listens.Select(x => x.Track).ToArray();

            Dictionary <int, InsightData> data = new Dictionary <int, InsightData>();

            for (int i = 0; i < _tracks.Length; i++)
            {
                if (data.Keys.Contains(_tracks[i].TrkId))
                {
                    if (_listens[i].ListenAt > data[_tracks[i].TrkId].listen.ListenAt)
                    {
                        data[_tracks[i].TrkId].listen.ListenAt = _listens[i].ListenAt;
                    }
                    data[_tracks[i].TrkId].count++;
                }
                else
                {
                    data.Add(_tracks[i].TrkId, new InsightData(_listens[i], _tracks[i], sp, dbContext));
                }
            }

            return(data.Values.ToArray());
        }
Exemplo n.º 2
0
            public InsightData(Listen listen, Track track, Spotify sp, SpotterAzure_dbContext dbContext)
            {
                this.listen = listen;
                this.track  = track;

                Task <Features> f = this.track.GetFeatures(sp, dbContext);
                Task <Artist>   a = this.track.GetArtist(sp, dbContext);

                if (!f.IsCompleted)
                {
                    f.Start(); f.Wait();
                }
                if (!a.IsCompleted)
                {
                    a.Start(); a.Wait();
                }

                this.features = f.Result;
                this.artist   = a.Result;

                if (this.artist.Details != null)
                {
                    this.genres = this.artist._artistDetails.genres.SelectMany(x => x.Split(' ')).Distinct().ToArray();
                }
            }
Exemplo n.º 3
0
        public async Task <IActionResult> Post([FromQuery] string code, [FromServices] SpotterAzure_dbContext dbContext)
        {
            if (code != null)
            {
                Spotify u = AuthFlow.FromCode(code);
                if (u != null)
                {
                    while (u.SpotifyId == "" || u.SpotifyId == null)
                    {
                    }

                    string authToken = Session.GetAuthToken();

                    IQueryable <Spotify> spot = dbContext.Spotifies.Where(x => x.SpotifyId == u.SpotifyId).Select(x => x);
                    if (spot.Any())
                    {
                        Spotify f = spot.First();
                        f.AuthExpires  = u.AuthExpires;
                        f.AuthToken    = u.AuthToken;
                        f.RefreshToken = u.RefreshToken;
                        dbContext.Spotifies.Update(f);
                    }
                    else
                    {
                        await dbContext.Spotifies.AddAsync(u);
                    }

                    await dbContext.SaveChangesAsync();

                    IQueryable <Session> sess = dbContext.Sessions.Where(x => x.SpotId == spot.First().SpotId);

                    if (!sess.Any())
                    {
                        Session s = new Session(authToken, spot.First());
                        await dbContext.Sessions.AddAsync(s);
                    }
                    else
                    {
                        Session s = sess.First();
                        s.SetAuthToken(authToken);
                        dbContext.Sessions.Update(s);
                    }

                    await dbContext.SaveChangesAsync();

                    CookieOptions options = new CookieOptions();
                    options.Expires = DateTimeOffset.Now.AddDays(1);

                    Actions.Log.Add("User Signed Up", Actions.LogError.Info);

                    HttpContext.Response.Cookies.Append("spotid", spot.First().SpotId.ToString(), options);

                    HttpContext.Response.Cookies.Append("authToken", authToken, options);
                    return(RedirectPermanent("/Insights"));
                }
            }
            return(BadRequest());
        }
Exemplo n.º 4
0
        public IActionResult Settings([FromServices] SpotterAzure_dbContext dbContext)
        {
            Spotify sp = authDetails.CheckAuth(Request, dbContext);

            if (sp != null)
            {
                return(View("Settings", sp));
            }
            else
            {
                return(Redirect("LoginError"));
            }
        }
Exemplo n.º 5
0
        public static async void CheckEvents(SpotterAzure_dbContext dbContext)
        {
            foreach (Spotify s in dbContext.Spotifies.ToArray())
            {
                if (await s.IsAlive())
                {
                    s.SetupKicked();

                    Setting _sett = dbContext.Settings.First(x => x.SpotId == s.SpotId);
                    dbContext.Entry(_sett).Reload();
                    s.Setting = _sett;

                    CheckUserEvent(s, dbContext);
                }
            }
        }
Exemplo n.º 6
0
        private static async void CheckUserEvent(Spotify user, SpotterAzure_dbContext dbContext)
        {
            CurrentlyPlayingContext playing = await user.spotify.Player.GetCurrentPlayback();

            if (playing != null)
            {
                if (!lastTracks.ContainsKey(user.SpotId))
                {
                    lastTracks.Add(user.SpotId, playing);
                    user.last      = playing;
                    user.lastTrack = (FullTrack)user.last.Item;
                }
                else if (playing.Item != null)
                {
                    FullTrack track = (FullTrack)playing.Item;

                    user.last      = lastTracks[user.SpotId];
                    user.lastTrack = (FullTrack)user.last.Item;

                    lastTracks[user.SpotId] = playing;

                    //Check if skipped
                    if (track.Id != user.lastTrack.Id)
                    {
                        Track  t = new Track(track, user);
                        Artist a = await t.GetArtist(user, dbContext);

                        t.ArtistId = a.ArtistId;

                        t.Artist = a;
                        await t.GetFeatures(user, dbContext);

                        if (!dbContext.Artists.Any(x => x.ArtistId == t.ArtistId))
                        {
                            (await dbContext.Artists.AddAsync(a)).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                        }
                        else if (!dbContext.Tracks.Any(x => x.TrackId == t.TrackId))
                        {
                            (await dbContext.Tracks.AddAsync(t)).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                        }

                        if (dbContext.Tracks.Any(x => x.TrackId == track.Id))
                        {
                            t = dbContext.Tracks.Where(x => x.TrackId == track.Id).First();

                            if (t.Features == null)
                            {
                                await t.GetFeatures(user, dbContext);
                            }
                            if (t.Artist == null)
                            {
                                t.ArtistId = a.ArtistId; await t.GetArtist(user, dbContext);
                            }

                            dbContext.Tracks.Update(t);
                        }

                        if (user.last.ProgressMs < user.lastTrack.DurationMs * IsntSkip)
                        {
                            if (OnSkip != null)
                            {
                                Skip s = await OnSkip(user, user.lastTrack, playing, dbContext);

                                if (s != null)
                                {
                                    s.Track = t;
                                    await dbContext.Skips.AddAsync(s);
                                }
                            }
                        }
                        dbContext.Listens.Add(new Listen(track, user));
                        if (OnNextSong != null)
                        {
                            OnNextSong(user, playing);
                        }
                    }


                    //Check if play state changed
                    if (playing.IsPlaying != user.last.IsPlaying)
                    {
                        if (playing.IsPlaying)
                        {
                            if (OnResume != null)
                            {
                                OnResume(user, playing);
                            }
                            else if (OnPause != null)
                            {
                                OnPause(user, playing);
                            }
                        }
                    }
                }
            }

            await dbContext.SaveChangesAsync();
        }
Exemplo n.º 7
0
        public IActionResult UpdateUser([FromForm] IFormCollection formDetails)
        {
            SpotterAzure_dbContext dbContext = new SpotterAzure_dbContext();

            Spotify sp = authDetails.CheckAuth(Request, dbContext);

            if (sp != null)
            {
                Setting setting = dbContext.Settings.First(x => x.SpotId == sp.SpotId);

                if (formDetails.ContainsKey("SkipOn"))
                {
                    setting.SkipOn = true;
                }
                else
                {
                    setting.SkipOn = false;
                }

                if (formDetails.ContainsKey("SkipIgnorePlaylist"))
                {
                    setting.SkipIgnorePlaylist = true;
                }
                else
                {
                    setting.SkipIgnorePlaylist = false;
                }

                if (formDetails.ContainsKey("SkipRemoveFromPlaylist"))
                {
                    setting.SkipRemoveFromPlaylist = true;
                }
                else
                {
                    setting.SkipRemoveFromPlaylist = false;
                }

                if (formDetails.ContainsKey("SkipMustBeLiked"))
                {
                    setting.SkipMustBeLiked = true;
                }
                else
                {
                    setting.SkipMustBeLiked = false;
                }

                if (formDetails.ContainsKey("SkipIgnorePostfix"))
                {
                    setting.SkipIgnorePostfix = formDetails["SkipIgnorePostfix"];
                }
                else
                {
                    setting.SkipIgnorePostfix = "-";
                }

                if (formDetails.ContainsKey("ShuffleOn"))
                {
                    setting.ShuffleOn = true;
                }
                else
                {
                    setting.ShuffleOn = false;
                }

                if (formDetails.ContainsKey("ShuffleAlbums"))
                {
                    setting.ShuffleAlbums = true;
                }
                else
                {
                    setting.ShuffleAlbums = false;
                }

                if (formDetails.ContainsKey("ShufflePlaylists"))
                {
                    setting.ShufflePlaylists = true;
                }
                else
                {
                    setting.ShufflePlaylists = false;
                }

                setting.SkipExpiryHours = int.Parse(formDetails["SkipExpiryHours"].First()) * 24;
                setting.SkipTrigger     = int.Parse(formDetails["SkipTrigger"].First());

                dbContext.Update(setting);
                dbContext.SaveChangesAsync();
            }

            return(RedirectPermanent("/settings"));
        }
Exemplo n.º 8
0
        public IActionResult Log([FromServices] SpotterAzure_dbContext dbContext)
        {
            Spotify sp = authDetails.CheckAuth(Request, dbContext);

            return(View("Log", sp));
        }
Exemplo n.º 9
0
        public static async Task <Skip> Skipped(Spotify user, FullTrack track, CurrentlyPlayingContext playing, SpotterAzure_dbContext dbContext)
        {
            if (user.Setting.SkipOn.Value &&
                !(user.Setting.SkipIgnorePlaylist.Value && (playing.Context != null && playing.Context.Type == "playlist")))
            {
                if (playing.Context == null || playing.Context.Type != "playlist" || !(await user.spotify.Playlists.Get(playing.Context.Href.Split('/').Last())).Name.EndsWith(user.Setting.SkipIgnorePostfix))
                {
                    List <bool> Exists = await user.spotify.Library.CheckTracks(new LibraryCheckTracksRequest(new List <string>()
                    {
                        track.Id
                    }));

                    if (Exists[0] || !user.Setting.SkipMustBeLiked.Value)
                    {
                        int recent = user.RecentSkips(track.Id, user.Setting.SkipExpiryHours.Value, dbContext);

                        if (recent >= user.Setting.SkipTrigger - 1)
                        {
                            if (user.KickedTracks.Count(x => ((FullTrack)x.Track).Id == track.Id) == 0)
                            {
                                await user.spotify.Playlists.AddItems(user.KickedPlaylist.Id, new PlaylistAddItemsRequest(new List <string>()
                                {
                                    track.Uri
                                }));

                                Track t = new Track(track, user);
                                user.KickedTracks.Add(new PlaylistTrack <IPlayableItem>());
                                user.KickedTracks.Last().Track = track;
                            }

                            if (Exists[0])
                            {
                                await user.spotify.Library.RemoveTracks(new LibraryRemoveTracksRequest(new List <string>()
                                {
                                    track.Id
                                }));
                            }

                            if (playing.Context.Type == "playlist" && user.Setting.SkipRemoveFromPlaylist.Value)
                            {
                                PlaylistRemoveItemsRequest removeReq = new PlaylistRemoveItemsRequest();

                                PlaylistRemoveItemsRequest.Item removeSongReq = new PlaylistRemoveItemsRequest.Item();
                                removeSongReq.Uri = track.Uri;
                                removeReq.Tracks  = new List <PlaylistRemoveItemsRequest.Item>()
                                {
                                    removeSongReq
                                };

                                await user.spotify.Playlists.RemoveItems(playing.Context.Href.Split('/').Last(), removeReq);
                            }
                        }
                        else
                        {
                        }
                        return(new Skip(track.Id, user));
                    }
                }
            }
            return(null);
        }