Skip to content

evex-dev/cookpad-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cookpad-c

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-curl
cd c/
make

libcookpad.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 をつける。

API

cookpad_client_new() / cookpad_client_free()

クライアント作成・破棄。引数は全部オプショナル。デフォルトで 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_recipes(client, query, page, per_page, &out)

レシピ検索。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_get_recipe(client, recipe_id, &out)

レシピ詳細を取得。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_get_similar_recipes(client, recipe_id, page, per_page, &out, &count)

似てるレシピ一覧。

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_get_comments(client, recipe_id, limit, after, label, &out)

つくれぽ・コメント取得。

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_search_users(client, query, page, per_page, &out)

ユーザー検索。

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);

cookpad_search_keywords(client, query, &out_json)

検索サジェスト。cJSON * を返すので cJSON_Delete() で解放する。

cJSON *result = NULL;
cookpad_search_keywords(client, "カレ", &result);
cJSON_Delete(result);

cookpad_get_search_history(client, local_history_json, &out_json)

検索履歴・トレンドキーワード。

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 (パブリックドメイン)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages