Skip to content

Commit

Permalink
Update README and add full refresh option to server
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolai Schimke authored and Nikolai Schimke committed Feb 1, 2024
1 parent 5355946 commit ac4f400
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- [x] Add CI
- [x] remove `unwrap()`s
- [x] Store Timestamps instead of strings
- [ ] Other sports
- [x] Other sports

- to install use the following commands

Expand All @@ -32,14 +32,24 @@
```bash
sportshub

# to scrape use, where <T> is number of simultaneous tabs (defaults to 10)
sportshub scrape <T>
# to scrape all events, where <T> is number of simultaneous tabs use (defaults to 10)
sportshub data scrape -t <T>

# to scrape stream links (videos) use (use -H for headless)
sportshub data update -t <T> -H

# to run the http api use
sportshub server
sportshub serve

# to run on custom port use
sportshub server -p <PORT>
sportshub serve -p <PORT>

# to run the server silently use
sportshub serve -s

# to do a full data refresh before serving use
sportshub serve -F

```

- You can use crontab to refresh every 15 minutes,
Expand Down
32 changes: 22 additions & 10 deletions src/bin/sportshub.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::Sub;

use anyhow::Error;
use clap::{Parser, Subcommand};
use diesel::sqlite::Sqlite;
Expand Down Expand Up @@ -33,17 +31,24 @@ enum Commands {
},
#[clap(about = "Run the web server")]
Serve {
/// port to run the server on
/// (default: 3000)
/// port to run the server on
/// (default: 3000)
/// usage: sportshub serve -p 5173
#[clap(short, long, default_value = "3000")]
port: u16,

/// Whether to run the server in silent mode
/// (default: false)
/// Whether to run the server in silent mode
/// (default: false)
/// usage: sportshub serve -s
#[clap(short, long)]
silent: bool,

/// Do a full data refresh before hosting server?
/// (default: false)
/// usage: sportshub serve -F
/// usage: sportshub serve -F -s
#[clap(short = 'F', long = "full-refresh")]
full_refresh: bool,
},
}

Expand All @@ -67,9 +72,9 @@ enum DataCommands {

/// Whether to run the browser in headless mode
/// (default: true)
/// usage: sportshub update -H false
/// usage: sportshub update -H false -t 20
#[clap(short = 'H', long = "headless", default_value = "true")]
/// usage: sportshub update -H
/// usage: sportshub update -H -t 20
#[clap(short = 'H', long = "headless")]
headless: bool,
},
#[clap(about = "Get the info about the current database")]
Expand Down Expand Up @@ -120,7 +125,14 @@ async fn main() {
}
}
}
Some(Commands::Serve { port, silent }) => {
Some(Commands::Serve {
port,
silent,
full_refresh,
}) => {
if full_refresh {
scrape_utils::start_scraping(10, true).unwrap();
}
web_server_utils::run(port, silent).await.unwrap();
}
None => {
Expand Down
4 changes: 3 additions & 1 deletion src/constants/sports.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Sport {
pub name: &'static str,
pub url: &'static str,
}

impl Sport {
pub const fn new(name: &'static str, url: &'static str) -> Sport {
Sport { name, url }
Expand Down
20 changes: 18 additions & 2 deletions src/web_server_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
use db::models::Stream;
use rocket::{get, routes, serde::json::Json, Rocket};

use crate::db::{self, helpers::LeagueWithCountry};
use crate::{
constants::{self, sports::Sport},
db::{self, helpers::LeagueWithCountry},
};

#[get("/")]
async fn get_route_desc() -> &'static str {
"Hello, world!"
}

#[get("/all")]
async fn get_all_streams() -> Json<Vec<Stream>> {
let mut conn = db::helpers::establish_connection().unwrap();
let streams = db::helpers::get_streams(&mut conn).unwrap();
Expand Down Expand Up @@ -70,6 +78,13 @@ async fn info_get_leagues() -> Json<Vec<LeagueWithCountry>> {
Json(leagues)
}

#[get("/sports")]
async fn info_get_sports() -> Json<Vec<Sport>> {
let sports = constants::sports::SPORTS.to_vec();

Json(sports)
}

pub async fn run(port: u16, silent: bool) -> anyhow::Result<()> {
Rocket::custom(rocket::Config {
port,
Expand All @@ -83,6 +98,7 @@ pub async fn run(port: u16, silent: bool) -> anyhow::Result<()> {
.mount(
"/",
routes![
get_route_desc,
get_all_streams,
get_active_streams,
get_stream_by_id,
Expand All @@ -92,7 +108,7 @@ pub async fn run(port: u16, silent: bool) -> anyhow::Result<()> {
get_streams_by_either_team,
],
)
.mount("/info", routes![info_get_leagues])
.mount("/info", routes![info_get_leagues, info_get_sports])
.launch()
.await?;

Expand Down

0 comments on commit ac4f400

Please sign in to comment.