Пример #1
0
        public async Task <string> GetUsersSpotifyUserName()
        {
            SpotifyUser user   = new SpotifyUser();
            string      token  = Request.Cookies["spot_toke"].Value.ToString();
            HttpClient  client = new HttpClient();

            client.BaseAddress = new Uri("https://api.spotify.com/v1/me");
            string phantomPram = "";

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            HttpResponseMessage response = client.GetAsync(phantomPram).Result;

            if (response.IsSuccessStatusCode)
            {
                var     result      = response.Content.ReadAsStringAsync().Result;
                string  replacement = System.Text.RegularExpressions.Regex.Replace(result, @"\t|\n|\r|\\\\", "");
                JObject s           = JObject.Parse(replacement);

                user = s.ToObject <SpotifyUser>();

                using (RelayDJDevEntities db = new RelayDJDevEntities())
                {
                    string userId = User.Identity.GetUserId();
                    Dj     item   = db.Djs.Where(x => x.DjUserId == userId).FirstOrDefault();
                    item.SpotifyUserName = user.id;
                    db.SaveChanges();
                }
            }
            return(user.id);
        }
        public async Task <LocationDevices> GetLocationDevices(string token)
        {
            LocationDevices locationDevices = new LocationDevices();

            using (RelayDJDevEntities db = new RelayDJDevEntities())
            {
                string userId = User.Identity.GetUserId();
                locationDevices.LocationId = db.Locations.Where(x => x.LocationOwnerId == userId).SingleOrDefault().LocationId;
            }
            List <Devices> devices = new List <Devices>();
            HttpClient     client  = new HttpClient();

            client.BaseAddress = new Uri("https://api.spotify.com/v1/me/player/devices");
            string phantomPram = "";

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            HttpResponseMessage response = client.GetAsync(phantomPram).Result;

            if (response.IsSuccessStatusCode)
            {
                var     result      = response.Content.ReadAsStringAsync().Result;
                string  replacement = System.Text.RegularExpressions.Regex.Replace(result, @"\t|\n|\r|\\\\", "");
                JObject s           = JObject.Parse(replacement);

                JArray sentDevices = (JArray)s["devices"];
                locationDevices.devices = sentDevices.ToObject <List <Devices> >();
            }
            return(locationDevices);
        }
        public ActionResult AddLocationConfirm(Location model)
        {
            string cacheOneTitle = model.LocationName + ":spotifyplaylistid";

            HttpContext.Cache[cacheOneTitle] = model.SpotifyPlayListId;

            HttpCookie cookie = new HttpCookie("stalled_location_name");

            HttpContext.Response.Cookies.Remove("stalled_location_name");
            cookie.Value = model.LocationName;
            HttpContext.Response.SetCookie(cookie);

            using (RelayDJDevEntities db = new RelayDJDevEntities())
            {
                Location loc = new Location();
                loc.LocationId        = Guid.NewGuid().ToString();
                loc.LocationName      = model.LocationName;
                loc.SportifyUserName  = model.SportifyUserName;
                loc.SpotifyPlayListId = model.SpotifyPlayListId;
                loc.LocationOwnerId   = User.Identity.GetUserId();
                db.Locations.Add(loc);
                db.SaveChanges();
            }
            return(RedirectToAction("LocationGetCode"));
        }
Пример #4
0
        public ActionResult UserSelectedLocation(string locationId)
        {
            HttpCookie cookie = new HttpCookie("selected_location");

            HttpContext.Response.Cookies.Remove("selected_location");
            cookie.Value = locationId;
            HttpContext.Response.SetCookie(cookie);

            using (RelayDJDevEntities db = new RelayDJDevEntities())
            {
                List <RelayOrder> ro = db.RelayOrders.Where(x => x.Id == locationId).ToList();
                if (ro.Count > 0)
                {
                    // there are already people in the list you need to get the max count
                    RelayOrder addingAnotherUser = new RelayOrder()
                    {
                        Id                  = Guid.NewGuid().ToString(),
                        RelayDate           = DateTime.Today,
                        UserId              = User.Identity.GetUserId(),
                        EnteredLocationDate = DateTime.Now,
                        RelayOrder1         = ro.Max(x => x.RelayOrder1) + 1,
                        PlaylistId          = db.Locations.Where(x => x.LocationId == locationId).SingleOrDefault().SpotifyPlayListId,
                        MaxPlays            = 2,
                        Selected            = 0,
                        NextInQue           = false,
                        ProcessCount        = 0,
                        LocationId          = locationId
                    };

                    db.RelayOrders.Add(addingAnotherUser);
                }
                else
                {
                    //there is no one is the list right now this user is the first one
                    RelayOrder addFirstUser = new RelayOrder()
                    {
                        Id                  = Guid.NewGuid().ToString(),
                        RelayDate           = DateTime.Today,
                        UserId              = User.Identity.GetUserId(),
                        EnteredLocationDate = DateTime.Now,
                        RelayOrder1         = 0,
                        PlaylistId          = db.Locations.Where(x => x.LocationId == locationId).SingleOrDefault().SpotifyPlayListId,
                        MaxPlays            = 2,
                        Selected            = 0,
                        NextInQue           = false,
                        ProcessCount        = 0,
                        LocationId          = locationId
                    };

                    db.RelayOrders.Add(addFirstUser);
                }

                db.SaveChanges();
            }
            return(RedirectToAction("SearchArtist"));
        }
Пример #5
0
        public ActionResult SelectLocation()
        {
            List <Location> locs = new List <Location>();

            using (RelayDJDevEntities db = new RelayDJDevEntities())
            {
                locs = db.Locations.ToList();
            }
            return(View(locs));
        }
        public async Task <string> GainLocationToken(string code)
        {
            var spotifyClient = WebConfigurationManager.AppSettings["SpotifyAPIClientID"];
            var spotifySecret = WebConfigurationManager.AppSettings["SpotifyAPIClientSecretID"];

            HttpClient client     = new HttpClient();
            var        authHeader = Convert.ToBase64String(Encoding.Default.GetBytes($"{spotifyClient}:{spotifySecret}"));

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeader);
            client.BaseAddress = new Uri("https://accounts.spotify.com/api/token");
            Dictionary <string, string> prams = new Dictionary <string, string>();

            prams.Add("grant_type", "authorization_code");
            prams.Add("code", code);
            prams.Add("redirect_uri", "http://whiskeyandtrust.azurewebsites.net/Locations/GetLocationToken/");

            HttpResponseMessage response = await client.PostAsync(client.BaseAddress.ToString(), new FormUrlEncodedContent(prams));

            var textResponse = await response.Content.ReadAsStringAsync();;

            string first = textResponse.ToString();

            string[] ara    = first.Split(',');
            string   change = ara[0].ToString();

            string[] aratwo     = change.Split(':');
            string   finalToken = aratwo[1].Replace("\"", "");

            string locationName = Request.Cookies["stalled_location_name"].Value.ToString();

            string   cachedLocationToken = "";
            Location location            = new Location();

            using (RelayDJDevEntities db = new RelayDJDevEntities())
            {
                string userId = User.Identity.GetUserId();
                location = db.Locations.Where(x => x.LocationOwnerId == userId).FirstOrDefault();
            }
            if (location != null)
            {
                cachedLocationToken = location.LocationId + ":token";
            }

            HttpContext.Cache[cachedLocationToken] = finalToken;

            return(finalToken);
        }
Пример #7
0
        public async Task <ActionResult> FinishRegistration(Dj model)
        {
            if (!string.IsNullOrEmpty(model.DJName))
            {
                using (RelayDJDevEntities db = new RelayDJDevEntities())
                {
                    model.DjId          = Guid.NewGuid().ToString();
                    model.AgreedToTerms = true;
                    model.Active        = true;
                    model.DjUserId      = User.Identity.GetUserId();

                    db.Djs.Add(model);
                    db.SaveChanges();
                }
            }
            return(RedirectToAction("Index"));
        }
        public ActionResult LocationDeviceSelected(string deviceId, string locationId)
        {
            string   locationName         = Request.Cookies["stalled_location_name"].Value.ToString();
            string   cachedLocationDevice = "";
            Location location             = new Location();

            using (RelayDJDevEntities db = new RelayDJDevEntities())
            {
                location = db.Locations.Where(x => x.LocationId == locationId).FirstOrDefault();
            }
            if (location != null)
            {
                cachedLocationDevice = location.LocationId + ":device";
            }
            HttpContext.Cache[cachedLocationDevice] = deviceId;
            return(RedirectToAction("Index", "Home"));
        }
Пример #9
0
        public ActionResult Index()
        {
            string usersID = User.Identity.GetUserId();

            using (RelayDJDevEntities db = new RelayDJDevEntities())
            {
                Dj dj = db.Djs.Where(x => x.DjUserId == usersID).FirstOrDefault();
                //string userName = await GetUsersSpotifyUserName();
                if (dj != null)
                {
                    //set location of playlist
                    return(View());
                }
                else
                {
                    return(RedirectToAction("RegisterDJ", "Account", new { id = usersID }));
                }
            }
        }
Пример #10
0
        public async Task <bool> AddToPlayList(string id)
        {
            string spotifyplayListId = "";
            string locationId        = "";

            using (RelayDJDevEntities db = new RelayDJDevEntities())
            {
                string userId = User.Identity.GetUserId();
                spotifyplayListId = db.RelayOrders.Where(x => x.UserId == userId).SingleOrDefault().PlaylistId;
                locationId        = db.RelayOrders.Where(x => x.UserId == userId).SingleOrDefault().LocationId;
            }
            bool   addedToPlaylist = false;
            string playlist        = "";

            string   userName = "";
            Location pulled   = new Location();

            using (RelayDJDevEntities db = new RelayDJDevEntities())
            {
                pulled = db.Locations.Where(x => x.LocationId == locationId).FirstOrDefault();
            }
            if (!string.IsNullOrEmpty(pulled.SportifyUserName))
            {
                userName = pulled.SportifyUserName;
                playlist = pulled.SpotifyPlayListId;
            }

            string locationTokenPull = pulled.LocationId + ":token";
            string token             = (string)HttpContext.Cache[locationTokenPull];

            if (!String.IsNullOrEmpty(token))
            {
                addedToPlaylist = await AddingToPlayList(playlist, userName, id, token);
            }
            return(addedToPlaylist);
        }
Пример #11
0
        public async Task <bool> PlayerCheck(string playlist, string userName, string token)
        {
            bool        nothing     = false;
            PlayerCheck playerCheck = new PlayerCheck();

            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            string url = "https://api.spotify.com/v1/me/player";

            client.BaseAddress = new Uri(url);
            string urlParameters         = "";
            HttpResponseMessage response = client.GetAsync(urlParameters).Result;

            if (response.IsSuccessStatusCode)
            {
                var     result      = response.Content.ReadAsStringAsync().Result;
                string  replacement = System.Text.RegularExpressions.Regex.Replace(result, @"\t|\n|\r", "");
                JObject s           = JObject.Parse(replacement);

                playerCheck = JsonConvert.DeserializeObject <PlayerCheck>(replacement);
                if (!playerCheck.is_playing)
                {
                    // need device id

                    string locationId = "";
                    using (RelayDJDevEntities db = new RelayDJDevEntities())
                    {
                        string userId = User.Identity.GetUserId();
                        locationId = db.RelayOrders.Where(x => x.UserId == userId).SingleOrDefault().LocationId;
                    }

                    Location pulled = new Location();
                    using (RelayDJDevEntities db = new RelayDJDevEntities())
                    {
                        pulled = db.Locations.Where(x => x.LocationId == locationId).FirstOrDefault();
                    }

                    string locationdevicePull = pulled.LocationId + ":device";
                    string deviceId           = (string)HttpContext.Cache[locationdevicePull];


                    // we need to get the play list content

                    string thing = "{\"context_uri\":\"spotify:user:"******":playlist:" + playlist + "\",\"position_ms\":0}";

                    string     urlpc    = "https://api.spotify.com/v1/me/player/play" + "?device_id=" + deviceId;
                    HttpClient clientpc = new HttpClient();
                    clientpc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                    clientpc.BaseAddress = new Uri(urlpc);
                    var httpContent = new StringContent(thing, Encoding.UTF8, "application/json");
                    HttpResponseMessage responsepc = await clientpc.PutAsync(clientpc.BaseAddress.ToString(), httpContent);

                    if (responsepc.IsSuccessStatusCode)
                    {
                        //play back has started.
                    }
                }
            }
            return(nothing);
        }