// コンストラクタ /// <summary> /// アマ検の検索のためのアクションのインスタンスを開始します。 /// </summary> public AmazonDoSearch(AmazonSearch model, AbsPath path, string query, AmazonIndexType ait, int page) : base(model, path){ myPath = myModel.BasePath; myQuery = query; myIndexType = ait; myPageNum = page; if(myPageNum < 1) myPageNum = 1; }
protected XmlNode GetSelect(AmazonIndexType selectedType){ XmlNodeList xnl = Model.Document.DocumentElement.GetElementsByTagName(AmazonSearch.AmazonFormOptionsName); if(xnl == null || xnl.Count == 0) throw new Exception(AmazonSearch.AmazonFormOptionsName + "要素がみつかりません。"); XmlElement options = xnl[0] as XmlElement; XmlNode result = Html.CreateDocumentFragment(); XmlElement label = Html.Create("label", null, "検索対象:"); label.SetAttribute("for", AmazonSearch.IndexTypeName); result.AppendChild(label); XmlElement select = Html.Create("select"); select.SetAttribute("name", AmazonSearch.IndexTypeName); select.SetAttribute("id", AmazonSearch.IndexTypeName); foreach(XmlElement e in options.GetElementsByTagName(AmazonSearch.OptionName)){ XmlElement option = Html.Create("option"); option.InnerText = e.InnerText; string val = e.GetAttributeValue(AmazonSearch.ValueAttributeName); AmazonIndexType currentType = (AmazonIndexType)Enum.Parse(typeof(AmazonIndexType), val, true); if((selectedType == AmazonIndexType.None && e.Attributes["selected"] != null) || currentType == selectedType){ option.SetAttribute("selected", "selected"); } option.SetAttribute("value", currentType.ToString()); select.AppendChild(option); } result.AppendChild(select); return result; }
// 検索結果を取得します。 // キャッシュにあればそこから、無ければリクエストを行います。 public AmazonItemList GetSearchItem(AmazonIndexType index, string query, int pageNum){ string key = index.ToString() + "/" + query + "/" + pageNum.ToString(); if(myItemsDic.ContainsKey(key)) return myItemsDic[key]; AmazonItemList i = Search(index, query, pageNum); myItemsDic[key] = i; return i; }
// キーワードとページ数、検索対象のAmazonIndexTypeを指定して、ItemSearchの結果を含むXmlDocumentを取得します。 public XmlDocument GetItemSearchXml(string keywords, AmazonIndexType type, int itemPage){ Dictionary<string, string> requestParams = new Dictionary<string, String>(); requestParams["Service"] = "AWSECommerceService"; requestParams["Version"] = "2009-03-31"; requestParams["AssociateTag"] = myManager.IniData.AmazonAssociateTag; requestParams["Operation"] = "ItemSearch"; requestParams["Keywords"] = keywords; requestParams["ItemPage"] = itemPage.ToString(); requestParams["SearchIndex"] = type.ToString(); requestParams["ResponseGroup"] = "ItemAttributes,Images"; string requestUrl = myHelper.Sign(requestParams); return Request(requestUrl); }
// プロテクトメソッド protected XmlElement GetSearchForm(string query, AmazonIndexType selectedType){ XmlElement result = Html.Form(); XmlElement fs = Html.Fieldset("Amazon検索"); XmlElement p1 = Html.P(); p1.AppendChild(GetSelect(selectedType)); fs.AppendChild(p1); XmlElement p2 = Html.P(); p2.AppendChild(Html.Input(AmazonSearch.QueryName, query, "キーワード")); fs.AppendChild(p2); XmlElement submitP = Html.P("submit", Html.Submit("検索")); fs.AppendChild(submitP); result.AppendChild(fs); return result; }
// キーワードと検索対象を指定して、ItemSearchの結果を含むXmlDocumentを取得します。 public XmlDocument GetItemSearchXml(string keywords, AmazonIndexType type){ return GetItemSearchXml(keywords, type, 1); }
// Web サービスにリクエストを発行して検索結果を取得します。 private AmazonItemList Search(AmazonIndexType index, string q, int pageNum){ XmlDocument result = Aws.GetItemSearchXml(q, index, pageNum); if(result == null){ throw new Exception("XMLが取得できませんでした。"); } return AmazonItemList.Parse(result); }