-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetimgur-script
executable file
·43 lines (34 loc) · 1.4 KB
/
getimgur-script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/Rscript --no-restore --no-save
args <- commandArgs(trailingOnly = TRUE)
library(XML)
library(RCurl)
destdir <- "~/Desktop/to be xfrd"
getimgurURL<- function(url, destdir) {
preparse <- htmlParse(url)
# Album title becomes folder title
dirtitle <- gsub("\n|\t|(\\s{2}|\\s+$)+",
"",
xpathSApply(preparse, "//head//title", xmlValue))
dirtitle <- paste(gsub("/", "-", dirtitle), basename(url))
# set up directory, bail out if the file exists
dirtitle <- file.path(destdir, dirtitle)
if (file.exists(dirtitle)) {
return(NULL)
}
# Some galleries use different containers
container <- ifelse("outside album" %in% xpathSApply(preparse, "//body//div[@id='content']", xmlAttrs),
"//body//img[@class='unloaded thumb-title-embed']",
"//body//img[@class='unloaded thumb-title']")
# Grab image urls. Walks through list of image divs
thumbs.uri <- xpathSApply(preparse, container, xmlGetAttr, "data-src")
# Thumbnails on imgur are denoted with a trailing "s" in the filename
thumbs.uri <- sub("s.", ".", thumbs.uri, fixed = TRUE)
filetitles <- file.path(dirtitle, basename(thumbs.uri))
dir.create(dirtitle)
file.create(filetitles)
for (i in seq_along(thumbs.uri)) {
writeBin(getBinaryURL(thumbs.uri[i]), filetitles[i])
}
}
for (i in 1:length(args)) getimgurURL(url = args[i], destdir = destdir)
q()