From b22c1f8113edb0544e042df0f8cb44462a704e3b Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Tue, 4 Oct 2016 19:08:32 +0200 Subject: [PATCH 1/8] =?UTF-8?q?Add=20a=20codegen=20crate=20for=20generatin?= =?UTF-8?q?g=20an=20=E2=80=9CRecent=20Updates=E2=80=9D=20section?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codegen/.gitignore | 1 + codegen/Cargo.toml | 9 +++++ codegen/src/main.rs | 92 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 codegen/.gitignore create mode 100644 codegen/Cargo.toml create mode 100644 codegen/src/main.rs diff --git a/codegen/.gitignore b/codegen/.gitignore new file mode 100644 index 00000000..eb5a316c --- /dev/null +++ b/codegen/.gitignore @@ -0,0 +1 @@ +target diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml new file mode 100644 index 00000000..2c4c2efd --- /dev/null +++ b/codegen/Cargo.toml @@ -0,0 +1,9 @@ +[package] +authors = ["Philipp Oppermann "] +name = "codegen" +version = "0.1.0" + +[dependencies] +chrono = "0.2.25" +getopts = "0.2.14" +requests = "0.0.22" diff --git a/codegen/src/main.rs b/codegen/src/main.rs new file mode 100644 index 00000000..00ccab3e --- /dev/null +++ b/codegen/src/main.rs @@ -0,0 +1,92 @@ +extern crate requests; +extern crate getopts; +extern crate chrono; + +use chrono::Duration; +use std::fmt; + +fn main() { + use std::env; + use std::fs::File; + use std::io::Write; + + let args: Vec = env::args().collect(); + let mut opts = getopts::Options::new(); + + opts.optopt("o", "", "set output file name", "NAME"); + let matches = opts.parse(&args[1..]).unwrap(); + let output = matches.opt_str("o"); + + let pr_list = pr_list(); + + match output { + None => println!("{:?}", pr_list), + Some(output) => { + let mut file = File::create(output).expect("error while opening/creating output file"); + file.write_all(pr_list.as_bytes()).expect("error while writing to output file"); + } + } +} + +fn pr_list() -> String { + use chrono::{UTC, DateTime}; + + const URL: &'static str = "https://api.github.com/search/issues?q=repo:phil-opp/blog_os+type:\ + pr+is:merged+label:relnotes"; + + let mut ret = String::from("
    "); + + let res = requests::get(URL).expect("Error while querying GitHub API"); + let data = res.json().expect("Error parsing JSON"); + + for pr in data["items"].members().take(5) { + let now = UTC::now(); + let merged_at = pr["closed_at"].as_str().unwrap().parse::>().unwrap(); + let ago = now - merged_at; + + let item = format!(r#"
  • {} {}"#, + pr["html_url"], + pr["title"], + DateFmt(ago)); + ret.push_str(&item); + } + + ret.push_str("
"); + ret +} + +struct DateFmt(Duration); + +impl fmt::Display for DateFmt { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + try!(write!(f, r#"