Ejemplo n.º 1
0
        /// <summary>
        /// Find a Track from it's Spotify URI.
        /// If it isn't in the DB we will load it's details and create it.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="reqUri"></param>
        /// <returns></returns>
        public static Track GetOrCreateTrackBySpotifyUri(E3RadioEntities db, string reqUri)
        {
            // find existing track, if it has been played before
            var track = db.Tracks.FirstOrDefault(t => t.SpotifyUri == reqUri);

            if (track == null)
            {
                // Track is not in the database, load info from spotify/last.fm
                track = new Track()
                {
                    SpotifyUri = reqUri,
                    Likes      = 0,
                    Dislikes   = 0,
                    DateAdded  = DateTime.Now
                };

                // look up track info from spotify or die
                if (!LookupTrackInfo(track))
                {
                    return(null);
                }

                // update additional track info (cover art) from last.fm
                LastFM.UpdateTrackFromLastFm(track);

                db.Tracks.Add(track);
            }
            else // Track is already in the database.
            {
                // Update fields we recently added from Spotify
                if (!track.Length.HasValue)
                {
                    LookupTrackInfo(track);
                }

                // Try to get cover art if not set
                if (track.PictureSmall == null)
                {
                    LastFM.UpdateTrackFromLastFm(track);
                }
            }

            return(track);
        }
Ejemplo n.º 2
0
        public static User GetOrCreateUser(E3RadioEntities db, long userId)
        {
            var u = db.Users.SingleOrDefault(us => us.UserID == userId);

            if (u == null)
            {
                u = new e3Radio.Data.User();

                // get the dude's info from book of face
                var     fb = new Facebook.FacebookClient();
                dynamic me = fb.Get("/" + userId);
                u.UserID       = userId;
                u.Username     = me.username ?? me.name;
                u.Name         = me.name;
                u.FacebookLink = me.link;
                u.DateCreated  = DateTime.Now;

                db.Users.Add(u);
                db.SaveChanges();
            }
            return(u);
        }