Skip to content

Instantly share code, notes, and snippets.

@kfox
Last active October 26, 2019 00:42
Show Gist options
  • Save kfox/3eccf9958c706eb49083891729fe935d to your computer and use it in GitHub Desktop.
Save kfox/3eccf9958c706eb49083891729fe935d to your computer and use it in GitHub Desktop.
Download Desktop Wallpaper from Bing's "Picture of the Day" (macOS)
#!/usr/local/bin/node
const { spawnSync } = require('child_process')
const { createWriteStream, existsSync, mkdirSync } = require('fs')
const { get } = require('https')
const { join } = require('path')
const { parse: queryParser } = require('querystring')
const { parse: urlParser } = require('url')
const { HOME } = process.env
const WALLPAPERS_DIR = `${HOME}/Google Drive/Pictures/Bing`
const DAYS_TO_RETAIN_WALLPAPERS = 365
mkdirSync(WALLPAPERS_DIR, { recursive: true })
const getBingIndexPage = url => {
return new Promise((resolve, reject) => {
const request = get(url, response => {
let data = ''
if (response.statusCode !== 200) {
reject(new Error(`GET request failed with a ${response.statusCode}`))
}
response.on('data', chunk => data += chunk)
response.on('end', () => resolve(data))
})
request.on('error', (err) => reject(err))
})
}
const downloadAndSetWallpaperFromUrl = async urlObj => {
const filename = queryParser(urlObj.query).id
.replace(/^OHR./, '')
.replace(/_EN-US[0-9]+/, '')
const pathToFile = join(WALLPAPERS_DIR, filename)
if (!existsSync(pathToFile)) {
const file = createWriteStream(pathToFile)
get(urlObj, response => {
response.pipe(file)
response.on('end', () => setDesktopPicture(pathToFile))
})
}
}
const setDesktopPicture = pathToFile => {
const args = [
'-e',
`tell application "Finder" to set desktop picture to POSIX file "${pathToFile}"`,
'>/dev/null',
'2>&1',
]
const options = {
stdio: 'inherit',
}
spawnSync('/usr/bin/osascript', args, options)
}
const removeOldFiles = async () => {
const args = [
WALLPAPERS_DIR,
'-name',
'*.jpg',
'-ctime',
`+${DAYS_TO_RETAIN_WALLPAPERS}`,
'-delete',
]
const options = {
stdio: 'inherit',
}
spawnSync('find', args, options)
}
const getWallpaperUrl = async url => {
try {
const data = await getBingIndexPage(url)
const regex = /url:'(.*?)'/
const path = regex.exec(data)[1]
const downloadUrl = urlParser(path.replace(/^.*\//, url))
return downloadUrl
} catch (error) {
console.error(error)
}
}
;(async () => {
const url = 'https://www.bing.com/'
const wallpaperUrl = await getWallpaperUrl(url)
await downloadAndSetWallpaperFromUrl(wallpaperUrl)
await removeOldFiles()
})()

Usage

Note that this assumes you're saving the files into your local Google Drive folder. If you don't want that, change the WALLPAPERS_DIR value. If you want to keep more or less than 365 pictures, change the DAYS_TO_RETAIN_WALLPAPERS value.

  1. Install Node.js for macOS.
  2. Download the download-bing-wallpaper.js script.
  3. echo '1,31 * * * * /usr/local/bin/node /path/to/download-bing-wallpaper.js' | crontab -
  4. In System Preferences, open the Desktop & Screen Saver pane. On the Desktop tab, click the plus button (+) and enter the path to your saved wallpapers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment