Skip to content

Commit

Permalink
support filtering by upvotes
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdallas committed Nov 7, 2022
1 parent 220de10 commit 17276e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ OPTIONS:
-o, --output <DATA_DIR> Directory to save the media to [default: .]
-p, --period <PERIOD> Time period to download from [default: day] [possible values: now, hour, day, week, month, year, all]
-s, --subreddit <SUBREDDIT>... Download media from these subreddit
-u, --upvotes <NUM> Minimum number of upvotes to download [default: 0]
```


Expand Down
14 changes: 7 additions & 7 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use reqwest::StatusCode;
use url::{Position, Url};

use crate::errors::GertError;
use crate::structures::{GfyData, StreamableApiResponse};
use crate::structures::Post;
use crate::structures::{GfyData, StreamableApiResponse};
use crate::utils::{check_path_present, check_url_has_mime_type};

pub static JPG_EXTENSION: &str = "jpg";
Expand Down Expand Up @@ -43,8 +43,8 @@ static GIPHY_MEDIA_SUBDOMAIN_2: &str = "media2.giphy.com";
static GIPHY_MEDIA_SUBDOMAIN_3: &str = "media3.giphy.com";
static GIPHY_MEDIA_SUBDOMAIN_4: &str = "media4.giphy.com";

pub static STREAMABLE_DOMAIN : &str = "streamable.com";
static STREAMABLE_API : &str = "https://api.streamable.com/videos";
pub static STREAMABLE_DOMAIN: &str = "streamable.com";
static STREAMABLE_API: &str = "https://api.streamable.com/videos";

/// Media Types Supported
#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -515,15 +515,15 @@ impl<'a> Downloader<'a> {
};

if parsed.files.contains_key(MP4_EXTENSION) {
let video_url = parsed.files.get(MP4_EXTENSION).unwrap().url.borrow().to_owned().unwrap();
let video_url =
parsed.files.get(MP4_EXTENSION).unwrap().url.borrow().to_owned().unwrap();
let ext = MP4_EXTENSION.to_owned();
let task = DownloadTask::from_post(post, video_url, ext , None);

let task = DownloadTask::from_post(post, video_url, ext, None);
self.schedule_task(task).await;
} else {
self.fail("No mp4 file found in streamable API response");
}

}

fn fail(&self, msg: &str) {
Expand Down
29 changes: 25 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn main() -> Result<(), GertError> {
.help("URL of a single post to download")
.takes_value(true)
.required_unless("subreddit")
.conflicts_with_all(&["subreddit", "period", "feed", "limit", "match"]),
.conflicts_with_all(&["subreddit", "period", "feed", "limit", "match", "upvotes"]),
)
.arg(
Arg::with_name("environment")
Expand Down Expand Up @@ -127,6 +127,15 @@ async fn main() -> Result<(), GertError> {
.possible_values(&["hot", "new", "top", "rising"])
.default_value("hot"),
)
.arg(
Arg::with_name("upvotes")
.short("u")
.long("upvotes")
.value_name("NUM")
.help("Minimum number of upvotes to download")
.takes_value(true)
.default_value("0"),
)
.get_matches();

let env_file = matches.value_of("environment");
Expand All @@ -138,6 +147,11 @@ async fn main() -> Result<(), GertError> {
// generate human readable file names instead of MD5 Hashed file names
let use_human_readable = matches.is_present("human_readable");
// restrict downloads to these subreddits
let upvotes = matches
.value_of("upvotes")
.unwrap()
.parse::<i64>()
.unwrap_or_else(|_| exit("Upvotes must be a number"));

let subreddits: Vec<&str> = match matches.is_present("subreddits") {
true => matches.values_of("subreddits").unwrap().collect(),
Expand Down Expand Up @@ -280,9 +294,16 @@ async fn main() -> Result<(), GertError> {
for subreddit in &subreddits {
let listing = Subreddit::new(subreddit).get_feed(feed, limit, period).await?;
posts.extend(
listing.data.children.into_iter().filter(|post| post.data.url.is_some() && !post.data.is_self).filter(
|post| pattern.is_match(post.data.title.as_ref().unwrap_or(&"".to_string())),
),
listing
.data
.children
.into_iter()
.filter(|post| {
post.data.url.is_some() && !post.data.is_self && post.data.score > upvotes
})
.filter(|post| {
pattern.is_match(post.data.title.as_ref().unwrap_or(&"".to_string()))
}),
);
}
}
Expand Down

0 comments on commit 17276e2

Please sign in to comment.