SHARE
TWEET

gelbooru

a guest Mar 4th, 2015 779 Never
  1. /*
  2.         A program to download images from Danbooru.
  3.     Copyright (C) 2014  Anonymous
  4.  
  5.     This program is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19.  
  20. package main
  21.  
  22. import (
  23.         "fmt"
  24.         "net/http"
  25.         "os"
  26.         "encoding/xml"
  27.         "flag"
  28.         "io"
  29.         "io/ioutil"
  30.         "strings"
  31.         "strconv"
  32.         "errors"
  33. )
  34.  
  35. const BASE_URL = `http://gelbooru.com`
  36.  
  37. type Post struct {
  38.         Id int `xml:"id,attr"`
  39.         Url string `xml:"file_url,attr"`
  40. }
  41.  
  42. type PostList struct {
  43.         Posts []Post `xml:"post"`
  44. }
  45.  
  46. var cat string
  47. var start int
  48. var end int
  49.  
  50.  
  51. func main() {
  52.         initFlags()
  53.  
  54.         for i:=start; i<=end; i++ {
  55.                 fmt.Println("Downloading page", i)
  56.                 r, httpErr:=requestPage(i)
  57.                
  58.                 if httpErr!=nil {
  59.                         fmt.Println("Failed at page", i)
  60.                         fmt.Fprintln(os.Stderr, httpErr)
  61.                         os.Exit(1)
  62.                 }
  63.                
  64.                 if r.Status != "200 OK" {
  65.                         fmt.Println("Failed at page", i)
  66.                         fmt.Fprintln(os.Stderr, r.Status)
  67.                         os.Exit(1)
  68.                 }
  69.                
  70.                 body, readErr := ioutil.ReadAll(r.Body)
  71.                 r.Body.Close()
  72.                
  73.                 if readErr!=nil {
  74.                         fmt.Println("Failed at page", i)
  75.                         fmt.Fprintln(os.Stderr, readErr)
  76.                         os.Exit(1)
  77.                 }
  78.                
  79.                 var page PostList
  80.                 page.Posts=make([]Post, 0)
  81.                 xmlErr := xml.Unmarshal(body, &page)
  82.                
  83.                 if xmlErr!=nil {
  84.                         fmt.Println("Failed at page", i)
  85.                         fmt.Fprintln(os.Stderr, xmlErr)
  86.                         os.Exit(1)
  87.                 }
  88.                
  89.                 for _, p:=range(page.Posts) {
  90.                         fmt.Println("Downloading post", p.Id)
  91.                         dlErr:=downloadFromUrl(p.Url)
  92.                        
  93.                         if dlErr!=nil {
  94.                                 fmt.Fprintln(os.Stderr, dlErr)
  95.                         }
  96.                 }
  97.         }
  98. }
  99.  
  100. func initFlags() {
  101.         flag.StringVar(&cat, "c", "loli", "The category to download")
  102.         flag.IntVar(&start, "s", 0, "The first page to download")
  103.         flag.IntVar(&end, "e", 0, "The last page to download")
  104.        
  105.         flag.Parse()
  106. }
  107.  
  108. func downloadFromUrl(url string) error {
  109.         tokens := strings.Split(url, "/")
  110.         fileName := tokens[len(tokens)-1]
  111.  
  112.         if _, err := os.Stat(fileName); err == nil {
  113.                 return errors.New("File already exists")
  114.         }
  115.        
  116.         output, err := os.Create(fileName)
  117.         if err != nil {
  118.                 return err
  119.         }
  120.         defer output.Close()
  121.  
  122.         response, err := http.Get(url)
  123.         if err != nil {
  124.                 return err
  125.         }
  126.         defer response.Body.Close()
  127.  
  128.         _, copyErr := io.Copy(output, response.Body)
  129.         if copyErr != nil {
  130.                 return copyErr
  131.         }
  132.         return nil
  133. }
  134.  
  135. func requestPage(page int) (*http.Response, error) {
  136.         client := new(http.Client)
  137.  
  138.         r, err:=http.NewRequest("GET", BASE_URL+`/index.php?page=dapi&s=post&q=index&tags=`+cat+`&pid=`+strconv.Itoa(page), nil)
  139.        
  140.         if err!=nil {
  141.                 return nil, err
  142.         }
  143.        
  144.         r.Header.Add("User-Agent", `Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0`)
  145.        
  146.         return client.Do(r)
  147. }
RAW Paste Data
Top