Fetches the average % of where in the YouTube video the sponsored segment is
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use std::error::Error; | |
use std::ffi::OsString; | |
use std::fs::File; | |
use std::process; | |
use csv::StringRecord; | |
fn get_first_arg() -> Result<OsString, Box<dyn Error>> { | |
match env::args_os().nth(1) { | |
None => Err(From::from("expected 1 argument, but got none")), | |
Some(file_path) => Ok(file_path), | |
} | |
} | |
fn should_skip(record: &StringRecord, video_length: f64) -> bool { | |
if video_length < 0.1 { | |
return true; | |
} | |
let video_votes = record[3].parse::<i32>().unwrap(); | |
if video_votes < 10 { | |
return true; | |
} | |
let category = record[10].to_string(); | |
let action = record[11].to_string(); | |
if category != "sponsor" || action != "skip" { | |
return true; | |
} | |
false | |
} | |
fn get_sponsor_average(record: &StringRecord, video_length: f64) -> f64 { | |
let start = record[1].parse::<f64>().unwrap(); | |
let end = record[2].parse::<f64>().unwrap(); | |
let average = ((start + end) / 2.0).round(); | |
((average / video_length) * 100.0).round() | |
} | |
fn get_videos() -> Result<(), Box<dyn Error>> { | |
let file_path = get_first_arg()?; | |
let file = File::open(file_path)?; | |
let mut rdr = csv::Reader::from_reader(file); | |
for result in rdr.records() { | |
let record = result?; | |
let video_length = record[13].parse::<f64>().unwrap(); | |
if should_skip(&record, video_length) { | |
continue; | |
} | |
let sponsor_average = get_sponsor_average(&record, video_length); | |
println!("{}", sponsor_average); | |
} | |
Ok(()) | |
} | |
fn main() { | |
let video_result = get_videos(); | |
if let Err(err) = video_result { | |
println!("error running example: {}", err); | |
process::exit(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment