K5 Playgroundで複数のサービスから画像を検索して表示するポータルサイトを作ります。Google検索APIやFlickrのAPIを使う方法を学ぶことができます。
概要
材料
- テンプレート: Form Search
- APIロジック: Flickr Get Popular Images, Google Custom Search
機能
- FlickrとGoogleで画像を検索
- 検索した画像の表示
アプリの作成
K5 Playgroundにアクセスします。
Frontend
- K5 Playgroundにアクセスして、
Form Searchテンプレートを選択します。
Backend
左メニューのWebAPIsを選択します。
image/flickrという名前のWebAPIを新規作成して、GETを有効化します。image/googleという名前のWebAPIを新規作成してGETを有効化します。
左メニューの
GET image/flickrエンドポイントを選択します。SNSメニューから
Flickr Search imagesAPIロジックを追加して、次のように編集します。1
2
3
4
5var options = {
text: req.query.keyword || 'image',
per_page: req.query.limit || 100,
page: req.query.offset || 0,
};1
2
3
4
5
6
7photoList.push({
_id: value.id,
name: value.title,
image_url: 'https://farm'+value.farm+'.staticflickr.com/'+value.server+'/'+value.id+'_'+value.secret+'.jpg',
type: 'flickr',
category: '',
});1
2
3
4next({
images: photoList,
count: photoList.length < options.per_page ? ((options.page + 1) * photoList.per_page + photoList.length) : 100;,
});
GET image/googleエンドポイントを選択します。右側のSearchメニューから
Google Custom SearchAPIロジックを追加して次のように編集します。1
2
3
4
5
6
7
8qs: {
key: apiKey,
cx: searchEngineId,
q: req.query.keyword || 'image',
start: req.query.offset + 1,
num: req.query.limit || 100,
searchType:'image',
},1
2
3
4
5
6
7
8
9
10
11
12next({
images: body.items.map(function(value) {
return {
_id: value.link,
name: value.title,
image_url: value.link,
type: 'google',
category: '',
};
}),
count: body.searchInformation.totalResults
});
アプリをダウンロードします。
アプリのカスタマイズ
Frontend
actions/ProductActionCreators.jsを次のように編集します。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22const ProductActionCreators = {
getProducts({ offset = 0, limit = 9, keyword, types }) {
var url = types === 'flickr' ? '/image/flickr' : '/image/google';
const getProducts = api.get(url, {
params: {
offset,
limit,
keyword,
},
});
axios.all([getProducts])
.then(axios.spread((productList) => {
AppDispatcher.dispatch({
type: ActionTypes.GET_PRODUCTS,
data: {
products: productList.data.images,
count: productList.data.count,
},
});
})).catch(() => {});
},components/FormSearchBox/FormSearchBox.jsを次のように編集します。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19const radioButtonOptions = (
<RadioButtonGroup
name="types"
valueSelected={type}
style={styles.radioButtonOptions}
onChange={(event, value) => this.handleTypeChange(value)}
>
<RadioButton
value="flickr"
label="Flickr"
style={styles.radioButton}
/>
<RadioButton
value="google"
label="Google"
style={styles.radioButton}
/>
</RadioButtonGroup>
);components/containers/ProductListContainers.jsを次のように修正します。1
types: filter.type,
stores/FilterStore.jsを次のように編集します。1
2
3
4
5getInitialState() {
return {
data: Immutable.fromJS({
keyword: '',
type: 'flickr',
Backend
config.jsに接続情報を記入します。
アプリのデプロイ
Backend
1 | cd backend |
Frontend
1 | cd frontend |
ダウンロード
Version
- Form Search Template: v1.0.0