Cookpad の非公式 Rust クライアント。 cookpad-py を Rust にリライトした。
Cargo.toml に追加:
[dependencies]
cookpad = { git = "https://github.com/evex-dev/cookpad-rs" }一部の引数とかは、認証済みの token じゃないと動かないので注意
use cookpad::Cookpad;
#[tokio::main]
async fn main() -> Result<(), cookpad::CookpadError> {
let client = Cookpad::new();
// レシピ検索
let results = client.search_recipes("カレー").await?;
for recipe in &results.recipes {
println!("{} (つくれぽ: {})", recipe.title, recipe.cooksnaps_count);
}
// レシピ詳細
let recipe = client.get_recipe(25410768).await?;
println!("{}", recipe.title);
for step in &recipe.steps {
println!(" - {}", step.description);
}
Ok(())
}クライアント作成。デフォルトで anonymous token 使うからそのまま動く。
// デフォルト (日本語・匿名)
let client = Cookpad::new();
// カスタム
let client = Cookpad::with_config(
"your_token",
"JP",
"ja",
"Asia/Tokyo",
"+09:00",
"custom/1.0",
"8",
);レシピ検索。SearchResponse を返す。
let results = client.search_recipes("鶏むね肉").await?;
println!("全 {} 件", results.total_count);
for recipe in &results.recipes {
println!(" {}", recipe.title);
}
// 次のページ
if let Some(next) = results.next_page {
let page2 = client.search_recipes_with_options("鶏むね肉", next as u32, 30).await?;
}レシピ詳細を取得。Recipe を返す。
let recipe = client.get_recipe(25410768).await?;
println!("{}", recipe.title);
println!("{}", recipe.story);
println!("材料 ({}):", recipe.serving);
for ing in &recipe.ingredients {
println!(" {}: {}", ing.name, ing.quantity);
}
for step in &recipe.steps {
println!(" {}", step.description);
}似てるレシピ一覧。
let similar = client.get_similar_recipes(25410768, 1, 30).await?;
for recipe in &similar {
println!("{}", recipe.title);
}つくれぽ・コメント取得。
let comments = client.get_comments(18510866, 10, "", "cooksnap").await?;
for comment in &comments.comments {
if let Some(user) = &comment.user {
println!("{}: {}", user.name, comment.body);
}
}
// ページネーション (カーソルベース)
if let Some(cursor) = &comments.next_cursor {
let more = client.get_comments(18510866, 10, cursor, "cooksnap").await?;
}ユーザー検索。
let users = client.search_users("test", 1, 20).await?;
for user in &users.users {
println!("{} (レシピ数: {})", user.name, user.recipe_count);
}検索サジェスト。
let suggestions = client.search_keywords("カレ").await?;検索履歴・トレンドキーワード。
let history = client.get_search_history(&[]).await?;レスポンスは全部 struct でパース済み。serde::Serialize も実装してるので JSON に戻せる。
Recipe- レシピ (id, title, story, serving, ingredients, steps, ...)Ingredient- 材料 (name, quantity)Step- 手順 (description, image_url)User- ユーザー (id, name, recipe_count, ...)Comment- コメント/つくれぽ (body, user, image_url, ...)Image- 画像 (url, filename, alt_text)SearchResponse- 検索結果 (recipes, total_count, next_page)CommentsResponse- コメント一覧 (comments, next_cursor)UsersResponse- ユーザー一覧 (users, total_count, next_page)
use cookpad::CookpadError;
match client.get_recipe(99999999).await {
Ok(recipe) => println!("{}", recipe.title),
Err(CookpadError::NotFoundError(_)) => println!("レシピが見つからない"),
Err(CookpadError::RateLimitError) => println!("レート制限"),
Err(e) => println!("なんかエラー: {}", e),
}The Unlicense (パブリックドメイン)