Esempio n. 1
0
        public Response(HttpWebResponse response) {
            System.Globalization.CultureInfo enUS = new System.Globalization.CultureInfo("en-US");
            Tracks = new List<Track>();
            for (int i = 0; i < response.Headers.Count; i++)
            {
                KeyValuePair<string, string> header = new KeyValuePair<string, string>(response.Headers.GetKey(i), response.Headers.Get(i));
                switch (header.Key)
                {
                    case "Status":
                        this.Status = header.Value;
                        if(header.Value.StartsWith("200")) {
                            this.Success = true;
                        }
                        else
                        {
                            this.Success = false;
                        }
                        break;


                    default:
                        // any other response we are not interested...
                        break;
                }
            }


            XmlDocument xmlDoc = new XmlDocument();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string result = reader.ReadToEnd();
                response.Close();
                xmlDoc.LoadXml(result);
            }

            XmlNodeList rootList = xmlDoc.GetElementsByTagName("lfm");
            XmlNode rootElement = rootList[0];
            if (true)
            {
                if (rootElement.Attributes["status"].Value.ToLower() == "ok")
                {
                    ErrorCode = 0;
                    Success = true;
                    ErrorText = "";
                    XmlNodeList allTracks = xmlDoc.GetElementsByTagName("track");
                    foreach (XmlNode currentNode in allTracks)
                    {
                        Track track = new Track();
                        try {
                            if(currentNode.Attributes["nowplaying"].Value == "true") {
                                track.NowPlaying = true;
                            }
                        } catch {}
                        foreach(XmlNode subNode in currentNode) {
                            switch(subNode.Name) {
                                case "artist":
                                    track.Artist = subNode.InnerText;
                                    break;

                                case "name":
                                    track.Name = subNode.InnerText;
                                    break;

                                case "album":
                                    track.Album = subNode.InnerText;
                                    break;

                                case "url":
                                    track.Link = subNode.InnerText;
                                    break;

                                case "image":
                                switch (subNode.Attributes["size"].Value) {
                                    case "small":
                                        track.AlbumArtSmall = subNode.InnerText;
                                        break;

                                    case "medium":
                                        track.AlbumArtMedium = subNode.InnerText;
                                        break;

                                    case "large":
                                        track.AlbumArtLarge = subNode.InnerText;
                                        break;

                                    case "extralarge":
                                        track.AlbumArtExtraLarge = subNode.InnerText;
                                        break;
                                }
                                break;

                                default:
                                    // ignoring
                                break;
                            }
                        
                        }
                        if (track.NowPlaying)
                        {
                            NowPlaying = track;
                        }
                        else
                        {
                            if (LastPlayed == null)
                            {
                                LastPlayed = track;
                            }
                        }
                        Tracks.Add(track);
                    }

                }
                else
                {
                    XmlElement errorElement = xmlDoc.GetElementById("error");
                    ErrorCode = Convert.ToInt32(errorElement.Attributes["code"].Value);
                    ErrorText = errorElement.Value;
                    Success = false;

                }
            }
            else
            {
                Success = false;
                ErrorCode = 999;
                ErrorText = "Invalid response from last.fm";
            }


        }
        static void monitorUser()
        {
            int lastConnectionErrorId = 0;

            
            //MD5Hash key = new MD5Hash("1ca43b465fcb2306f51f4df7c010067c", true, Encoding.ASCII);
            //MD5Hash secret = new MD5Hash("8c225ea33ff798e680368c2cd45a5f1e", true, Encoding.ASCII);
            //AuthData myAuth = new AuthData(key, secret);
            //Settings20.AuthData = myAuth;

            // LastFmClient client = LastFmClient.Create(myAuth);
            //  client.LastFmUser.Username = "******";
            //  client.LastFmUser.EncryptAndSetPassword("xxxx");

            Track lastPlaying = new Track();
            Track lastRecent = new Track(); ;
            bool directlyAfterStart = true;

            while (true)
            {
                try
                {

                    Response lastFmData = HttpCommunications.SendGetRequest("http://ws.audioscrobbler.com/2.0/", new
                    {
                        method = "user.getrecenttracks",
                        user = userNameString,
                        api_key = "1ca43b465fcb2306f51f4df7c010067c",
                        limit = 2
                    }, false);

                    if (!lastFmData.Success)
                    {
                        lastConnectionErrorId = SnarlConnector.ShowMessageEx("last.fm error", "last.fm API error", lastFmData.ErrorText, 20, iconPath, hwnd, Snarl.WindowsMessage.WM_USER + 13, "");
                    }
                    else
                    {
                        Track nowPlaying = lastFmData.NowPlaying;
                        Track lastTrack = lastFmData.LastPlayed;


                        if (nowPlaying != null && lastPlaying.Name != nowPlaying.Name)
                        {

                            string artworkPath = nowPlaying.getBestAlbumArt();
                            if (artworkPath == "")
                            {
                                artworkPath = iconPath;
                            }
                            SnarlConnector.ShowMessageEx("Now being played track", nowPlaying.Artist, nowPlaying.Name + "\n\n" + nowPlaying.Album, 10, artworkPath, hwnd, Snarl.WindowsMessage.WM_USER + 11, "");
                            snarlComWindow.currentUrl = nowPlaying.Link;
                            lastPlaying = nowPlaying;

                        }
                        if (lastTrack != null && lastRecent.Name != lastTrack.Name)
                        {
                            if (!directlyAfterStart)
                            {
                                string artworkPath = lastTrack.getBestAlbumArt();
                                if (artworkPath == "")
                                {
                                    artworkPath = iconPath;
                                }
                                SnarlConnector.ShowMessageEx("Recently played track", lastTrack.Artist, lastTrack.Name + "\n\n" + lastTrack.Album, 10, artworkPath, hwnd, Snarl.WindowsMessage.WM_USER + 12, "");
                                snarlComWindow.recentUrl = lastTrack.Link;


                            }


                            lastRecent = lastTrack;
                        }

                        directlyAfterStart = false;
                    }
                    Thread.Sleep(1000);
                }


                catch (Exception exp)
                {
                    if (lastConnectionErrorId == 0 && exp.Message != "Thread was being aborted.")
                    {
                        lastConnectionErrorId = SnarlConnector.ShowMessageEx("Connection error", "Connection to last.fm failed", "Connection to last.fm can't be established. Maybe the site is down or your internet connection is not available.\n\n" + exp.Message, 20, iconPath, hwnd, Snarl.WindowsMessage.WM_USER + 13, "");
                        if (DEBUG)
                        {
                            SnarlConnector.ShowMessageEx("Debug message", "Error message", exp.Message, 0, iconPath, hwnd, Snarl.WindowsMessage.WM_USER + 77, "");
                            SnarlConnector.ShowMessageEx("Debug message", "Error source", exp.Source, 0, iconPath, hwnd, Snarl.WindowsMessage.WM_USER + 77, "");
                            SnarlConnector.ShowMessageEx("Debug message", "Stack trace", exp.StackTrace.Substring(0, 500), 0, iconPath, hwnd, Snarl.WindowsMessage.WM_USER + 77, "");
                        }
                    }
                }
            }
        }