1
Fork 0
steam-rss/source/main.rs

245 lines
6.7 KiB
Rust
Raw Normal View History

2022-09-21 13:48:49 +00:00
//! # Steam RSS
//!
//! > **Get RSS feeds for Steam games.**
//!
//! *AGPL-3.0-or-later*
#![forbid(unsafe_code)]
#![warn(missing_docs, clippy::missing_docs_in_private_items)]
use std::{thread::sleep, time::Duration};
use {
clap::Parser,
color_eyre::{install, Result},
indicatif::{ProgressBar, ProgressStyle},
regex::Regex,
2022-09-22 16:42:44 +00:00
serde::Deserialize,
serde_json::Value,
2022-09-21 13:48:49 +00:00
};
/// CLI arguments struct using [`clap`]'s Derive API.
#[derive(Debug, Parser)]
#[clap(about, author, version)]
pub struct Args {
/// A game's AppID, can be used multiple times.
#[clap(short, long)]
pub appid: Vec<usize>,
2022-09-21 14:18:53 +00:00
/// Output the feeds as OPML.
#[clap(long)]
pub opml: bool,
2022-09-21 13:48:49 +00:00
/// The time in milliseconds to sleep between HTTP requests.
#[clap(short, long, default_value = "250")]
pub timeout: u64,
/// Verify potential feeds by downloading them and checking if they return XML.
#[clap(short, long)]
pub verify: bool,
/// A game's store URL, can be used multiple times.
#[clap(long)]
pub url: Vec<String>,
2022-09-22 16:42:44 +00:00
/// A person's steamcommunity.com ID or full URL, can be used multiple times.
#[clap(long)]
pub user: Vec<String>,
2022-09-21 13:48:49 +00:00
}
2022-09-21 19:56:34 +00:00
/// A simple feed struct.
#[derive(Debug)]
pub struct Feed {
2022-09-22 16:42:44 +00:00
/// A potential alternate friendly URL, see [`SteamApp::friendly_url`] for an
/// explanation.
pub friendly_url: Option<String>,
2022-09-21 19:56:34 +00:00
/// The text to use for the feed in the OPML output.
pub text: Option<String>,
/// The URL of the feed.
pub url: String,
}
2022-09-22 16:42:44 +00:00
/// A small representation of a Steam game that is parsed from JSON.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SteamApp {
/// The AppID of the game.
pub appid: usize,
/// The name of the game.
pub name: String,
/// A friendly URL name of the game, some feeds will use this instead of their
/// AppID for their RSS feed.
///
/// For example, [Portal's feed](https://steamcommunity.com/games/Portal/rss)
/// uses `Portal`, instead of
/// [its AppID 400](https://steamcommunity.com/games/400/rss).
///
/// Some games may also have a friendly URL different from their AppID but
/// don't use it for their feed. Steam is weird.
#[serde(rename = "friendlyURL")]
pub friendly_url: Value,
}
2022-09-21 13:48:49 +00:00
fn main() -> Result<()> {
install()?;
let args = Args::parse();
let timeout = Duration::from_millis(args.timeout);
let ureq_agent = ureq::AgentBuilder::new()
.user_agent("Steam Feeds (https://git.bauke.xyz/Bauke/steam-rss)")
.build();
let mut potential_feeds = vec![];
let mut feeds_to_output = vec![];
let store_url_regex =
Regex::new(r"(?i)^https?://store.steampowered.com/app/(?P<appid>\d+)")?;
2022-09-22 16:44:34 +00:00
let user_json_regex = Regex::new(r"var rgGames = (?P<json>\[.+\]);\s+var")?;
let user_id_regex = Regex::new(r"(i?)^\w+$")?;
let user_url_regex =
Regex::new(r"(?i)https?://steamcommunity.com/id/(?P<userid>\w+)")?;
2022-09-21 13:48:49 +00:00
for appid in args.appid {
2022-09-21 19:56:34 +00:00
potential_feeds.push(Feed {
2022-09-22 16:42:44 +00:00
friendly_url: None,
2022-09-21 19:56:34 +00:00
text: Some(format!("Steam AppID {appid}")),
url: appid_to_rss_url(appid),
2022-09-21 19:56:34 +00:00
});
2022-09-21 13:48:49 +00:00
}
for url in args.url {
let appid = store_url_regex
.captures(&url)
.and_then(|captures| captures.name("appid"))
.and_then(|appid_match| appid_match.as_str().parse::<usize>().ok());
if let Some(appid) = appid {
potential_feeds.push(Feed {
2022-09-22 16:42:44 +00:00
friendly_url: None,
text: Some(format!("Steam AppID {appid}")),
url: appid_to_rss_url(appid),
});
}
}
2022-09-22 16:44:34 +00:00
for user in args.user {
let user_url = if user_id_regex.is_match(&user) {
userid_to_games_url(user)
} else if let Some(user) = user_url_regex
.captures(&user)
.and_then(|captures| captures.name("userid"))
{
userid_to_games_url(user.as_str())
} else {
continue;
};
let body = ureq_agent.get(&user_url).call()?.into_string()?;
sleep(timeout);
let games_json = user_json_regex
.captures(&body)
.and_then(|captures| captures.name("json"))
.map(|json| json.as_str());
if let Some(games_json) = games_json {
let games = serde_json::from_str::<Vec<SteamApp>>(games_json)?;
for game in games {
let friendly_url = if game.friendly_url.is_string() {
Some(appid_to_rss_url(game.friendly_url.as_str().unwrap()))
} else {
None
};
potential_feeds.push(Feed {
friendly_url,
text: Some(game.name),
url: appid_to_rss_url(game.appid),
});
}
} else {
eprintln!("Couldn't scan games from: {user_url}");
eprintln!(
"Make sure \"Game Details\" in Privacy Settings is set to Public."
);
continue;
}
}
2022-09-21 13:48:49 +00:00
if args.verify {
let progress = ProgressBar::new(potential_feeds.len().try_into()?)
.with_style(ProgressStyle::with_template("Verifying {pos}/{len} {bar}")?);
2022-09-22 16:44:34 +00:00
let verify_feed = |url: &str| -> Result<_> {
let response = ureq_agent.get(&url).call()?;
sleep(timeout);
2022-09-22 16:44:34 +00:00
Ok((
response.content_type() == "text/xml",
response.into_string()?,
))
};
for mut potential_feed in potential_feeds {
let (mut is_valid_feed, mut body) = verify_feed(&potential_feed.url)?;
// If the potential URL doesn't return `text/xml`, try the friendly URL
// if one exists.
if !is_valid_feed && potential_feed.friendly_url.is_some() {
let friendly_url = potential_feed.friendly_url.as_deref().unwrap();
(is_valid_feed, body) = verify_feed(friendly_url)?;
if is_valid_feed {
potential_feed.url = friendly_url.to_string();
}
}
let verified_feed = if is_valid_feed {
let title_start = body.find("<title>").unwrap() + 7;
let title_end = body.find("</title>").unwrap();
Feed {
text: Some(body[title_start..title_end].to_string()),
..potential_feed
}
} else {
continue;
};
2022-09-22 16:44:34 +00:00
feeds_to_output.push(verified_feed);
2022-09-21 13:48:49 +00:00
progress.inc(1);
}
} else {
2022-09-22 16:44:34 +00:00
feeds_to_output.append(&mut potential_feeds);
2022-09-21 13:48:49 +00:00
}
2022-09-21 21:50:43 +00:00
let mut opml_document = opml::OPML {
head: None,
..Default::default()
};
2022-09-21 13:48:49 +00:00
for feed in feeds_to_output {
2022-09-21 14:18:53 +00:00
if args.opml {
2022-09-21 19:56:34 +00:00
opml_document
.add_feed(&feed.text.unwrap_or_else(|| feed.url.clone()), &feed.url);
2022-09-21 14:18:53 +00:00
} else {
2022-09-21 19:56:34 +00:00
println!("{}", feed.url);
2022-09-21 14:18:53 +00:00
}
}
if args.opml {
println!("{}", opml_document.to_string()?);
2022-09-21 13:48:49 +00:00
}
Ok(())
}
/// Creates a Steam RSS URL from a given AppID.
fn appid_to_rss_url<D: std::fmt::Display>(appid: D) -> String {
format!("https://steamcommunity.com/games/{appid}/rss/")
}
2022-09-22 16:44:34 +00:00
/// Creates a user's Steam Games URL from a given User ID.
fn userid_to_games_url<D: std::fmt::Display>(userid: D) -> String {
format!("https://steamcommunity.com/id/{userid}/games/?tab=all")
}