mygpoclient 0.2.0

Client library for gpodder.net API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! [Directory API](https://gpoddernet.readthedocs.io/en/latest/api/reference/directory.html)

use crate::client::{AuthenticatedClient, DeviceClient, PublicClient};
use crate::error::Error;
use crate::subscription::Podcast;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use url::form_urlencoded::byte_serialize;
use url::Url;

/// Podcast tag
#[derive(Deserialize, Serialize, Debug, Clone, Eq)]
pub struct Tag {
    /// more reader-friendly representation of tag
    pub title: String,
    /// actual tag, unique identifier
    pub tag: String,
    /// number of podcasts using this tag
    pub usage: u16,
}

/// Podcast episode
#[derive(Deserialize, Serialize, Debug, Clone, Eq)]
pub struct Episode {
    /// title
    pub title: String,
    /// media url
    pub url: Url,
    /// podcast title
    pub podcast_title: String,
    /// podcast feed url
    pub podcast_url: Url,
    /// description
    pub description: String,
    /// website
    pub website: Option<Url>,
    /// gpodder internal link
    pub mygpo_link: Url,
    /// release date
    pub released: NaiveDateTime,
}

/// see [`retrieve_top_tags`](./trait.RetrieveTopTags.html#tymethod.retrieve_top_tags)
pub trait RetrieveTopTags {
    /// Retrieve Top Tags
    ///
    /// # Parameters
    ///
    /// - `count`: number of tags to return
    ///
    /// # Examples
    ///
    /// ```
    /// use mygpoclient::client::PublicClient;
    /// use mygpoclient::directory::RetrieveTopTags;
    ///
    /// let tags = PublicClient::default().retrieve_top_tags(10)?;
    /// assert_eq!(10, tags.len());
    ///
    /// # Ok::<(), mygpoclient::error::Error>(())
    /// ```
    ///
    /// # See also
    ///
    /// - [gpodder.net API Documentation](https://gpoddernet.readthedocs.io/en/latest/api/reference/directory.html#retrieve-top-tags)
    fn retrieve_top_tags(&self, count: u8) -> Result<Vec<Tag>, Error>;
}

/// see [`retrieve_podcasts_for_tag`](./trait.RetrievePodcastsForTag.html#tymethod.retrieve_podcasts_for_tag)
pub trait RetrievePodcastsForTag {
    /// Retrieve Podcasts for Tag
    ///
    /// # Parameters
    ///
    /// - `tag`: podcast tag
    /// - `count`: number of podcasts to return
    ///
    /// # Examples
    ///
    /// ```
    /// use mygpoclient::client::PublicClient;
    /// use mygpoclient::directory::RetrievePodcastsForTag;
    ///
    /// let max_results = 3;
    /// let podcasts = PublicClient::default().retrieve_podcasts_for_tag("new", max_results)?;
    /// assert!(podcasts.len() <= max_results as usize);
    ///
    /// # Ok::<(), mygpoclient::error::Error>(())
    /// ```
    ///
    /// # See also
    ///
    /// - [gpodder.net API Documentation](https://gpoddernet.readthedocs.io/en/latest/api/reference/directory.html#retrieve-podcasts-for-tag)
    fn retrieve_podcasts_for_tag(&self, tag: &str, count: u8) -> Result<Vec<Podcast>, Error>;
}

/// see [`retrieve_podcast_data`](./trait.RetrievePodcastData.html#tymethod.retrieve_podcast_data)
pub trait RetrievePodcastData {
    /// Returns information for the podcast with the given URL or Error if there is no podcast with this URL.
    ///
    /// # Parameters
    ///
    /// - `url`: podcast feed url
    ///
    /// # Examples
    ///
    /// ```
    /// use mygpoclient::client::PublicClient;
    /// use mygpoclient::directory::RetrievePodcastData;
    /// use url::Url;
    ///
    /// let url = Url::parse("http://feeds.feedburner.com/coverville").unwrap();
    /// let podcast = PublicClient::default().retrieve_podcast_data(url)?;
    ///
    /// # Ok::<(), mygpoclient::error::Error>(())
    /// ```
    ///
    /// # See also
    ///
    /// - [gpodder.net API Documentation](https://gpoddernet.readthedocs.io/en/latest/api/reference/directory.html#retrieve-podcast-data)
    fn retrieve_podcast_data(&self, url: Url) -> Result<Podcast, Error>;
}

/// see [`retrieve_episode_data`](./trait.RetrieveEpisodeData.html#tymethod.retrieve_episode_data)
pub trait RetrieveEpisodeData {
    /// Returns information for the episode with the given url that belongs to the given podcast
    ///
    /// # Parameters
    ///
    /// - `url`: media url of episode
    /// - `podcast`: podcast feed url
    ///
    /// # Examples
    ///
    /// ```
    /// use mygpoclient::client::PublicClient;
    /// use mygpoclient::directory::RetrieveEpisodeData;
    /// use url::Url;
    ///
    /// let url = Url::parse("https://www.podtrac.com/pts/redirect.mp3/audio.wnyc.org/otm/otm011520_podextra.mp3").unwrap();
    /// let podcast = Url::parse("http://feeds.wnyc.org/onthemedia?format=xml").unwrap();
    /// let episode = PublicClient::default().retrieve_episode_data(url, podcast)?;
    ///
    /// # Ok::<(), mygpoclient::error::Error>(())
    /// ```
    ///
    /// # See also
    ///
    /// - [gpodder.net API Documentation](https://gpoddernet.readthedocs.io/en/latest/api/reference/directory.html#retrieve-episode-data)
    fn retrieve_episode_data(&self, podcast: Url, url: Url) -> Result<Episode, Error>;
}

/// see [`podcast_toplist`](./trait.PodcastToplist.html#tymethod.podcast_toplist)
pub trait PodcastToplist {
    /// Returns list of top podcasts
    ///
    /// # Parameters
    ///
    /// - `number`: maximum number of podcasts to return
    /// - `scale_logo`: provides a link to a scaled logo for each podcast. Has to be a positive number up to 256 and defaults to 64.
    ///
    /// # Examples
    ///
    /// ```
    /// use mygpoclient::client::PublicClient;
    /// use mygpoclient::directory::PodcastToplist;
    ///
    /// let max_results = 10;
    /// let podcasts = PublicClient::default().podcast_toplist(max_results, None)?;
    /// assert_eq!(max_results as usize, podcasts.len());
    ///
    /// # Ok::<(), mygpoclient::error::Error>(())
    /// ```
    ///
    /// # See also
    ///
    /// - [gpodder.net API Documentation](https://gpoddernet.readthedocs.io/en/latest/api/reference/directory.html#podcast-toplist)
    fn podcast_toplist(&self, number: u8, scale_logo: Option<u16>) -> Result<Vec<Podcast>, Error>;
}

/// see [`podcast_search`](./trait.PodcastSearch.html#tymethod.podcast_search)
pub trait PodcastSearch {
    /// Carries out a service-wide search for podcasts that match the given query. Returns a list of podcasts.
    ///
    /// # Parameters
    ///
    /// - `q`: search query
    /// - `scale_logo`: provides a link to a scaled logo for each podcast. Has to be a positive number up to 256 and defaults to 64.
    ///
    /// # Examples
    ///
    /// ```
    /// use mygpoclient::client::PublicClient;
    /// use mygpoclient::directory::PodcastSearch;
    ///
    /// let podcasts = PublicClient::default().podcast_search("raumzeit", None)?;
    /// assert!(podcasts.len() > 0);
    ///
    /// # Ok::<(), mygpoclient::error::Error>(())
    /// ```
    ///
    /// # See also
    ///
    /// - [gpodder.net API Documentation](https://gpoddernet.readthedocs.io/en/latest/api/reference/directory.html#podcast-search)
    fn podcast_search(&self, q: &str, scale_logo: Option<u16>) -> Result<Vec<Podcast>, Error>;
}

impl RetrieveTopTags for PublicClient {
    fn retrieve_top_tags(&self, count: u8) -> Result<Vec<Tag>, Error> {
        Ok(self
            .get(&format!(
                "https://gpodder.net/api/2/tags/{}.json",
                count.to_string()
            ))?
            .json()?)
    }
}

impl RetrieveTopTags for AuthenticatedClient {
    fn retrieve_top_tags(&self, count: u8) -> Result<Vec<Tag>, Error> {
        self.public_client.retrieve_top_tags(count)
    }
}

impl RetrieveTopTags for DeviceClient {
    fn retrieve_top_tags(&self, count: u8) -> Result<Vec<Tag>, Error> {
        self.authenticated_client.retrieve_top_tags(count)
    }
}

impl RetrievePodcastsForTag for PublicClient {
    fn retrieve_podcasts_for_tag(&self, tag: &str, count: u8) -> Result<Vec<Podcast>, Error> {
        let tag_urlencoded: String = byte_serialize(tag.as_bytes()).collect();
        Ok(self
            .get(&format!(
                "https://gpodder.net/api/2/tag/{}/{}.json",
                tag_urlencoded,
                count.to_string()
            ))?
            .json()?)
    }
}

impl RetrievePodcastsForTag for AuthenticatedClient {
    fn retrieve_podcasts_for_tag(&self, tag: &str, count: u8) -> Result<Vec<Podcast>, Error> {
        self.public_client.retrieve_podcasts_for_tag(tag, count)
    }
}

impl RetrievePodcastsForTag for DeviceClient {
    fn retrieve_podcasts_for_tag(&self, tag: &str, count: u8) -> Result<Vec<Podcast>, Error> {
        self.authenticated_client
            .retrieve_podcasts_for_tag(tag, count)
    }
}

impl RetrievePodcastData for PublicClient {
    fn retrieve_podcast_data(&self, url: Url) -> Result<Podcast, Error> {
        Ok(self
            .get_with_query(
                "https://gpodder.net/api/2/data/podcast.json",
                &[&("url", url.as_str())],
            )?
            .json()?)
    }
}

impl RetrievePodcastData for AuthenticatedClient {
    fn retrieve_podcast_data(&self, url: Url) -> Result<Podcast, Error> {
        self.public_client.retrieve_podcast_data(url)
    }
}

impl RetrievePodcastData for DeviceClient {
    fn retrieve_podcast_data(&self, url: Url) -> Result<Podcast, Error> {
        self.authenticated_client.retrieve_podcast_data(url)
    }
}

impl RetrieveEpisodeData for PublicClient {
    fn retrieve_episode_data(&self, url: Url, podcast: Url) -> Result<Episode, Error> {
        Ok(self
            .get_with_query(
                "https://gpodder.net/api/2/data/episode.json",
                &[&("url", url.as_str()), &("podcast", podcast.as_str())],
            )?
            .json()?)
    }
}

impl RetrieveEpisodeData for AuthenticatedClient {
    fn retrieve_episode_data(&self, url: Url, podcast: Url) -> Result<Episode, Error> {
        self.public_client.retrieve_episode_data(url, podcast)
    }
}

impl RetrieveEpisodeData for DeviceClient {
    fn retrieve_episode_data(&self, url: Url, podcast: Url) -> Result<Episode, Error> {
        self.authenticated_client
            .retrieve_episode_data(url, podcast)
    }
}

impl PodcastToplist for PublicClient {
    fn podcast_toplist(&self, number: u8, scale_logo: Option<u16>) -> Result<Vec<Podcast>, Error> {
        let url = &format!("https://gpodder.net/toplist/{}.json", number);

        if let Some(size) = scale_logo {
            Ok(self
                .get_with_query(url, &[&("scale_logo", size.to_string())])?
                .json()?)
        } else {
            Ok(self.get(url)?.json()?)
        }
    }
}

impl PodcastToplist for AuthenticatedClient {
    fn podcast_toplist(&self, number: u8, scale_logo: Option<u16>) -> Result<Vec<Podcast>, Error> {
        self.public_client.podcast_toplist(number, scale_logo)
    }
}

impl PodcastToplist for DeviceClient {
    fn podcast_toplist(&self, number: u8, scale_logo: Option<u16>) -> Result<Vec<Podcast>, Error> {
        self.authenticated_client
            .podcast_toplist(number, scale_logo)
    }
}

impl PodcastSearch for PublicClient {
    fn podcast_search(&self, q: &str, scale_logo: Option<u16>) -> Result<Vec<Podcast>, Error> {
        let mut query_parameters: Vec<&(&str, &str)> = Vec::new();

        let query_parameter_since = ("q", q);
        query_parameters.push(&query_parameter_since);

        let scale_logo_string = match scale_logo {
            Some(size) => size.to_string(),
            None => String::new(),
        };
        let query_parameter_scale_logo: (&str, &str) = ("scale_logo", scale_logo_string.as_ref());

        if !scale_logo_string.is_empty() {
            query_parameters.push(&query_parameter_scale_logo);
        }

        Ok(self
            .get_with_query("https://gpodder.net/search.json", &query_parameters)?
            .json()?)
    }
}

impl PodcastSearch for AuthenticatedClient {
    fn podcast_search(&self, q: &str, scale_logo: Option<u16>) -> Result<Vec<Podcast>, Error> {
        self.public_client.podcast_search(q, scale_logo)
    }
}

impl PodcastSearch for DeviceClient {
    fn podcast_search(&self, q: &str, scale_logo: Option<u16>) -> Result<Vec<Podcast>, Error> {
        self.authenticated_client.podcast_search(q, scale_logo)
    }
}

impl PartialEq for Tag {
    fn eq(&self, other: &Self) -> bool {
        self.tag == other.tag
    }
}

impl Ord for Tag {
    fn cmp(&self, other: &Self) -> Ordering {
        self.tag.cmp(&other.tag)
    }
}

impl PartialOrd for Tag {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Hash for Tag {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.tag.hash(state);
    }
}

impl fmt::Display for Tag {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.tag, self.title)
    }
}

impl PartialEq for Episode {
    fn eq(&self, other: &Self) -> bool {
        self.url == other.url
    }
}

impl Ord for Episode {
    fn cmp(&self, other: &Self) -> Ordering {
        self.url.cmp(&other.url)
    }
}

impl PartialOrd for Episode {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Hash for Episode {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.url.hash(state);
    }
}

impl fmt::Display for Episode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.title, self.url)
    }
}

#[cfg(test)]
mod tests {
    use super::Episode;
    use super::Tag;
    use chrono::NaiveDate;
    use std::cmp::Ordering;
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};
    use url::Url;

    #[test]
    fn equal_tag_means_equal_hash() {
        let tag1 = Tag {
            title: String::from("TAG"),
            tag: String::from("tag"),
            usage: 0,
        };
        let tag2 = Tag {
            title: String::from("GAT"),
            tag: String::from("tag"),
            usage: 100,
        };

        assert_eq!(tag1, tag2);
        assert_eq!(tag1.partial_cmp(&tag2), Some(Ordering::Equal));

        let mut hasher1 = DefaultHasher::new();
        tag1.hash(&mut hasher1);

        let mut hasher2 = DefaultHasher::new();
        tag2.hash(&mut hasher2);

        assert_eq!(hasher1.finish(), hasher2.finish());
    }

    #[test]
    fn not_equal_tags_have_non_equal_ordering() {
        let tag1 = Tag {
            title: String::from("TAG"),
            tag: String::from("abc"),
            usage: 0,
        };
        let tag2 = Tag {
            title: String::from("TAG"),
            tag: String::from("xyz"),
            usage: 0,
        };

        assert_ne!(tag1, tag2);
        assert_eq!(tag1.partial_cmp(&tag2), Some(Ordering::Less));

        let mut hasher1 = DefaultHasher::new();
        tag1.hash(&mut hasher1);

        let mut hasher2 = DefaultHasher::new();
        tag2.hash(&mut hasher2);

        assert_ne!(hasher1.finish(), hasher2.finish());
    }

    #[test]
    fn display_tag() {
        let tag = Tag {
            title: String::from("TAG"),
            tag: String::from("xyz"),
            usage: 0,
        };

        assert_eq!("xyz: TAG".to_owned(), format!("{}", tag));
    }

    #[test]
    fn equal_episode_means_equal_hash() {
        let episode1 = Episode {
            title: String::from("TWiT 245: No Hitler For You"),
            url: Url::parse("http://www.podtrac.com/pts/redirect.mp3/aolradio.podcast.aol.com/twit/twit0245.mp3").unwrap(),
            podcast_title: String::from("this WEEK in TECH - MP3 Edition"),
            podcast_url: Url::parse("http://leo.am/podcasts/twit").unwrap(),
            description: String::from("[...]"),
            website: Some(Url::parse("http://www.podtrac.com/pts/redirect.mp3/aolradio.podcast.aol.com/twit/twit0245.mp3").unwrap()),
            mygpo_link: Url::parse("http://gpodder.net/episode/1046492").unwrap(),
            released: NaiveDate::from_ymd(2010, 12, 25).and_hms(0, 30, 0),
        };
        let episode2 = Episode {
            title: String::from("Climate Change, News Corp, and the Australian Fires"),
            url: Url::parse("http://www.podtrac.com/pts/redirect.mp3/aolradio.podcast.aol.com/twit/twit0245.mp3").unwrap(),
            podcast_title: String::from("On the Media"),
            podcast_url: Url::parse("http://feeds.wnyc.org/onthemedia?format=xml").unwrap(),
            description: String::from("[...]"),
            website: Some(Url::parse("http://www.wnycstudios.org/story/climate-change-news-corp-and-australian-fires/").unwrap()),
            mygpo_link: Url::parse("http://gpodder.net/podcast/on-the-media-1/climate-change-news-corp-and-the-australian-fires").unwrap(),
            released: NaiveDate::from_ymd(2020, 1, 15).and_hms(17, 0, 0),
        };

        assert_eq!(episode1, episode2);
        assert_eq!(episode1.partial_cmp(&episode2), Some(Ordering::Equal));

        let mut hasher1 = DefaultHasher::new();
        episode1.hash(&mut hasher1);

        let mut hasher2 = DefaultHasher::new();
        episode2.hash(&mut hasher2);

        assert_eq!(hasher1.finish(), hasher2.finish());
    }

    #[test]
    fn not_equal_episodes_have_non_equal_ordering() {
        let episode1 = Episode {
            title: String::from("TWiT 245: No Hitler For You"),
            url: Url::parse("http://www.podtrac.com/pts/redirect.mp3/aolradio.podcast.aol.com/twit/twit0245.mp3").unwrap(),
            podcast_title: String::from("this WEEK in TECH - MP3 Edition"),
            podcast_url: Url::parse("http://leo.am/podcasts/twit").unwrap(),
            description: String::from("[...]"),
            website: Some(Url::parse("http://www.podtrac.com/pts/redirect.mp3/aolradio.podcast.aol.com/twit/twit0245.mp3").unwrap()),
            mygpo_link: Url::parse("http://gpodder.net/episode/1046492").unwrap(),
            released: NaiveDate::from_ymd(2010, 12, 25).and_hms(0, 30, 0),
        };
        let episode2 = Episode {
            title: String::from("Climate Change, News Corp, and the Australian Fires"),
            url: Url::parse("https://www.podtrac.com/pts/redirect.mp3/audio.wnyc.org/otm/otm011520_podextra.mp3").unwrap(),
            podcast_title: String::from("On the Media"),
            podcast_url: Url::parse("http://feeds.wnyc.org/onthemedia?format=xml").unwrap(),
            description: String::from("[...]"),
            website: Some(Url::parse("http://www.wnycstudios.org/story/climate-change-news-corp-and-australian-fires/").unwrap()),
            mygpo_link: Url::parse("http://gpodder.net/podcast/on-the-media-1/climate-change-news-corp-and-the-australian-fires").unwrap(),
            released: NaiveDate::from_ymd(2020, 1, 15).and_hms(17, 0, 0),
        };

        assert_ne!(episode1, episode2);
        assert_eq!(episode1.partial_cmp(&episode2), Some(Ordering::Less));

        let mut hasher1 = DefaultHasher::new();
        episode1.hash(&mut hasher1);

        let mut hasher2 = DefaultHasher::new();
        episode2.hash(&mut hasher2);

        assert_ne!(hasher1.finish(), hasher2.finish());
    }

    #[test]
    fn display_episode() {
        let episode = Episode {
            title: String::from("TWiT 245: No Hitler For You"),
            url: Url::parse("http://www.podtrac.com/pts/redirect.mp3/aolradio.podcast.aol.com/twit/twit0245.mp3").unwrap(),
            podcast_title: String::from("this WEEK in TECH - MP3 Edition"),
            podcast_url: Url::parse("http://leo.am/podcasts/twit").unwrap(),
            description: String::from("[...]"),
            website: Some(Url::parse("http://www.podtrac.com/pts/redirect.mp3/aolradio.podcast.aol.com/twit/twit0245.mp3").unwrap()),
            mygpo_link: Url::parse("http://gpodder.net/episode/1046492").unwrap(),
            released: NaiveDate::from_ymd(2010, 12, 25).and_hms(0, 30, 0),
        };

        assert_eq!("TWiT 245: No Hitler For You: http://www.podtrac.com/pts/redirect.mp3/aolradio.podcast.aol.com/twit/twit0245.mp3".to_owned(), format!("{}", episode));
    }
}