ItemSearchのサンプルコード(VB.NET) †
ここで紹介しているコードはこちらのプロキシクラスを使用しています。
ここで紹介しているコードはこちらのC#のコードをVB.NETに変換したものです。
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
| | <%@ Page Language="VB" %>
<%@ 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 Sub Button1_Click(ByVal sender As Object, _
ByVal e As EventArgs)
Dim req As New ItemSearchRequest()
req.SearchIndex = searchIndex.SelectedValue
req.Keywords = TextBox1.Text
req.ResponseGroup = New String() _
{"Small", "ItemAttributes", "OfferFull", _
"Images", "Reviews", "EditorialReview", _
"ListmaniaLists", "Similarities", "Tracks"}
req.ItemPage = "1"
If req.SearchIndex <> "Blended" AndAlso _
req.SearchIndex <> "MusicTracks" Then
req.Sort = "salesrank"
End If If TextBox2.Text <> "" Then
req.BrowseNode = TextBox2.Text
End If If req.SearchIndex <> "Blended" AndAlso _
req.SearchIndex <> "MusicTracks" Then
req.Condition = Condition.All
req.ConditionSpecified = True
req.MerchantId = "All"
req.Availability = ItemSearchRequestAvailability.Available
req.AvailabilitySpecified = True
End If
Dim isc As New ItemSearch()
isc.AWSAccessKeyId = "(あなたのAccessKeyID)"
isc.AssociateTag = "dobonnet-22"
isc.Request = New ItemSearchRequest() {req}
Dim aws As New AWSECommerceService()
Dim res As ItemSearchResponse
Try
res = aws.ItemSearch(isc)
Catch ex As Exception
Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message)
Return
End Try
If res Is Nothing OrElse res.Items Is Nothing Then
Label1.Text = "結果を取得できませんでした。"
Return
End If
If Not (res.Items(0).Request.Errors Is Nothing) Then
Label1.Text = "エラー:" + _
res.Items(0).Request.Errors(0).Message
Return
End If
If res.Items(0).Item Is Nothing Then
Label1.Text = "該当する商品が見つかりませんでした。"
Return
End If
Label1.Text = ""
Label1.Text += res.Items(0).TotalResults + _
" 件見つかりました。<br />" + vbLf
Label1.Text += "<ul>" + vbLf
Dim i As Item
For Each i In res.Items(0).Item
Label1.Text += "<li><b><a href=""" + _
i.DetailPageURL + """>" + i.ItemAttributes.Title + _
"</a></b><br />" + vbLf
Label1.Text += "ASIN : " + i.ASIN + "<br />" + vbLf
If Not (i.MediumImage Is Nothing) Then
Label1.Text += "<img src=""" + i.MediumImage.URL + _
""" width=""" + _
i.MediumImage.Width.Value.ToString() + _
""" height=""" + _
i.MediumImage.Height.Value.ToString() + _
"""><br />" + vbLf
End If
If Not (i.ItemAttributes.Creator Is Nothing) Then
Dim c As ItemAttributesCreator
For Each c In i.ItemAttributes.Creator
Label1.Text += c.Role + " : " + _
c.Value + "<br />" + vbLf
Next c
End If
If Not (i.ItemAttributes.Publisher Is Nothing) Then
Label1.Text += i.ItemAttributes.Publisher + _
"<br />" + vbLf
End If
If Not (i.ItemAttributes.ListPrice Is Nothing) Then
Label1.Text += "参考価格 : " + _
i.ItemAttributes.ListPrice.FormattedPrice + _
"<br />" + vbLf
End If
If Not (i.Offers Is Nothing) AndAlso _
Not (i.Offers.Offer Is Nothing) Then
Label1.Text += "価格 : <br />" + vbLf
Label1.Text += "<ul>" + vbLf
Dim off As Offer
For Each off In i.Offers.Offer
If Not ([off].OfferListing Is Nothing) Then
Label1.Text += "<ul>" + vbLf
Dim ol As OfferListing
For Each ol In [off].OfferListing
Label1.Text += "<li>" + _
ol.Price.FormattedPrice + vbLf
Next ol
Label1.Text += "</ul>" + vbLf
End If
Next off
Label1.Text += "</ul>" + vbLf
End If
If Not (i.OfferSummary Is Nothing) AndAlso _
Not (i.OfferSummary.LowestNewPrice Is Nothing) AndAlso _
Not (i.OfferSummary.LowestNewPrice.FormattedPrice _
Is Nothing) Then
Label1.Text += "新品の最安値 : " + _
i.OfferSummary.LowestNewPrice.FormattedPrice + _
"<br />" + vbLf
End If
If Not (i.EditorialReviews Is Nothing) Then
Label1.Text += "エディターレビュー : <br />"
Label1.Text += "<ul>" + vbLf
Dim r As EditorialReview
For Each r In i.EditorialReviews
Label1.Text += "<li>" + r.Source + " : " + _
r.Content + vbLf
Next r
Label1.Text += "</ul>" + vbLf
End If
If Not (i.CustomerReviews Is Nothing) Then
Label1.Text += "お勧め度 : " + _
i.CustomerReviews.AverageRating.ToString() + _
"<br />"
Label1.Text += "カスタマーレビュー : <br />"
Label1.Text += "<ul>" + vbLf
Dim r As Review
For Each r In i.CustomerReviews.Review
Label1.Text += "<li>" + r.Summary + vbLf
Next r
Label1.Text += "</ul>" + vbLf
End If
If Not (i.ListmaniaLists Is Nothing) Then
Label1.Text += "リストマニア : <br />"
Label1.Text += "<ul>" + vbLf
Dim lm As ListmaniaListsListmaniaList
For Each lm In i.ListmaniaLists
Label1.Text += "<li>" + lm.ListName + vbLf
Next lm
Label1.Text += "</ul>" + vbLf
End If
If Not (i.SimilarProducts Is Nothing) Then
Label1.Text += _
"この商品を買った人はこんな商品も買っています" + _
" : <br />"
Label1.Text += "<ul>" + vbLf
Dim sp As SimilarProductsSimilarProduct
For Each sp In i.SimilarProducts
Label1.Text += "<li>" + sp.Title + vbLf
Next sp
Label1.Text += "</ul>" + vbLf
End If
If Not (i.Tracks Is Nothing) Then
Label1.Text += "曲目リスト : <br />"
Label1.Text += "<ul>" + vbLf
Dim td As TracksDisc
For Each td In i.Tracks
Label1.Text += "<li>" + td.Number + vbLf
If Not (td.Track Is Nothing) Then
Label1.Text += "<ul>" + vbLf
Dim tdt As TracksDiscTrack
For Each tdt In td.Track
Label1.Text += "<li>" + tdt.Number + ":" + _
tdt.Value + vbLf
Next tdt
Label1.Text += "</ul>" + vbLf
End If
Next td
Label1.Text += "</ul>" + vbLf
End If
Next i
Label1.Text += "</ul>" + vbLf
End Sub
</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>
|