Exemplo n.º 1
0
        private async void getPlaylist()
        {
            //show progress indicator
            setProgressIndicator(true);
            SystemTray.ProgressIndicator.Text = "Getting your Tweet Mix";

            try
            {
                //set an int as the id
                int id = 0;
                //build twitter url
                string url = twitter.getUrl(username, apiAttempt);

                //call twitter vaI base api class, we only want the response code at this point
                string apiResponse = await twitter.Call(url);

                //if the response did not come back null or contain an error code
                if (apiResponse != null || apiResponse.Contains("Error: "))
                {
                    //use json.net to turn json into an opject
                    TwitterItems apiData = JsonConvert.DeserializeObject <TwitterItems>(apiResponse);

                    //loop through and set everything to the list
                    foreach (Status data in apiData.statuses)
                    {
                        PlayListItems items = new PlayListItems();
                        //add id to list
                        items.ID = id.ToString();
                        //add twitter details to list
                        items.Message      = data.text;
                        items.ProfileImage = data.user.profile_image_url;
                        items.Username     = data.user.name;
                        //go spilt the message via the Song class
                        Song song = spiltMessage(data.text);
                        //add those values to the list
                        items.ArtistName = song.Artist;
                        items.SongName   = song.Track;

                        //now we need the artowrk so go get it from spotify as its more reliable
                        SpotifyApi spotify = new SpotifyApi();
                        //build a url of the songname to go ask spotify for its details
                        string spotifySearchUrl = spotify.BuildSearchUrl(song.Track);

                        string response = await spotify.Call(spotifySearchUrl);

                        //add in method so that calls can be used again
                        if (response != null)
                        {
                            List <SpotifyItem> tracks         = new List <SpotifyItem>();
                            SpotifyItem        spotifyApiData = JsonConvert.DeserializeObject <SpotifyItem>(response);

                            //call the spotify embed api to go get artwork
                            string spotifyArtowrkUrl = spotify.buildEmbedUrl(spotifyApiData.tracks[0].href);
                            string artworkResponse   = await spotify.Call(spotifyArtowrkUrl);

                            SpotifyImageItems imageData = JsonConvert.DeserializeObject <SpotifyImageItems>(artworkResponse);
                            items.AlbumArtworkUrl = imageData.thumbnail_url;
                        }
                        Items.Add(items);
                        id++;
                    }
                }
                else if (apiResponse.Contains("Error: "))
                {
                    //lets go see what error it is
                    int errorCode = twitter.getCorrectError(apiResponse);
                    if (errorCode == 429 && apiAttempt < 3)
                    {
                        setProgressIndicator(false);
                        //call again;
                        callAgain();
                    }
                    else
                    {
                        string errorMessage = twitter.ErrorMesssage(errorCode);
                        showError(errorMessage);
                        callAgain();
                        setProgressIndicator(false);
                    }
                }
                setProgressIndicator(false);
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Wifi is disabled in phone settings");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                setProgressIndicator(false);
                callAgain();
            }

            setDataContext();
        }