ItemSearchのサンプルコード(C#)

Amazon.co.jpで商品を検索するサンプルです。TextBoxに検索するキーワードを入れて、DropDownListで検索するカテゴリを指定できます。さらに、BrowseNodeを入力することもできるようになっています。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<%@ Page Language="C#" %>
<%@ Import Namespace="Amazon.jp.v20060913" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        ItemSearchRequest req = new ItemSearchRequest();
        //検索するストアのインデックスを指定する(必須)
        req.SearchIndex = searchIndex.SelectedValue;
        //検索する語句を指定
        req.Keywords = TextBox1.Text;
        //取得するデータの種類(範囲)を指定 
        req.ResponseGroup = new string[] {
            "Small","ItemAttributes", "OfferFull",
            "Images", "Reviews", "EditorialReview",
            "ListmaniaLists", "Similarities", "Tracks"};
        //ページを指定する。1-400まで。1ページ10の結果まで取得。
        req.ItemPage = "1";
        //並び替え方
        //SearchIndexや地域によって異なる
        if (req.SearchIndex != "Blended" &&
            req.SearchIndex != "MusicTracks")
            req.Sort = "salesrank";
        //BrowseNode
        if (TextBox2.Text != "")
            req.BrowseNode = TextBox2.Text;
        //新品以外や、Amazon以外の売り手の商品も表示
        if (req.SearchIndex != "Blended" &&
            req.SearchIndex != "MusicTracks")
        {
            req.Condition = Condition.All;
            req.ConditionSpecified = true;
            req.MerchantId = "All";
            //利用可能な商品のみ表示
            req.Availability = ItemSearchRequestAvailability.Available;
            req.AvailabilitySpecified = true;
        }
 
        ItemSearch isc = new ItemSearch();
        //あなたのAccess Key ID
        //AWSAccessKeyIdかSubscriptionIdのどちらかを必ず指定する
        isc.AWSAccessKeyId = "(あなたのAccessKeyID)";
        //あなたのAssociate ID
        isc.AssociateTag = "dobonnet-22";
        isc.Request = new ItemSearchRequest[] { req };
 
        AWSECommerceService aws = new AWSECommerceService();
        //プロキシを通す場合は、次のようにする
        //aws.Proxy = new System.Net.WebProxy("localhost", 8080);
        ItemSearchResponse res;
        try
        {
            //ItemSearchオペレーションを実行し、結果を取得
            res = aws.ItemSearch(isc);
        }
        catch (Exception ex)
        {
            Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message);
            return;
        }
 
        if (res == null || res.Items == null)
        {
            Label1.Text = "結果を取得できませんでした。";
            return;
        }
        if (res.Items[0].Request.Errors != null)
        {
            Label1.Text = "エラー:" +
                res.Items[0].Request.Errors[0].Message;
            return;
        }
        if (res.Items[0].Item == null)
        {
            Label1.Text = "該当する商品が見つかりませんでした。";
            return;
        }
 
        //結果を表示
        Label1.Text = "";
        Label1.Text += res.Items[0].TotalResults +
            " 件見つかりました。<br />\n";
        Label1.Text += "<ul>\n";
        foreach (Item i in res.Items[0].Item)
        {
            //タイトル
            Label1.Text += "<li><b><a href=\"" + i.DetailPageURL + "\">" +
                i.ItemAttributes.Title + "</a></b><br />\n";
            Label1.Text += "ASIN : " + i.ASIN + "<br />\n";
 
            //画像を表示
            //ResponseGroupにImagesなどが必要
            if (i.MediumImage != null)
                Label1.Text += "<img src=\"" + i.MediumImage.URL +
                    "\" width=\"" + i.MediumImage.Width.Value.ToString() +
                    "\" height=\"" + i.MediumImage.Height.Value.ToString() +
                    "\"><br />\n";
 
            //クリエーター情報
            if (i.ItemAttributes.Creator != null)
            {
                foreach (ItemAttributesCreator c in i.ItemAttributes.Creator)
                {
                    Label1.Text += c.Role + " : " + c.Value + "<br />\n";
                }
            }
            //出版社
            if (i.ItemAttributes.Publisher != null)
                Label1.Text += i.ItemAttributes.Publisher + "<br />\n";
            
            //価格
            //ResponseGroupにItemAttributesなどが必要
            if (i.ItemAttributes.ListPrice != null)
                Label1.Text += "参考価格 : " +
                    i.ItemAttributes.ListPrice.FormattedPrice + "<br />\n";
            //ResponseGroupにOfferFullなどが必要
            //実際の価格の表示
            if (i.Offers != null && i.Offers.Offer != null)
            {
                Label1.Text += "価格 : <br />\n";
                Label1.Text += "<ul>\n";
                foreach (Offer off in i.Offers.Offer)
                {
                    if (off.OfferListing != null)
                    {
                        Label1.Text += "<ul>\n";
                        foreach (OfferListing ol in off.OfferListing)
                        {
                            Label1.Text +=
                                "<li>" + ol.Price.FormattedPrice + "\n";
                        }
                        Label1.Text += "</ul>\n";
                    }
                }
                Label1.Text += "</ul>\n";
            }
            
            //最安値
            if (i.OfferSummary != null &&
                i.OfferSummary.LowestNewPrice != null &&
                i.OfferSummary.LowestNewPrice.FormattedPrice != null)
                Label1.Text += "新品の最安値 : " +
                    i.OfferSummary.LowestNewPrice.FormattedPrice +
                    "<br />\n";
            
            //EditorialReview
            if (i.EditorialReviews != null)
            {
                Label1.Text += "エディターレビュー : <br />";
                Label1.Text += "<ul>\n";
                foreach (EditorialReview r in i.EditorialReviews)
                {
                    Label1.Text += "<li>" + r.Source + " : "
                        + r.Content + "\n";
                }
                Label1.Text += "</ul>\n";
            }
            
            //カスタマーレビュー
            if (i.CustomerReviews != null)
            {
                Label1.Text += "お勧め度 : " +
                    i.CustomerReviews.AverageRating.ToString() +
                    "<br />";
                Label1.Text += "カスタマーレビュー : <br />";
                Label1.Text += "<ul>\n";
                foreach (Review r in i.CustomerReviews.Review)
                {
                    Label1.Text += "<li>" + r.Summary + "\n";
                }
                Label1.Text += "</ul>\n";
            }
            
            //リストマニア
            if (i.ListmaniaLists != null)
            {
                Label1.Text += "リストマニア : <br />";
                Label1.Text += "<ul>\n";
                foreach (ListmaniaListsListmaniaList lm in i.ListmaniaLists)
                {
                    Label1.Text += "<li>" + lm.ListName + "\n";
                }
                Label1.Text += "</ul>\n";
            }
            
            //この商品を買った人はこんな商品も買っています
            if (i.SimilarProducts != null)
            {
                Label1.Text += "この商品を買った人はこんな商品も買っています" +
                    " : <br />";
                Label1.Text += "<ul>\n";
                foreach (SimilarProductsSimilarProduct sp in i.SimilarProducts)
                {
                    Label1.Text += "<li>" + sp.Title + "\n";
                }
                Label1.Text += "</ul>\n";
            }
            
            //曲目リスト
            if (i.Tracks != null)
            {
                Label1.Text += "曲目リスト : <br />";
                Label1.Text += "<ul>\n";
                foreach (TracksDisc td in i.Tracks)
                {
                    Label1.Text += "<li>" + td.Number + "\n";
                    if (td.Track != null)
                    {
                        Label1.Text += "<ul>\n";
                        foreach (TracksDiscTrack tdt in td.Track)
                            Label1.Text +=
                                "<li>" + tdt.Number + ":" + tdt.Value + "\n";
                        Label1.Text += "</ul>\n";
                    }
                }
                Label1.Text += "</ul>\n";
            }
        }
        Label1.Text += "</ul>\n";
    }
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Amazon商品検索</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
        Text="検索" EnableViewState="False" /><br />
        <asp:DropDownList ID="searchIndex" runat="server">
            <asp:ListItem Value="Blended">すべて</asp:ListItem>
            <asp:ListItem Value="Books">本</asp:ListItem>
            <asp:ListItem Value="Classical">クラシック</asp:ListItem>
            <asp:ListItem>DVD</asp:ListItem>
            <asp:ListItem Value="Electronics">エレクトロニクス</asp:ListItem>
            <asp:ListItem Value="ForeignBooks">洋書</asp:ListItem>
            <asp:ListItem Value="Hobbies">ホビー</asp:ListItem>
            <asp:ListItem Value="Kitchen">ホーム&キッチン</asp:ListItem>
            <asp:ListItem Value="Music">音楽</asp:ListItem>
            <asp:ListItem Value="MusicTracks">曲名</asp:ListItem>
            <asp:ListItem Value="Software">ソフトウェア</asp:ListItem>
            <asp:ListItem Value="SportingGoods">スポーツ</asp:ListItem>
            <asp:ListItem Value="Toys">おもちゃ</asp:ListItem>
            <asp:ListItem>VHS</asp:ListItem>
            <asp:ListItem Value="Video">DVD&ビデオ</asp:ListItem>
            <asp:ListItem Value="VideoGames">ゲーム</asp:ListItem>
        </asp:DropDownList><br />
        <label for="TextBox2">BrowseNode : </label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        </div>
    </form>
    <br />
    <asp:Label ID="Label1" runat="server" EnableViewState="False">
    </asp:Label>
</body>
</html>

ページ情報
  • 作成日 : 2006-10-20 (金) 00:21:32
  • 作成者 : DOBON!
  • 最終編集日 : 2006-10-20 (金) 00:21:32
  • 最終編集者 : DOBON!
[ トップ ]   [ 新規 | 子ページ作成 | 一覧 | 単語検索 | 最終更新 | ヘルプ ]