Browse Source

Fixed verse -f

It now deletes the file (if present), and downloads a fresh file.
Steve Thielemann 1 month ago
parent
commit
9ed732fee2
2 changed files with 35 additions and 23 deletions
  1. 32 23
      src/main.rs
  2. 3 0
      src/parse.rs

+ 32 - 23
src/main.rs

@@ -32,7 +32,7 @@ struct Cli {
     #[arg(short, long, default_value = "ESV")]
     version: String,
 
-    /// User Agent
+    /// Update Firefox User Agent version
     #[arg(short, long, action=clap::ArgAction::SetTrue)]
     agent_update: bool,
 
@@ -50,15 +50,15 @@ enum Commands {
     },
     /// Extract information from cached files
     Extract {
-        /// Count
-        #[arg(short, long)] // , default_value = "5")]
+        /// How many chapters to extract (default is all)
+        #[arg(short, long)] 
         count: Option<u32>,
 
-        /// Output file
+        /// JSON Output file
         #[arg(short, long)]
         output: Option<PathBuf>,
 
-        /// SQL output
+        /// SQL output file
         #[arg(short, long)]
         sql: Option<PathBuf>,
     },
@@ -89,6 +89,8 @@ static BOOKS: LazyLock<Vec<&str>> = LazyLock::new(|| {
     ])
 });
 
+// Note: There are more books in the Catholic bible that aren't listed here.
+
 #[allow(unused)]
 static BOOK_NAMES: LazyLock<Vec<&str>> = LazyLock::new(|| {
     Vec::from([
@@ -173,7 +175,7 @@ fn find_files(base_dir: &str, version: &str) -> Vec<String> {
     for path in paths {
         if let Ok(dir) = path {
             let filename = dir.file_name().to_string_lossy().to_string();
-            
+
             // Currently, I don't have anywhere to store the introduction part.
             if filename.contains("INTRO") {
                 continue;
@@ -208,25 +210,19 @@ fn find_files(base_dir: &str, version: &str) -> Vec<String> {
     result
 }
 
-// And maybe:
-// https://www.bible.com/bible/2692/GEN.1.NASB2020
-// https://www.bible.com/bible/1/GEN.1.KJV
-// https://www.bible.com/bible/2692/GEN.1.NASB2020
-// https://www.bible.com/bible/111/GEN.1.NIV
-// https://www.bible.com/bible/821/GEN.1.YLT98
-// Catholic
-// https://www.bible.com/bible/42/GEN.1.CPDV
-
-// Audio
-// https://www.bible.com/audio-bible/59/GEN.1.ESV
-// <script type="application/ld+json">{"@context":"https://schema.org","@type":"AudioObject","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.bible.com/audio-bible/59/GEN.1.ESV"},"headline":"Audio Bible: Listen to Genesis 1 English Standard Version 2016 ESV","contentUrl":"https://audio-bible-cdn.youversionapi.com/1/32k/GEN/1-9dcefc68c6f7244489f59475fc7a1866.mp3?version_id=59",
-
-// https://www.bible.com/verse-of-the-day
-
 fn main() -> Result<()> {
     let mut config = config::read_config(CONFIG_FILE)?;
 
     let cli = Cli::parse();
+
+    if !cli.work.is_dir() {
+        println!("The given work directory {:?} doesn't exist.", cli.work);
+        println!("Sorry, you'll need to create it to use it.");
+        // Display help.
+        let _show_help: Cli = Cli::parse_from(["--help"]);
+        return Ok(());
+    }
+
     // println!("Work Dir: {:?}", cli.work);
     // println!("Bible: {:?}", cli.bible);
 
@@ -250,7 +246,7 @@ fn main() -> Result<()> {
             println!("User agent OK.");
         }
     }
-
+ 
     match &cli.command {
         Some(Commands::Fetch { delay }) => {
             let client = reqwest::blocking::Client::builder()
@@ -479,12 +475,25 @@ fn main() -> Result<()> {
             }
         }
 
-        Some(Commands::Verse { fetch: _ }) => {
+        Some(Commands::Verse { fetch }) => {
             let client = reqwest::blocking::Client::builder()
                 .user_agent(&config.user_agent)
                 .build()?;
 
             println!("Verse of the day.");
+
+            if *fetch {
+                let filename = fetch::filename_from_url(VOD_URL)?;
+                let mut path = PathBuf::from(&cli.work);
+                path = path.join(filename);
+                if path.is_file() {
+                    println!("Removing file {:?}", path);
+                    std::fs::remove_file(path)?;
+                } else {
+                    println!("I don't see the file {:?} ?", path);
+                }
+            }
+
             let result = fetch::fetch_cache(
                 cli.work
                     .as_os_str()

+ 3 - 0
src/parse.rs

@@ -6,6 +6,8 @@ use scraper::Element;
 use std::collections::HashSet;
 use std::string::String;
 
+
+/// Find newest Firefox version number.
 pub fn find_versions(html: &String) -> Result<String> {
     let document = scraper::Html::parse_document(&html);
     let select_a = scraper::Selector::parse("a").unwrap();
@@ -13,6 +15,7 @@ pub fn find_versions(html: &String) -> Result<String> {
     for a in document.select(&select_a) {
         let text = element_text(a);
 
+        // Or:  "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:{}) Gecko/20100101 Firefox/{}"
         if version_match.is_match(&text) {
             return Ok(format!(
                 "Mozilla/5.0 (X11; Linux x86_64; rv:{}) Gecko/20100101 Firefox/{}",