139 lines
3.6 KiB
Rust
139 lines
3.6 KiB
Rust
extern crate rss;
|
|
extern crate serde;
|
|
extern crate tokio;
|
|
extern crate chrono;
|
|
extern crate reqwest;
|
|
extern crate serde_json;
|
|
use std::env;
|
|
use std::path::Path;
|
|
use std::process::exit;
|
|
use reqwest::Client;
|
|
use serde_json::json;
|
|
use serde::Deserialize;
|
|
use chrono::{Utc, DateTime, NaiveDateTime};
|
|
use rss::{Item, ItemBuilder, ChannelBuilder, GuidBuilder};
|
|
|
|
const QUERY: &str = r#"query ($id: Int) {
|
|
Media (id: $id) {
|
|
title {
|
|
romaji
|
|
english
|
|
}
|
|
airingSchedule {
|
|
nodes {
|
|
id
|
|
airingAt
|
|
timeUntilAiring
|
|
episode
|
|
}
|
|
}
|
|
episodes
|
|
siteUrl
|
|
}
|
|
}"#;
|
|
|
|
#[derive(Deserialize)]
|
|
struct Root {
|
|
pub data: Data
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all="PascalCase")]
|
|
struct Data {
|
|
pub media: Media
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all="camelCase")]
|
|
struct Media {
|
|
pub title: MediaTitle,
|
|
pub airing_schedule: AiringScheduleConnection,
|
|
pub episodes: Option<i32>,
|
|
pub site_url: String
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct MediaTitle {
|
|
pub romaji: String,
|
|
pub english: Option<String>
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct AiringScheduleConnection {
|
|
pub nodes: Vec<AiringSchedule>
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all="camelCase")]
|
|
struct AiringSchedule {
|
|
pub id: i32,
|
|
pub airing_at: i64,
|
|
pub time_until_airing: i64,
|
|
pub episode: i32
|
|
}
|
|
|
|
async fn async_main() {
|
|
let mut args = env::args();
|
|
let path = args.next().expect("Cannot get binary path");
|
|
let path = Path::new(&path).file_stem().unwrap().to_str().unwrap();
|
|
let anime_id = match args.next() {
|
|
Some(anime_id) => anime_id.parse::<i32>().unwrap(),
|
|
None => {
|
|
eprintln!("Usage: {} <anime id>", path);
|
|
exit(1);
|
|
}
|
|
};
|
|
let client = Client::new();
|
|
let json = json!({"query": QUERY, "variables": {"id": anime_id}});
|
|
let resp = client.post("https://graphql.anilist.co/")
|
|
.header("Content-Type", "application/json")
|
|
.header("Accept", "application/json")
|
|
.body(json.to_string())
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
let resp: Root = serde_json::from_str(&resp.text().await.unwrap()).unwrap();
|
|
let mut pub_date = Utc::now().to_rfc2822();
|
|
let mut last_build_date = pub_date.clone();
|
|
let resp = &resp.data.media;
|
|
let title = resp.title.english.as_ref().unwrap_or(&resp.title.romaji);
|
|
let mut items: Vec<Item> = Vec::with_capacity(resp.airing_schedule.nodes.len());
|
|
for i in &resp.airing_schedule.nodes {
|
|
let i_date = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(i.airing_at, 0), Utc).to_rfc2822();
|
|
pub_date = i_date.clone();
|
|
if i.time_until_airing > 0 {
|
|
break;
|
|
}
|
|
last_build_date = i_date.clone();
|
|
let mut title = format!("{} - {}", title, i.episode);
|
|
if Some(i.episode) == resp.episodes {
|
|
title.push_str(" END");
|
|
}
|
|
items.push(ItemBuilder::default()
|
|
.title(title)
|
|
.link(resp.site_url.clone())
|
|
.pub_date(i_date)
|
|
.guid(GuidBuilder::default().permalink(false).value(format!("ran-{}-{}", anime_id, i.id)).build().unwrap())
|
|
.build()
|
|
.unwrap()
|
|
);
|
|
}
|
|
items.reverse();
|
|
let channel = ChannelBuilder::default()
|
|
.title(title)
|
|
.link(resp.site_url.clone())
|
|
.description(format!("Aired episodes of {}", title))
|
|
.pub_date(pub_date)
|
|
.last_build_date(last_build_date)
|
|
.items(items)
|
|
.build()
|
|
.unwrap();
|
|
println!("{}", channel.to_string());
|
|
}
|
|
|
|
fn main() {
|
|
tokio::runtime::Runtime::new()
|
|
.unwrap()
|
|
.block_on(async_main());
|
|
}
|