Skip to content

Instantly share code, notes, and snippets.

@mikezila
Last active August 29, 2015 14:05
Show Gist options
  • Save mikezila/2053110d509c834e5c4d to your computer and use it in GitHub Desktop.
Save mikezila/2053110d509c834e5c4d to your computer and use it in GitHub Desktop.
Rips 4chan thread content to a folder.
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"io"
"net/http"
"os"
"strings"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("Need exactly one arg, a 4chan thread url.")
return
}
doc, err := goquery.NewDocument(os.Args[1])
if err != nil {
panic(err)
}
titletokens := strings.Split(doc.Url.Path, "/")
title := titletokens[len(titletokens)-1]
os.Mkdir(title, 0775)
os.Chdir(title)
doc.Find(".fileText").Each(func(i int, s *goquery.Selection) {
imageurl, _ := s.Find("a").Attr("href")
tokens := strings.Split(imageurl, "/")
imageName := tokens[len(tokens)-1]
fmt.Printf("%s\n", imageName)
output, err := os.Create(imageName)
defer output.Close()
response, err := http.Get("http:" + imageurl)
if err != nil {
panic(err)
}
io.Copy(output, response.Body)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment