search/
search.rs

1use std::env;
2
3use yt_api::{
4	search::{Error, ItemType, SearchList},
5	ApiKey,
6};
7
8/// prints the first answer of a search query
9fn main() -> Result<(), Error> {
10	futures::executor::block_on(async {
11		// take api key from enviroment variable
12		let key = ApiKey::new(&env::var("YT_API_KEY").expect("YT_API_KEY env-var not found"));
13
14		// create the SearchList struct for the query "rust lang"
15		let result = SearchList::new(key)
16			.q("rust lang")
17			.item_type(ItemType::Video)
18			.await?;
19
20		// outputs the title of the first search result
21		println!(
22			"Title: \"{}\"",
23			result.items[0].snippet.title.as_ref().unwrap()
24		);
25		// outputs the video id of the first search result
26		println!(
27			"https://youtube.com/watch?v={}",
28			result.items[0].id.video_id.as_ref().unwrap()
29		);
30
31		println!(
32			"Default thumbnail: {}",
33			result.items[0]
34				.snippet
35				.thumbnails
36				.as_ref()
37				.unwrap()
38				.default
39				.as_ref()
40				.unwrap()
41				.url
42		);
43
44		Ok(())
45	})
46}