Skip to content

Commit

Permalink
pick the correct file extensions for collections
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdallas committed Nov 3, 2022
1 parent 9470a68 commit 16fe476
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'a> Downloader<'a> {
let mut local_skipped = 0;
for (index, url) in media_urls.iter().enumerate() {
let mut item_index = format!("{}", index);

let mut extension = {
let mut full_url = Url::parse(url).unwrap();
full_url.set_query(None);
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'a> Downloader<'a> {
&item_index,
);

if self.should_download {
if self.should_download {
let status = self.save_or_skip(url, &file_name);
// update the summary statistics based on the status
match status.await? {
Expand Down Expand Up @@ -501,6 +501,7 @@ impl<'a> Downloader<'a> {

/// Check if a particular URL contains supported media.
async fn get_media(&self, data: &PostData) -> Result<Vec<SupportedMedia>, GertError> {

let original = data.url.as_ref().unwrap();
let mut media: Vec<SupportedMedia> = Vec::new();

Expand All @@ -514,6 +515,7 @@ impl<'a> Downloader<'a> {

let url = &parsed[..Position::AfterPath];
let gallery_info = data.gallery_data.borrow();
let media_metadata = data.media_metadata.borrow();

// reddit images and gifs
if url.contains(REDDIT_IMAGE_SUBDOMAIN) {
Expand Down Expand Up @@ -570,10 +572,21 @@ impl<'a> Downloader<'a> {
// collect all the URLs for the images in the album
let mut image_urls = Vec::new();
for item in gallery.items.iter() {


let mut ext = JPG_EXTENSION;
if let Some(metadata) = media_metadata {
if let Some(media) = metadata.get(&item.media_id) {
ext = media.m.split('/').last().unwrap();
}
}



// extract the media ID from each gallery item and reconstruct the image URL
let image_url = format!(
"https://{}/{}.{}",
REDDIT_IMAGE_SUBDOMAIN, item.media_id, JPG_EXTENSION
REDDIT_IMAGE_SUBDOMAIN, item.media_id, ext
);
image_urls.push(image_url);
}
Expand Down
14 changes: 12 additions & 2 deletions src/structures.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::ops::Add;
use std::{ops::Add, collections::HashMap};

/// Data structure that represents a user's info
#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -107,6 +107,8 @@ pub struct PostData {
pub title: Option<String>,
/// A timestamp of the time when the post was created, in **UTC**.
pub created_utc: Value,
/// Media Metadata
pub media_metadata: Option<HashMap<String, MediaMetadata>>,
/// Gallery metadata
pub gallery_data: Option<GalleryItems>,
/// Is post a video?
Expand All @@ -115,6 +117,14 @@ pub struct PostData {
pub media: Option<PostMedia>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct MediaMetadata {
pub status: String,
pub e: String,
pub m: String,
pub id: String,
}

#[derive(Deserialize, Debug, Clone)]
pub struct PostMedia {
pub reddit_video: Option<RedditVideo>,
Expand All @@ -132,7 +142,7 @@ pub struct GalleryItems {
pub items: Vec<GalleryItem>,
}

#[derive(Deserialize, Debug, Clone)]
#[derive(Deserialize, Debug, Serialize, Clone)]
pub struct GalleryItem {
/// The reddit media id, can be used to construct a redd.it URL
pub media_id: String,
Expand Down

0 comments on commit 16fe476

Please sign in to comment.