Cookpad の非公式 C クライアント。 cookpad-py を C にリライトした。
MSYS2 UCRT64 が必要。入ってなければ msys2.org から入れる。
pacman -S --noconfirm mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-make mingw-w64-ucrt-x86_64-curlcd c/
makelibcookpad.a が生成される。
一部の引数とかは、認証済みの token じゃないと動かないので注意
#include "cookpad.h"
cookpad_client_t *client = cookpad_client_new();
// レシピ検索
cookpad_search_response_t *resp = NULL;
cookpad_search_recipes(client, "カレー", 1, 30, &resp);
for (int i = 0; i < resp->count; i++)
printf("%s\n", resp->recipes[i]->title);
cookpad_search_response_free(resp);
// レシピ詳細
cookpad_recipe_t *recipe = NULL;
cookpad_get_recipe(client, 25410768, &recipe);
printf("%s\n", recipe->title);
cookpad_recipe_free(recipe);
cookpad_client_free(client);リンク時は -lcookpad -lcurl をつける。
クライアント作成・破棄。引数は全部オプショナル。デフォルトで anonymous token 使うからそのまま動く。
// デフォルト (日本語・匿名)
cookpad_client_t *c = cookpad_client_new();
// カスタム
cookpad_client_t *c = cookpad_client_new_ex(
NULL, // token (NULL でデフォルト)
"JP", // country
"ja", // language
NULL, // timezone_id
NULL, // timezone_offset
"custom/1.0",// user_agent
NULL // provider_id
);レシピ検索。cookpad_search_response_t を返す。
cookpad_search_response_t *resp = NULL;
cookpad_search_recipes(client, "鶏むね肉", 1, 30, &resp);
printf("全 %d 件\n", resp->total_count);
for (int i = 0; i < resp->count; i++)
printf(" %s\n", resp->recipes[i]->title);
// 次のページがあるか
if (resp->next_page > 0)
cookpad_search_recipes(client, "鶏むね肉", resp->next_page, 30, &resp);
cookpad_search_response_free(resp);レシピ詳細を取得。cookpad_recipe_t を返す。
cookpad_recipe_t *r = NULL;
cookpad_get_recipe(client, 25410768, &r);
printf("%s\n", r->title);
printf("%s\n", r->story);
for (int i = 0; i < r->ingredient_count; i++)
printf(" %s: %s\n", r->ingredients[i]->name, r->ingredients[i]->quantity);
for (int i = 0; i < r->step_count; i++)
printf(" %s\n", r->steps[i]->description);
cookpad_recipe_free(r);似てるレシピ一覧。
cookpad_recipe_t **similar = NULL;
int count = 0;
cookpad_get_similar_recipes(client, 25410768, 1, 30, &similar, &count);
for (int i = 0; i < count; i++) {
printf("%s\n", similar[i]->title);
cookpad_recipe_free(similar[i]);
}
free(similar);つくれぽ・コメント取得。
cookpad_comments_response_t *resp = NULL;
cookpad_get_comments(client, 18510866, 10, "", "cooksnap", &resp);
for (int i = 0; i < resp->count; i++)
printf("%s: %s\n", resp->comments[i]->user->name, resp->comments[i]->body);
// ページネーション (カーソルベース)
if (resp->next_cursor)
cookpad_get_comments(client, 18510866, 10, resp->next_cursor, "cooksnap", &resp);
cookpad_comments_response_free(resp);ユーザー検索。
cookpad_users_response_t *resp = NULL;
cookpad_search_users(client, "test", 1, 20, &resp);
for (int i = 0; i < resp->count; i++)
printf("%s (レシピ数: %d)\n", resp->users[i]->name, resp->users[i]->recipe_count);
cookpad_users_response_free(resp);検索サジェスト。cJSON * を返すので cJSON_Delete() で解放する。
cJSON *result = NULL;
cookpad_search_keywords(client, "カレ", &result);
cJSON_Delete(result);検索履歴・トレンドキーワード。
cJSON *result = NULL;
cookpad_get_search_history(client, "[]", &result);
cJSON_Delete(result);レスポンスは全部 struct でパース済み。
cookpad_recipe_t- レシピ (id, title, story, serving, ingredients, steps, ...)cookpad_ingredient_t- 材料 (name, quantity)cookpad_step_t- 手順 (description, image_url)cookpad_user_t- ユーザー (id, name, recipe_count, ...)cookpad_comment_t- コメント/つくれぽ (body, user, image_url, ...)cookpad_image_t- 画像 (url, filename, alt_text)cookpad_search_response_t- 検索結果 (recipes, count, total_count, next_page)cookpad_comments_response_t- コメント一覧 (comments, count, next_cursor)cookpad_users_response_t- ユーザー一覧 (users, count, total_count, next_page)
cookpad_error_t err = cookpad_get_recipe(client, 99999999, &recipe);
switch (err) {
case COOKPAD_ERR_NOT_FOUND: printf("レシピが見つからない\n"); break;
case COOKPAD_ERR_RATE_LIMIT: printf("レート制限\n"); break;
case COOKPAD_OK: break;
default: printf("なんかエラー: %s\n", cookpad_error_str(err));
}The Unlicense (パブリックドメイン)