Pastebin PRO Accounts EASTER SPECIAL! For a limited time only get 40% discount on a LIFETIME PRO account! Offer Ends April 2nd!
SHARE
TWEET
gelbooru
a guest
Mar 4th, 2015
779
Never
- /*
- A program to download images from Danbooru.
- Copyright (C) 2014 Anonymous
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package main
- import (
- "fmt"
- "net/http"
- "os"
- "encoding/xml"
- "flag"
- "io"
- "io/ioutil"
- "strings"
- "strconv"
- "errors"
- )
- const BASE_URL = `http://gelbooru.com`
- type Post struct {
- Id int `xml:"id,attr"`
- Url string `xml:"file_url,attr"`
- }
- type PostList struct {
- Posts []Post `xml:"post"`
- }
- var cat string
- var start int
- var end int
- func main() {
- initFlags()
- for i:=start; i<=end; i++ {
- fmt.Println("Downloading page", i)
- r, httpErr:=requestPage(i)
- if httpErr!=nil {
- fmt.Println("Failed at page", i)
- fmt.Fprintln(os.Stderr, httpErr)
- os.Exit(1)
- }
- if r.Status != "200 OK" {
- fmt.Println("Failed at page", i)
- fmt.Fprintln(os.Stderr, r.Status)
- os.Exit(1)
- }
- body, readErr := ioutil.ReadAll(r.Body)
- r.Body.Close()
- if readErr!=nil {
- fmt.Println("Failed at page", i)
- fmt.Fprintln(os.Stderr, readErr)
- os.Exit(1)
- }
- var page PostList
- page.Posts=make([]Post, 0)
- xmlErr := xml.Unmarshal(body, &page)
- if xmlErr!=nil {
- fmt.Println("Failed at page", i)
- fmt.Fprintln(os.Stderr, xmlErr)
- os.Exit(1)
- }
- for _, p:=range(page.Posts) {
- fmt.Println("Downloading post", p.Id)
- dlErr:=downloadFromUrl(p.Url)
- if dlErr!=nil {
- fmt.Fprintln(os.Stderr, dlErr)
- }
- }
- }
- }
- func initFlags() {
- flag.StringVar(&cat, "c", "loli", "The category to download")
- flag.IntVar(&start, "s", 0, "The first page to download")
- flag.IntVar(&end, "e", 0, "The last page to download")
- flag.Parse()
- }
- func downloadFromUrl(url string) error {
- tokens := strings.Split(url, "/")
- fileName := tokens[len(tokens)-1]
- if _, err := os.Stat(fileName); err == nil {
- return errors.New("File already exists")
- }
- output, err := os.Create(fileName)
- if err != nil {
- return err
- }
- defer output.Close()
- response, err := http.Get(url)
- if err != nil {
- return err
- }
- defer response.Body.Close()
- _, copyErr := io.Copy(output, response.Body)
- if copyErr != nil {
- return copyErr
- }
- return nil
- }
- func requestPage(page int) (*http.Response, error) {
- client := new(http.Client)
- r, err:=http.NewRequest("GET", BASE_URL+`/index.php?page=dapi&s=post&q=index&tags=`+cat+`&pid=`+strconv.Itoa(page), nil)
- if err!=nil {
- return nil, err
- }
- r.Header.Add("User-Agent", `Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0`)
- return client.Do(r)
- }
RAW Paste Data
