Skip to content

Instantly share code, notes, and snippets.

@mxlje
Last active March 23, 2018 13:31
Show Gist options
  • Save mxlje/f003d6f02a149c01539b to your computer and use it in GitHub Desktop.
Save mxlje/f003d6f02a149c01539b to your computer and use it in GitHub Desktop.
download dropbox images from Camera Uploads folder

Dropbox Camera Upload image downloader

Disclaimer: This script is very hacky and could be optimized in almost every regard. It got the job done for me a little while ago (I was very angry at Dropbox when I wrote it), and I thought I’d share it.

You have been warned.

Setup

Dropbox Keys

You will need:

  • client ID
  • client secret
  • access token

You need to create an app in the Dropbox Developer Center and then you will get these values. Put them in on line 18 - 20. More info here https://github.com/stacktic/dropbox

log file

create an empty file called downloaded.txt, in which the script will log the images it has downloaded. You can do that by typing echo "" > downloaded.txt into your terminal.

Folder structure for the images to be downloaded

create an empty folder called Camera Uploads in the same directory. The script will put the downloaded images in there (it basically mirrors the path to the images from the root of your Dropbox).

running the script

You need a working Go environment, install instructions are here https://golang.org/dl/

Then:

$ go get github.com/cheggaaa/pb
$ go get github.com/davecgh/go-spew/spew
$ go get github.com/stacktic/dropbox
$ go run download.go

It should tell you how many image paths it saved (it will download those) and then there should be a progress bar.

package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"github.com/cheggaaa/pb"
"github.com/davecgh/go-spew/spew"
"github.com/stacktic/dropbox"
)
var (
err error
db *dropbox.Dropbox
clientid = ""
clientsecret = ""
token = ""
logfile *os.File
imagePaths []string
)
func init() {
logfile, err = os.OpenFile("./downloaded.txt", os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
}
func log(filepath string) {
logstring := fmt.Sprintf("%s\n", filepath)
if _, err = logfile.WriteString(logstring); err != nil {
panic(err)
}
}
func main() {
db = dropbox.NewDropbox()
db.SetAppInfo(clientid, clientsecret)
db.SetAccessToken(token)
gatherFilePaths()
downloadImages()
}
func gatherFilePaths() {
buf := new(bytes.Buffer)
entry, err := db.Metadata("Camera Uploads", true, false, "", "", -1)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
return
}
for _, entry := range entry.Contents {
buf.WriteString(fmt.Sprintf("%s\n", entry.Path))
}
err = ioutil.WriteFile("./imagepaths.txt", buf.Bytes(), 0644)
fmt.Println("Saved", len(entry.Contents), "image paths")
}
func downloadImages() {
file, err := os.Open("./imagepaths.txt")
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
imagePaths = append(imagePaths, scanner.Text())
}
if err := scanner.Err(); err != nil {
panic(err)
}
count := len(imagePaths)
bar := pb.StartNew(count)
for _, i := range imagePaths {
dest := fmt.Sprintf(".%s", i)
err = db.DownloadToFile(i, dest, "")
if err == nil {
log(i)
bar.Increment()
} else {
spew.Dump(err)
}
}
bar.Finish()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment