Ejemplo n.º 1
0
        /// <summary>
        /// Send a request to the audioscrobbler server
        /// parse the response into the approriate
        /// AudioscrobblerReponse type
        /// </summary>
        private static AudioscrobblerResponse Send(Uri url)
        {
            // the response object to return
            AudioscrobblerResponse aResponse = null;

            // create the request
            var request = (HttpWebRequest)WebRequest.Create(url);

            // set the method to POST
            request.Method        = "POST";
            request.ContentLength = 0;

            // grab the response
            // TODO: Change response type to HttpWebResponse
            // TODO: Better error handling
            using (WebResponse response = request.GetResponse())
            {
                using (Stream dataStream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(dataStream))
                    {
                        // parse the response string
                        aResponse = ParseResponse(reader.ReadToEnd());
                    }
                }
            }
            return(aResponse);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Submit a single track to audioscrobbler
        /// </summary>
        public void SubmitTrack(SongInfo track)
        {
            // verify that a successful handshake has occured
            if (_handshakeSuccessful == false)
            {
                Handshake();
            }

            // initialize the url to send requests to
            var url = new Uri(_urlPrefix + ProcessTrack(track));

            // send the request
            AudioscrobblerResponse response = Send(url);

            // set the interval variable
            _interval = response.Interval;

            // parse the response type
            // (doesn't do anything for now)
            switch (response.Type)
            {
            case AudioscrobblerResponseType.BADAUTH:
                break;

            case AudioscrobblerResponseType.FAILED:
                break;

            case AudioscrobblerResponseType.OK:
                break;
            }
        }
Ejemplo n.º 3
0
        // establish the connection between the client and audioscrobbler
        private void Handshake()
        {
            // values for client
            string clientid      = "tst";
            string clientversion = "1.0";

            // reset variables that are set during the handshake
            _urlPrefix           = string.Empty;
            _handshakeSuccessful = false;

            // generate the approriate handshake url
            // handshake url
            // {0} = clientid
            // {1} = client version
            // {2} = username
            var handshakeUrl = new Uri(string.Format("http://post.audioscrobbler.com/?hs=true&p=1.1&c={0}&v={1}&u={2}", clientid, clientversion, _username));

            // send the response
            AudioscrobblerResponse response = Send(handshakeUrl);

            // set the interval value returned by the response
            _interval = response.Interval;

            // react based on the response type
            switch (response.Type)
            {
            // successful response: grab the url to send tracks to
            case AudioscrobblerResponseType.UPTODATE:
                _urlPrefix           = GetUrlPrefix(response.Variables["MD5Challenge"], response.Variables["UrlToSubmitScript"]);
                _handshakeSuccessful = true;
                break;

            // successful response: grab the url to send tracks to
            case AudioscrobblerResponseType.UPDATE:
                _urlPrefix           = GetUrlPrefix(response.Variables["MD5Challenge"], response.Variables["UrlToSubmitScript"]);
                _handshakeSuccessful = true;
                break;

            // invalid user
            case AudioscrobblerResponseType.BADUSER:
                throw new AudioscrobblerException("Invalid User");

            // request failed for some other reason
            case AudioscrobblerResponseType.FAILED:
                throw new AudioscrobblerException(response.Variables["Reason"]);
            }
        }
Ejemplo n.º 4
0
        private static AudioscrobblerResponse GetResponse_OK(string responseString)
        {
            var response = new AudioscrobblerResponse();

            response.Type = AudioscrobblerResponseType.OK;

            string       regex   = @"OK\nINTERVAL (?<Interval>[0-9]*)";
            RegexOptions options = RegexOptions.Singleline | RegexOptions.IgnoreCase;
            var          reg     = new Regex(regex, options);

            Match match = reg.Match(responseString);

            if (match.Success)
            {
                response.Interval = Convert.ToInt32(match.Groups["Interval"].Value);
            }

            return(response);
        }
Ejemplo n.º 5
0
        private static AudioscrobblerResponse GetResponse_FAILED(string responseString)
        {
            var response = new AudioscrobblerResponse();

            response.Type = AudioscrobblerResponseType.FAILED;

            string       regex   = @"FAILED (?<Reason>[^\n]*)\nINTERVAL (?<Interval>[0-9]*)";
            RegexOptions options = RegexOptions.Singleline | RegexOptions.IgnoreCase;
            var          reg     = new Regex(regex, options);

            Match match = reg.Match(responseString);

            if (match.Success)
            {
                response.Variables.Add("Reason", match.Groups["Reason"].Value);
                response.Interval = Convert.ToInt32(match.Groups["Interval"].Value);
            }

            return(response);
        }
Ejemplo n.º 6
0
        // All the response parsers below work the same way
        // Set the approriate AudioscrobblerResponseType
        // and then user a regular expression to parse out the interval
        // and the variables

        private static AudioscrobblerResponse GetResponse_UPTODATE(string responseString)
        {
            var response = new AudioscrobblerResponse();

            response.Type = AudioscrobblerResponseType.UPTODATE;

            string       regex   = @"UPTODATE\n(?<MD5Challenge>[^\n]*)\n(?<UrlToSubmitScript>[^\n]*)\nINTERVAL (?<Interval>[0-9]*)";
            RegexOptions options = RegexOptions.Singleline | RegexOptions.IgnoreCase;
            var          reg     = new Regex(regex, options);

            Match match = reg.Match(responseString);

            if (match.Success)
            {
                response.Variables.Add("MD5Challenge", match.Groups["MD5Challenge"].Value);
                response.Variables.Add("UrlToSubmitScript", match.Groups["UrlToSubmitScript"].Value);
                response.Interval = Convert.ToInt32(match.Groups["Interval"].Value);
            }

            return(response);
        }