Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JS] A little modification to download all avaliable formats and also sort into folders. Also updated to ES6 imports #46

Open
inuzen opened this issue Sep 26, 2024 · 4 comments

Comments

@inuzen
Copy link

inuzen commented Sep 26, 2024

Basically title.
In this version i first get the list of content from 'https://storage.googleapis.com/panels-api/data/20240916/content-1a';
And then use HD download key to get the item from data link.
This allows to create folders with pictures names and download all avaliable formats for each picture from data url.
It will download like

  • downloads
  • -> Picture Label
    • -> Picture-hd.jpg
    • -> Picture-sd.jpg

Note: also used axios but you can replace it with fetch easily (i just had it installed already)

Code:

import axios from 'axios';
import { join, normalize } from 'path';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import fs from 'fs';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// URLs
const CONTENT_URL =
  'https://storage.googleapis.com/panels-api/data/20240916/content-1a';
const MEDIA_URL =
  'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~uhd';

// Create "downloads" folder if it doesn't exist
const downloadDir = join(__dirname, 'downloads');
if (!fs.existsSync(downloadDir)) {
  fs.mkdirSync(downloadDir);
}

const downloadImage = async (url, filePath) => {
  try {
    const response = await axios({
      url,
      method: 'GET',
      responseType: 'stream',
    });
    const writer = fs.createWriteStream(filePath);
    response.data.pipe(writer);

    return new Promise((resolve, reject) => {
      writer.on('finish', resolve);
      writer.on('error', reject);
    });
  } catch (error) {
    console.error(`Failed to download image from ${url}`, error);
  }
};


const downloadWallpapers = async () => {
  try {
    const contentResponse = await axios.get(CONTENT_URL);
    const mediaResponse = await axios.get(MEDIA_URL);
    const wallpapers = contentResponse.data.wallpapers;

    for (const wallpaper of wallpapers) {
      const label = wallpaper.label.replace(/[/\\?%*:|"<>]/g, '-'); // Sanitize folder name
      const folderPath = join(downloadDir, label);
      const dlKey = wallpaper?.dlm?.hd;
      if (!fs.existsSync(folderPath)) {
        fs.mkdirSync(folderPath);
      }

      const mediaData = mediaResponse.data.data?.[dlKey] || {};

      for (const key in mediaData) {
        const downloadLink = mediaData[key];
        const imageFileName = `${label}-${key}.jpg`;
        const filePath = join(folderPath, imageFileName);

        await downloadImage(downloadLink, filePath);
        console.log(`Downloaded ${imageFileName} to ${filePath}`);
      }
    }
  } catch (error) {
    console.error('Error downloading wallpapers:', error);
  }
};

downloadWallpapers();
@faithfulojebiyi
Copy link

?did you test you PR at all?

@inuzen
Copy link
Author

inuzen commented Sep 27, 2024

@faithfulojebiyi Need a little more info there friend.
This code worked for me with no issues.
Also, this is not a pull request but rather a suggestion. I trust you can modify the code however you like to suit your needs

@JJeeff248
Copy link

You just have to remove the return to download them all

@inuzen
Copy link
Author

inuzen commented Sep 28, 2024

@faithfulojebiyi hey, thanks! That is a left over when i tested it because i thought that the folder names do not match the content but turns out that it's just half of them is abstract lol
Removed the return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants