first draft of new version
This commit is contained in:
159
src/lib.rs
159
src/lib.rs
@@ -1,59 +1,142 @@
|
||||
use chrono::NaiveDate;
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Episode {
|
||||
pub url: String,
|
||||
pub date: NaiveDate,
|
||||
pub struct Feed {
|
||||
episodes: Vec<Broadcast>,
|
||||
}
|
||||
|
||||
pub async fn newest_morning_journal_streaming_url() -> Result<Episode, Box<dyn std::error::Error>> {
|
||||
let (date, url) = get_newest_morning_journal().await?;
|
||||
let url = get_streaming_url(url).await?;
|
||||
impl Feed {
|
||||
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let mut ret = Self {
|
||||
episodes: Vec::new(),
|
||||
};
|
||||
|
||||
Ok(Episode { url, date })
|
||||
ret.fetch().await?;
|
||||
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
pub async fn fetch(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let broadcasts = get_all_broadcasts().await?;
|
||||
println!("hea");
|
||||
|
||||
for broadcast in broadcasts {
|
||||
if !self.has_broadcast_url(&broadcast) {
|
||||
if let Ok(Some(broadcast)) = Broadcast::from(broadcast).await {
|
||||
self.episodes.push(broadcast);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn has_broadcast_url(&self, url: &str) -> bool {
|
||||
self.episodes.iter().any(|e| e.url == url)
|
||||
}
|
||||
|
||||
pub fn to_rss(&self) -> String {
|
||||
let mut ret = String::new();
|
||||
ret.push_str(r#"<?xml version="1.0" encoding="UTF-8"?>"#);
|
||||
ret.push_str(r#"<rss version="2.0">"#);
|
||||
ret.push_str("<channel>");
|
||||
ret.push_str("<title>Ö1 Live Journal Feed</title>");
|
||||
ret.push_str("<link>https://news.hofer.link</link>");
|
||||
ret.push_str("<description>Feed für Ö1 Journale. Live.</description>");
|
||||
|
||||
for episode in &self.episodes {
|
||||
ret.push_str("<item>");
|
||||
ret.push_str(&format!(
|
||||
"<title>{} ({})</title>",
|
||||
episode.title,
|
||||
episode.date.format("%d. %m.")
|
||||
));
|
||||
ret.push_str(&format!(
|
||||
"<enclosure url=\"{}\" length=\"0\" type=\"audio/mpeg\"/>\n",
|
||||
quick_xml::escape::escape(&episode.media_url)
|
||||
));
|
||||
ret.push_str("<description>Journal</description>");
|
||||
ret.push_str("</item>");
|
||||
}
|
||||
|
||||
ret.push_str(" </channel>");
|
||||
ret.push_str("</rss>");
|
||||
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
// List of broadcasts: https://audioapi.orf.at/oe1/api/json/current/broadcasts
|
||||
//
|
||||
// ^ contains link, e.g. https://audioapi.orf.at/oe1/api/json/4.0/broadcast/797577/20250611
|
||||
async fn get_newest_morning_journal() -> Result<(NaiveDate, String), Box<dyn std::error::Error>> {
|
||||
async fn get_all_broadcasts() -> Result<Vec<String>, Box<dyn std::error::Error>> {
|
||||
// List of broadcasts: https://audioapi.orf.at/oe1/api/json/current/broadcasts
|
||||
//
|
||||
// ^ contains link, e.g. https://audioapi.orf.at/oe1/api/json/4.0/broadcast/797577/20250611
|
||||
let url = "https://audioapi.orf.at/oe1/api/json/current/broadcasts";
|
||||
let data: Value = reqwest::get(url).await?.json().await?;
|
||||
|
||||
let mut ret: Vec<String> = Vec::new();
|
||||
|
||||
if let Some(days) = data.as_array() {
|
||||
for day in days.iter().rev() {
|
||||
for day in days {
|
||||
if let Some(broadcasts) = day["broadcasts"].as_array() {
|
||||
for broadcast in broadcasts.iter().rev() {
|
||||
if broadcast["title"] == "Einladen, ausladen, boykottieren"
|
||||
&& let Some(href) = broadcast["href"].as_str()
|
||||
{
|
||||
let date = broadcast["broadcastDay"].as_number().unwrap_or_else(|| {
|
||||
panic!("There needs to be a broadcastDay! {}", &broadcast)
|
||||
});
|
||||
let date = NaiveDate::parse_from_str(&date.to_string(), "%Y%m%d").expect("broadcastDay in https://audioapi.orf.at/oe1/api/json/current/broadcasts not in a valid format");
|
||||
return Ok((date, href.into()));
|
||||
}
|
||||
for broadcast in broadcasts {
|
||||
// if broadcast["title"] in [];
|
||||
ret.push(broadcast["href"].to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(String::from("No Ö1 Morgenjournal found").into())
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
async fn get_streaming_url(url: String) -> Result<String, Box<dyn std::error::Error>> {
|
||||
let data: Value = reqwest::get(url).await?.json().await?;
|
||||
let Some(streams) = data["streams"].as_array() else {
|
||||
return Err(String::from("No 'streams' found").into());
|
||||
};
|
||||
assert_eq!(streams.len(), 1);
|
||||
|
||||
let Some(id) = streams[0]["loopStreamId"].as_str() else {
|
||||
return Err(String::from("No 'loopStreamId' found").into());
|
||||
};
|
||||
|
||||
Ok(format!(
|
||||
"https://loopstream01.apa.at/?channel=oe1&shoutcast=0&id={id}"
|
||||
))
|
||||
#[derive(Clone)]
|
||||
pub struct Broadcast {
|
||||
pub url: String,
|
||||
pub media_url: String,
|
||||
pub title: String,
|
||||
pub date: NaiveDate,
|
||||
}
|
||||
|
||||
impl Broadcast {
|
||||
async fn from(url: String) -> Result<Option<Self>, Box<dyn std::error::Error>> {
|
||||
let data: Value = reqwest::get(&url).await?.json().await?;
|
||||
let Some(streams) = data["streams"].as_array() else {
|
||||
return Err(String::from("No 'streams' found").into());
|
||||
};
|
||||
if streams.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
assert_eq!(streams.len(), 1);
|
||||
|
||||
let Some(id) = streams[0]["loopStreamId"].as_str() else {
|
||||
return Err(String::from("No 'loopStreamId' found").into());
|
||||
};
|
||||
let media_url = format!("https://loopstream01.apa.at/?channel=oe1&shoutcast=0&id={id}");
|
||||
|
||||
let Some(title) = data["title"].as_str() else {
|
||||
return Err(format!("{url} has no title").into());
|
||||
};
|
||||
|
||||
let Some(date) = data["broadcastDay"].as_number() else {
|
||||
return Err(format!("{url} has no broadcastDay").into());
|
||||
};
|
||||
let Ok(date) = NaiveDate::parse_from_str(&date.to_string(), "%Y%m%d") else {
|
||||
return Err(
|
||||
format!("broadcastDay in {url} not in a valid format (%Y%m%d): {date}").into(),
|
||||
);
|
||||
};
|
||||
|
||||
Ok(Some(Self {
|
||||
url,
|
||||
media_url,
|
||||
title: title.into(),
|
||||
date,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Broadcast {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.url == other.url
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user