using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.XPath;
using System.Net;
using System.Web;
using System.Xml;
using System.IO;
namespace AmazonWebHelpers
{
// Content item for the combo box
public class AmazonItem
{
public string Title;
public string ASIN;
public string Artist;
public AmazonItem(string title, string Asin, string artist)
{
Title = title; ASIN = Asin; Artist = artist;
}
public override string ToString()
{
// Generates the text shown in the combo box
if (Artist == "")
return Title + " [Various]";
else
return Title + " [" + Artist + "]";
}
}
public class AmazonAlbumRetriever
{
#region private
private XmlNamespaceManager _ns;
private XPathNavigator _nav;
private string _proxyname;
private string _proxypassword;
private string _proxydomain;
private string _wsKey;
#endregion
#region Constructor
public AmazonAlbumRetriever(string WSKey){
_proxydomain="";
_proxyname="";
_proxypassword="";
_wsKey= WSKey;
_ns = new XmlNamespaceManager(new NameTable());
_ns.AddNamespace("asr", "http://webservices.amazon.com/AWSECommerceService/2007-01-15");
}
public AmazonAlbumRetriever(string WSKey,string ProxyName,string ProxyPassword,string ProxyDomain){
_proxydomain=ProxyDomain;
_proxyname=ProxyName;
_proxypassword=ProxyPassword;
_wsKey= WSKey;
_ns = new XmlNamespaceManager(new NameTable());
_ns.AddNamespace("asr", "http://webservices.amazon.com/AWSECommerceService/2007-01-15");
}
#endregion // Constructor
#region Property
///
/// The XMLNavigator for the loaded page.
///
public XPathNavigator XMLNavigator
{
get
{
if (_nav==null)
throw new ArgumentException("The XmlNavigator must be initialised before it is accessed.");
else
return _nav;
}
}
public int GetArtistAlbumResultsLength
{
get
{
if (_nav == null)
{
throw new ArgumentException("The XmlNavigator must be initialised before it is accessed.");
}
else
{
try
{
return this._nav.SelectSingleNode("/asr:ItemSearchResponse/asr:Items/asr:TotalResults", this._ns).ValueAsInt;
}
catch
{
return 0;
}
}
}
}
public int GetArtistAlbumPagesNo
{
get{
if (_nav == null)
{
throw new ArgumentException("The XmlNavigator must be initialised before it is accessed.");
}
else
{
try
{
return this._nav.SelectSingleNode("/asr:ItemSearchResponse/asr:Items/asr:TotalPages", this._ns).ValueAsInt;
}
catch
{
return 0;
}
}
}
}
#endregion
#region Methods
public string GetAlbumImageURL(string ASIN)
{
HttpWebRequest request;
HttpWebResponse response;
XPathNavigator nav;
XPathNavigator resnav;
try
{
string uri = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=" + this._wsKey + "&Operation=ItemLookup&AssociateTag=devconn-20&Version=2007-01-15&ItemId=" + ASIN + "&ResponseGroup=Images";
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
//set proxy auth if required
if (this._proxyname != "")
{
if (this._proxydomain == "")
request.Proxy.Credentials = new NetworkCredential(this._proxyname, this._proxypassword);
else
request.Proxy.Credentials = new NetworkCredential(this._proxyname, this._proxypassword,this._proxydomain);
}
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
nav = new XPathDocument(response.GetResponseStream()).CreateNavigator();
resnav = nav.SelectSingleNode("/asr:ItemLookupResponse/asr:Items/asr:Item/asr:LargeImage/asr:URL", this._ns);
return resnav.ToString();
}
return "";
}
catch
{
return "";
}
}
public string[] GetAlbumtracks(string ASIN)
{
HttpWebRequest request;
HttpWebResponse response;
XPathNavigator nav;
XPathNodeIterator nitr;
List Tracks = new List();
XPathNavigator resnav;
try
{
string uri = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=" + this._wsKey + "&Operation=ItemLookup&AssociateTag=devconn-20&Version=2007-01-15&ItemId=" + ASIN + "&ResponseGroup=Tracks";
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
//set proxy auth if required
if (this._proxyname != "")
{
if (this._proxydomain == "")
request.Proxy.Credentials = new NetworkCredential(this._proxyname, this._proxypassword);
else
request.Proxy.Credentials = new NetworkCredential(this._proxyname, this._proxypassword, this._proxydomain);
}
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
nav = new XPathDocument(response.GetResponseStream()).CreateNavigator();
nitr = this._nav.Select("/asr:ItemLookupResponse/asr:Items/asr:Item/asr:Tracks/asr:Disc/asr:Track", this._ns);
while (nitr.MoveNext())
{
Tracks.Add(nitr.Current.Value);
}
}
return Tracks.ToArray();
}
catch
{
return new List().ToArray();
}
}
public List GetArtistAlbumList(string artistname,int Page)
{
HttpWebRequest request;
HttpWebResponse response;
XPathNodeIterator nitr;
string CurrentASIN="";
string CurrentTitle = "";
string CurrentArtist = "";
List ItemList = new List();
string uri = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=" + this._wsKey + "&Operation=ItemSearch&AssociateTag=&Version=2007-01-15&SearchIndex=Music&Artist=" + artistname + "&ItemPage=" + Page.ToString();
try
{
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
//set proxy auth if required
if (this._proxyname != "")
{
if (this._proxydomain == "")
request.Proxy.Credentials = new NetworkCredential(this._proxyname, this._proxypassword);
else
request.Proxy.Credentials = new NetworkCredential(this._proxyname, this._proxypassword, this._proxydomain);
}
response = (HttpWebResponse)request.GetResponse();
this._nav = new XPathDocument(response.GetResponseStream()).CreateNavigator();
//Check that we got a result
if (this.GetArtistAlbumResultsLength != 0)
{
if (Page <= this.GetArtistAlbumPagesNo){
nitr = this._nav.Select("/asr:ItemSearchResponse/asr:Items/asr:Item", this._ns);
while (nitr.MoveNext())
{
XPathNodeIterator _nodeChildren = nitr.Current.SelectChildren(XPathNodeType.All);
while (_nodeChildren.MoveNext())
{
if (_nodeChildren.Current.LocalName == "ASIN")
CurrentASIN = _nodeChildren.Current.Value;
if (_nodeChildren.Current.LocalName == "ItemAttributes")
{
XPathNodeIterator _nodegChildren = _nodeChildren.Current.SelectChildren(XPathNodeType.All);
while (_nodegChildren.MoveNext())
{
if (_nodegChildren.Current.LocalName == "Title")
CurrentTitle = _nodegChildren.Current.Value;
if (_nodegChildren.Current.LocalName == "Artist")
CurrentArtist = _nodegChildren.Current.Value;
}
}
}
if (CurrentTitle != "" && CurrentASIN != "")
{
ItemList.Add(new AmazonItem(CurrentTitle,CurrentASIN, CurrentArtist));
}
CurrentTitle = "";
CurrentASIN = "";
CurrentArtist = "";
}
}
}
return ItemList;
}
catch
{
ItemList.Clear();
return ItemList;
}
}
public byte[] DownloadImagetobytes(string uri)
{
Stream stream = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
//set proxy auth if required
if (this._proxyname != "")
{
if (this._proxydomain == "")
request.Proxy.Credentials = new NetworkCredential(this._proxyname, this._proxypassword);
else
request.Proxy.Credentials = new NetworkCredential(this._proxyname, this._proxypassword, this._proxydomain);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
return ReadFully(stream, 32768);
}
public Stream DownloadImagetoStream(string uri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
//set proxy auth if required
if (this._proxyname != "")
{
if (this._proxydomain == "")
request.Proxy.Credentials = new NetworkCredential(this._proxyname, this._proxypassword);
else
request.Proxy.Credentials = new NetworkCredential(this._proxyname, this._proxypassword, this._proxydomain);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response.GetResponseStream();
}
private static byte[] ReadFully(Stream stream, int initialLength)
{
if (initialLength < 1)
{
initialLength = 32768;
}
byte[] buffer = new byte[initialLength];
int read = 0;
int chunk;
while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
read += chunk;
/* If we've reached the end of our buffer, check to see if there's
any more information */
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
/* End of stream? If so, we're done */
if (nextByte == -1)
{
return buffer;
}
/* Nope. Resize the buffer, put in the byte we've just
read, and continue */
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
}
}
/* Buffer is now too big. Shrink it. */
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}
#endregion
}
}