Debian Codegraph @

podcasts-data/src/discovery/itunes.rs

raw symbols SCIP
1 // itunes.rs
2 //
3 // Copyright 2022-2024 nee <[email protected]>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // SPDX-License-Identifier: GPL-3.0-or-later
19
20 use crate::discovery::data::*;
21 use anyhow::Result;
22 use chrono::prelude::*;
23 use serde::Deserialize;
24 use url::Url;
25
26 // curl "https://itunes.apple.com/search?term=chapo&entity=podcast&limit=10"
27 #[derive(Deserialize)]
28 struct SearchResult {
29 results: Vec<Podcast>,
30 }
31
32 #[derive(Deserialize)]
33 struct Podcast {
34 #[serde(rename = "feedUrl")]
35 feed_url: UrlString,
36 #[serde(rename = "collectionName")]
37 collection_name: String, // Name of the podcast
38 #[serde(rename = "artistName")]
39 artist_name: String,
40 #[serde(rename = "releaseDate")]
41 release_date: Option<DateTime<Local>>,
42 #[serde(rename = "trackCount")]
43 track_count: i32,
44 #[serde(rename = "artworkUrl100")]
45 artwork_url_100: UrlString,
46 }
47
48 impl From<Podcast> for FoundPodcast {
49 fn from(p: Podcast) -> FoundPodcast {
50 FoundPodcast {
51 feed: p.feed_url,
52 title: p.collection_name,
53 author: p.artist_name,
54 description: "".to_string(),
55 art: p.artwork_url_100,
56 episode_count: Some(p.track_count),
57 last_publication: p.release_date,
58 }
59 }
60 }
61
62 pub async fn search(query: &str, enabled: bool) -> Result<Vec<FoundPodcast>> {
63 if !enabled {
64 return Ok(vec![]);
65 }
66 let url = Url::parse_with_params(
67 "https://itunes.apple.com/search?entity=podcast&limit=10",
68 &[("term", query)],
69 )?;
70 let client = crate::downloader::client_builder().build()?;
71 let result: SearchResult = client.get(url).send().await?.json().await?;
72 Ok(result.results.into_iter().map(|p| p.into()).collect())
73 }
74
75 #[cfg(test)]
76 mod tests {
77 use super::*;
78
79 #[test]
80 fn test_1_result() -> Result<()> {
81 let input = std::fs::read_to_string("tests/itunes/search_1.txt")?;
82 let result: SearchResult = serde_json::from_str(&input)?;
83 let found: Vec<FoundPodcast> = result.results.into_iter().map(|p| p.into()).collect();
84 let expected: Vec<FoundPodcast> = vec![
85 FoundPodcast {
86 feed: "https://anchor.fm/s/19346b24/podcast/rss".to_string(),
87 title: "CushVlogs Audio - Matt Christman - Chapo Trap House".to_string(),
88 author: "Jackson Jacker".to_string(),
89 description: "".to_string(),
90 art: "https://is1-ssl.mzstatic.com/image/thumb/Podcasts124/v4/74/41/7c/74417c8a-151f-5090-5460-fe5e7ca0e671/mza_7528059777000521713.jpg/100x100bb.jpg".to_string(),
91 episode_count: Some(167),
92 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2021-06-08T06:56:00+02:00").unwrap().with_timezone(&chrono::Local))
93 }
94 ];
95 assert_eq!(1, found.len());
96 assert_eq!(expected, found);
97 Ok(())
98 }
99
100 #[test]
101 fn test_10_result() -> Result<()> {
102 let input = std::fs::read_to_string("tests/itunes/search_10.txt")?;
103 let result: SearchResult = serde_json::from_str(&input)?;
104 let found: Vec<FoundPodcast> = result.results.into_iter().map(|p| p.into()).collect();
105 let expected: Vec<FoundPodcast> = vec![
106 FoundPodcast {
107 feed: "https://feeds.acast.com/public/shows/7540dfb3-3c5f-43ae-91d3-aad22b2ede46".to_string(),
108 title: "Chapo".to_string(),
109 author: "VICE".to_string(),
110 description: "".to_string(),
111 art: "https://is1-ssl.mzstatic.com/image/thumb/Podcasts116/v4/58/9b/8d/589b8d72-13aa-55cb-463f-ad41096ca9ea/mza_560982554884614556.jpg/100x100bb.jpg".to_string(),
112 episode_count: Some(17),
113 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2023-01-25T06:01:00+01:00").unwrap().with_timezone(&chrono::Local))
114 }, FoundPodcast {
115 feed: "https://feeds.soundcloud.com/users/soundcloud:users:211911700/sounds.rss".to_string(),
116 title: "Chapo Trap House".to_string(),
117 author: "Chapo Trap House".to_string(),
118 description: "".to_string(),
119 art: "https://is1-ssl.mzstatic.com/image/thumb/Podcasts18/v4/37/d9/a0/37d9a0b4-64f8-70d4-722e-b3a8772f3424/mza_5335192259855381405.jpg/100x100bb.jpg".to_string(),
120 episode_count: Some(507),
121 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2024-03-05T21:55:00+01:00").unwrap().with_timezone(&chrono::Local))
122 }, FoundPodcast {
123 feed: "https://feeds.megaphone.fm/WMHY2717952910".to_string(),
124 title: "El Chapo: Dos rostros de un capo Podcast".to_string(),
125 author: "CNN en Español".to_string(),
126 description: "".to_string(),
127 art: "https://is1-ssl.mzstatic.com/image/thumb/Podcasts122/v4/31/e9/bb/31e9bb57-31f8-447d-e619-bf94e2ee42af/mza_7685287012963567517.jpg/100x100bb.jpg".to_string(),
128 episode_count: Some(7),
129 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2020-07-15T11:10:00+02:00").unwrap().with_timezone(&chrono::Local))
130 }, FoundPodcast {
131 feed: "https://anchor.fm/s/19346b24/podcast/rss".to_string(),
132 title: "CushVlogs Audio - Matt Christman - Chapo Trap House".to_string(),
133 author: "Jackson Jacker".to_string(),
134 description: "".to_string(),
135 art: "https://is1-ssl.mzstatic.com/image/thumb/Podcasts124/v4/74/41/7c/74417c8a-151f-5090-5460-fe5e7ca0e671/mza_7528059777000521713.jpg/100x100bb.jpg".to_string(),
136 episode_count: Some(167),
137 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2021-06-08T06:56:00+02:00").unwrap().with_timezone(&chrono::Local))
138 }, FoundPodcast {
139 feed: "https://audioboom.com/channels/4905580.rss".to_string(),
140 title: "‘El Chapo’: ¿héroe o villano?".to_string(),
141 author: "Univision".to_string(),
142 description: "".to_string(),
143 art: "https://is1-ssl.mzstatic.com/image/thumb/Podcasts116/v4/57/34/8e/57348e38-6c9a-7579-8269-42e61786ae3b/mza_17242328811639650011.jpg/100x100bb.jpg".to_string(),
144 episode_count: Some(4),
145 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2017-05-23T02:59:00+02:00").unwrap().with_timezone(&chrono::Local))
146 }, FoundPodcast {
147 feed: "https://anchor.fm/s/5b423b40/podcast/rss".to_string(),
148 title: "Chapo".to_string(),
149 author: "Jack Gold".to_string(),
150 description: "".to_string(),
151 art: "https://is1-ssl.mzstatic.com/image/thumb/Podcasts125/v4/43/67/d3/4367d309-707e-6b7e-390a-202565c1d530/mza_9950196886709505935.jpg/100x100bb.jpg".to_string(),
152 episode_count: Some(1),
153 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2021-05-15T07:32:00+02:00").unwrap().with_timezone(&chrono::Local))
154 }, FoundPodcast {
155 feed: "https://anchor.fm/s/1e56647c/podcast/rss".to_string(),
156 title: "Chapo".to_string(),
157 author: "Mijo 713".to_string(),
158 description: "".to_string(),
159 art: "https://is1-ssl.mzstatic.com/image/thumb/Podcasts123/v4/b5/7c/41/b57c410f-8b2f-d9ce-df76-44b5947ea261/mza_17707437198336862.jpg/100x100bb.jpg".to_string(),
160 episode_count: Some(1),
161 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2020-04-25T21:46:00+02:00").unwrap().with_timezone(&chrono::Local))
162 }, FoundPodcast {
163 feed: "https://www.omnycontent.com/d/playlist/e73c998e-6e60-432f-8610-ae210140c5b1/a6b57093-81fe-4401-a963-af2000fe4e3a/56b7926c-6bfd-4d8c-81f9-af2000ffad49/podcast.rss".to_string(),
164 title: "Surviving El Chapo: The Twins Who Brought Down A Drug Lord".to_string(),
165 author: "iHeartPodcasts".to_string(),
166 description: "".to_string(),
167 art: "https://is1-ssl.mzstatic.com/image/thumb/Podcasts116/v4/aa/90/0f/aa900f15-7a67-e498-7b8c-a8efc9a667b5/mza_18039635476170983620.jpg/100x100bb.jpg".to_string(),
168 episode_count: Some(26),
169 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2023-12-06T09:00:00+01:00").unwrap().with_timezone(&chrono::Local))
170 }, FoundPodcast {
171 feed: "https://anchor.fm/s/3a15ad8/podcast/rss".to_string(),
172 title: "CHAPOS Corner".to_string(),
173 author: "Chapo".to_string(),
174 description: "".to_string(),
175 art: "https://is1-ssl.mzstatic.com/image/thumb/Podcasts125/v4/a5/08/da/a508da5c-09c7-bc4d-3c2d-938bf406b36b/mza_14501171467539691063.jpg/100x100bb.jpg".to_string(),
176 episode_count: Some(704),
177 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2022-10-19T02:24:00+02:00").unwrap().with_timezone(&chrono::Local))
178 }, FoundPodcast {
179 feed: "https://anchor.fm/s/5d83aab0/podcast/rss".to_string(),
180 title: "EL CHAPO".to_string(),
181 author: "Bernardo".to_string(),
182 description: "".to_string(),
183 art: "https://is1-ssl.mzstatic.com/image/thumb/Podcasts115/v4/df/3b/2c/df3b2cbc-a8ac-315b-d5db-56d055690dfc/mza_8387652467730745876.jpg/100x100bb.jpg".to_string(),
184 episode_count: Some(10),
185 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2021-06-22T23:50:00+02:00").unwrap().with_timezone(&chrono::Local))
186 }
187 ];
188 assert_eq!(10, found.len());
189 assert_eq!(expected, found);
190 Ok(())
191 }
192
193 #[test]
194 fn empty_result() -> Result<()> {
195 let input = std::fs::read_to_string("tests/itunes/search_empty.txt")?;
196 let result: SearchResult = serde_json::from_str(&input)?;
197 let found: Vec<FoundPodcast> = result.results.into_iter().map(|p| p.into()).collect();
198 let expected: Vec<FoundPodcast> = vec![];
199 assert_eq!(expected, found);
200 Ok(())
201 }
202
203 #[test]
204 fn unicode_result() -> Result<()> {
205 let input = std::fs::read_to_string("tests/itunes/search_unicode.txt")?;
206 let result: SearchResult = serde_json::from_str(&input)?;
207 let found: Vec<FoundPodcast> = result.results.into_iter().map(|p| p.into()).collect();
208 let expected: Vec<FoundPodcast> = vec![FoundPodcast {
209 feed: "https://feeds.fireside.fm/cornerspaeti/rss".to_string(),
210 title: "Corner Späti".to_string(),
211 author: "The Späti Boys".to_string(),
212 description: "".to_string(),
213 art: "https://img-1.fyyd.de/pd/small/77182e877ac679f9414148fab3dddb74782c2.jpg"
214 .to_string(),
215 episode_count: Some(348),
216 last_publication: Some(
217 chrono::DateTime::parse_from_rfc3339("2024-03-07T10:00:00+01:00")
218 .unwrap()
219 .with_timezone(&chrono::Local),
220 ),
221 }];
222 assert_eq!(expected, found);
223 Ok(())
224 }
225 }