Skip to content

Commit

Permalink
Refactor scrape.rs and scrape_utils.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaiSch committed Feb 1, 2024
1 parent 9190409 commit 37a182b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
19 changes: 3 additions & 16 deletions src/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
constants::sports::{self, Sport},
db,
query_selectors,
scrape_utils,
scrape_utils::{self, create_browser},
};


Expand All @@ -42,25 +42,12 @@ pub fn start_scraping(open_tabs: usize, headless: bool) -> Result<(), anyhow::Er
// so... TODO!
check_all_links(&browser, &mut conn, open_tabs)?;

// we close all the tabs because otherwise it shows an error when program
// finishes
for t in (*browser.get_tabs().as_ref().lock().unwrap()).iter() {
t.close(true)?;
}

Ok(())
}

pub fn update_streams(open_tabs: usize, headless: bool) -> Result<(), anyhow::Error> {
// realised we didnt need adblocker when headless
let browser = Browser::new({
headless_chrome::LaunchOptions {
headless,
sandbox: true,
ignore_certificate_errors: true,
..Default::default()
}
})?;
let browser = create_browser(headless)?;

let mut conn = db::helpers::establish_connection()?;

Expand Down Expand Up @@ -174,7 +161,7 @@ pub fn parse_game(conn: &mut SqliteConnection, sport: &str, html: &str) -> Resul
let new_stream = models::StreamNew {
home: &home.trim(),
away: &away.trim(),
start_time: time.unwrap(),
start_time: time.ok_or(anyhow!("invalid time for {} - {}", &home.trim(), &away.trim()))?,
league,
country: &country.trim(),
url: &url.trim(),
Expand Down
29 changes: 29 additions & 0 deletions src/scrape_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,32 @@ pub fn create_browser(headless: bool) -> Result<Browser, Error> {

Ok(browser)
}


/// Close all tabs from browser, so we exit cleanly
///
/// # Arguments
/// * `browser` - The browser to close tabs from
///
/// # Returns
/// * `Result<(), Error>` - Whether the tabs were closed successfully
///
/// # Examples
/// ```
/// use scraper::scrape_utils::{close_tabs, create_browser};
///
/// let browser = create_browser(true).unwrap();
/// close_tabs(&browser).unwrap();
/// ```
///
/// # Panics
///
/// - If the browser is not running, this function will panic
/// - If the mutex is poisoned, this function will panic
pub fn close_tabs(browser: &Browser) -> Result<(), Error> {
for t in (*browser.get_tabs().as_ref().lock().unwrap()).iter() {
t.close(true)?;
}

Ok(())
}

0 comments on commit 37a182b

Please sign in to comment.