Skip to content

Commit

Permalink
refactor: remioved support for download pdfs (markdown still working)…
Browse files Browse the repository at this point in the history
… in github
  • Loading branch information
Kremilly committed Jul 11, 2024
1 parent 182f078 commit f943bca
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 94 deletions.
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,18 @@ readme = "https://gist.githubusercontent.com/Kremilly/5fd360d994bb0fe108b648d0e4
checksum = "https://gist.githubusercontent.com/kremilly/499d6d51d096c1813cea0eade8eb0bc4/raw/d7c5965aeaf005cf0b612e3468ab47c30480083b/scibun.sha256"
checksum.unmatch = "keep"
commands {
https://gist.githubusercontent.com/Kremilly/e0e0db11e43269da179adab610f38bb1/raw/6820be26a936a54bac713d03deb49edf804d0b6b/index.py
}
compress = "downloads.zip"
downloads {
https://arxiv.org/pdf/2405.01513 !ignore
https://www.scielo.br/j/rdbci/a/fwDKj9FMX7YTRDgkJGG4gnR?format=pdf&lang=pt !ignore
https://en.wikipedia.org/wiki/Rust_(programming_language) !ignore
https://en.wikipedia.org/wiki/Google !ignore
https://www.nasa.gov/wp-content/uploads/static/history/alsj/a17/A17_FlightPlan.pdf !ignore
https://sci-hub.se/10.1080/0025570x.2002.11953151
https://arxiv.org/pdf/2405.01513
https://olacesar.com/e-books/protegido.pdf
https://github.com/huyubing/books-pdf/blob/master/slime.pdf !ignore
https://raw.githubusercontent.com/facebook/react/main/README.md !ignore
https://pt.wikisource.org/wiki/Manifesto_da_Guerrilha_do_Livre_Acesso !ignore
}
commands {
https://gist.githubusercontent.com/Kremilly/e0e0db11e43269da179adab610f38bb1/raw/6820be26a936a54bac713d03deb49edf804d0b6b/index.py
}
```

> [!note]
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod tasks;
pub mod checksum;
pub mod monset;
pub mod monset;
pub mod checksum;
55 changes: 25 additions & 30 deletions src/cmd/monset.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use reqwest;

use std::{
fs::File,
error::Error,
Expand All @@ -12,6 +10,7 @@ use std::{

use crate::{
args_cli::Flags,
cmd::tasks::Tasks,
utils::validation::Validate,
ui::errors_alerts::ErrorsAlerts,

Expand All @@ -23,37 +22,33 @@ use crate::{

pub struct Monset;

impl Monset {
impl Monset {

pub async fn exec(run: &str, flags: &Flags) -> Result<(), Box<dyn Error>> {
async fn read_file(run: &str) -> Result<Cursor<Vec<u8>>, Box<dyn Error>> {
let mut buffer = Vec::new();

if run.starts_with("http") {
let _ = Validate::file_type(run, ".pbd").map_err(|e| {
ErrorsAlerts::generic(&e.to_string());
});

let response = reqwest::get(run).await?;
let bytes = response.bytes().await?;
buffer.extend_from_slice(&bytes);
} else {
let _ = Validate::file(run).map_err(|e| {
ErrorsAlerts::generic(&e.to_string());
});

let mut file = File::open(run)?;
file.read_to_end(&mut buffer)?;
}

{
let mut reader = Cursor::new(buffer.clone());
DownloadsBlock::read_lines(&mut reader, flags, run).await?;
}

{
let mut reader = Cursor::new(buffer);
RunnerBlock::read_lines(&mut reader).await?;
}
let _ = Validate::file(run).map_err(|e| {
ErrorsAlerts::generic(&e.to_string());
});

let mut file = File::open(run)?;
file.read_to_end(&mut buffer)?;

Ok(Cursor::new(buffer.clone()))
}

pub async fn downloads(run: &str, flags: &Flags) -> Result<Vec<String>, Box<dyn Error>> {
let mut reader = Self::read_file(run).await?;
let refs = DownloadsBlock::read_lines(&mut reader, flags, run).await?;

Tasks::compress(run, &refs)?;

Ok(refs)
}

pub async fn run_code(run: &str) -> Result<(), Box<dyn Error>> {
let mut reader = Self::read_file(run).await?;
RunnerBlock::read_lines(&mut reader).await?;

Ok(())
}
Expand Down
6 changes: 1 addition & 5 deletions src/consts/uris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ pub struct Uris;

impl Uris {

pub const PROVIDERS_DOMAINS: [&'static str; 5] = [
pub const PROVIDERS_DOMAINS: [&'static str; 2] = [
"wikipedia.org",
"sci-hub.se",
"github.com",
"githubusercontent.com",
"wikisource.org",
];

// Scimon
pub const SCIMON_API_REQUEST: &'static str = "http://localhost/Scimon/api/";
// pub const SCIHUB_ADDONS_ENDPOINT: &'static str = "https://addons.scibun.com/scihub?paper=";
pub const SCIMON_SCRAPE_API_ENPOINT: &'static str = "https://addons.scibun.com/scrape?url=";

pub const README_TEMPLATE_LINK: &'static str = "https://template.scibun.com/";
Expand Down
6 changes: 3 additions & 3 deletions src/monset/blocks/downloads_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct DownloadsBlock;

impl DownloadsBlock {

pub async fn read_lines<R>(reader: R, flags: &Flags, checksum_file: &str) -> Result<(), Box<dyn Error>> where R: BufRead {
pub async fn read_lines<R>(reader: R, flags: &Flags, checksum_file: &str) -> Result<Vec<String>, Box<dyn Error>> where R: BufRead {
let mut links = Vec::new();

let contents = reader.lines().collect::<Result<Vec<_>, _>>()?.join("\n");
Expand All @@ -52,7 +52,7 @@ impl DownloadsBlock {
let downloads_content = &contents[start_index + "downloads ".len()..end_index];

if downloads_content.trim().starts_with("commands {") {
return Ok(());
return Ok(links);
}

UI::section_header("downloads");
Expand Down Expand Up @@ -99,7 +99,7 @@ impl DownloadsBlock {
eprintln!("'downloads' block not found in file.");
}

Ok(())
Ok(links)
}

}
3 changes: 2 additions & 1 deletion src/scimon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ impl Scimon {

if !run.is_empty() {
if !Scibun::check_is_user(run) {
let _ = Monset::exec(run, &flags).await;
let _ = Monset::downloads(run, &flags).await;
let _ = Monset::run_code(run).await;
let _ = ReadMeBlock::render_block_and_save_file(run, &flags);
} else {
let _ = Scibun::get(run, &flags).await;
Expand Down
45 changes: 2 additions & 43 deletions src/system/providers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::error::Error;

use scihub_scraper::SciHubScraper;

use crate::{
consts::uris::Uris,
addons::wikipedia::Wikipedia,
Expand All @@ -17,18 +15,6 @@ pub struct Providers;

impl Providers {

fn extract_doi(url: &str) -> String {
if let Some(index) = url.find('/') {
let restante = &url[index + 2..];

if let Some(index) = restante.find('/') {
return restante[index + 1..].to_string();
}
}

String::new()
}

pub fn arxiv(url: &str) -> String {
let escape_quotes = UrlMisc::escape_quotes(url);

Expand All @@ -38,22 +24,9 @@ impl Providers {
escape_quotes.replace("/abs/", "/pdf/")
}
}

pub fn github(url: &str) -> String {
let escape_quotes = UrlMisc::escape_quotes(url);

if !Domains::check(&escape_quotes, Uris::PROVIDERS_DOMAINS[2]) {
escape_quotes.to_owned()
} else if !Domains::check(&escape_quotes, Uris::PROVIDERS_DOMAINS[3]) {
escape_quotes.to_owned()
} else {
escape_quotes.replace("/blob/", "/raw/")
}
}

pub fn check_provider_line(url: &str) -> String {
let arxiv_check = Providers::arxiv(url);
Providers::github(&arxiv_check)
Self::arxiv(url)
}

pub fn check_provider_domain(url: &str) -> bool {
Expand All @@ -68,18 +41,6 @@ impl Providers {
valid_domain
}

pub async fn scihub(url: &str) -> Result<(String, String), Box<dyn Error>> {
let mut scraper = SciHubScraper::new();

let paper = Self::extract_doi(url);
let paper = scraper.fetch_paper_pdf_url_by_doi(&paper).await?;

let paper_url = paper.to_string();
let filename = Remote::get_filename(&paper_url, true).await?;

Ok((paper_url, filename))
}

pub async fn generic(url: &str) -> Result<(String, String), Box<dyn Error>> {
let request_uri = url.to_string();
let filename = Remote::get_filename(url, true).await?;
Expand All @@ -93,10 +54,8 @@ impl Providers {

if Domains::check(url, Uris::PROVIDERS_DOMAINS[0]) {
(request_uri, filename) = Wikipedia::wikipedia(url);
} else if Domains::check(url, Uris::PROVIDERS_DOMAINS[4]) {
(request_uri, filename) = Wikipedia::wikisource(url);
} else if Domains::check(url, Uris::PROVIDERS_DOMAINS[1]) {
(request_uri, filename) = Self::scihub(url).await?;
(request_uri, filename) = Wikipedia::wikisource(url);
} else {
(request_uri, filename) = Self::generic(url).await?;
}
Expand Down

0 comments on commit f943bca

Please sign in to comment.