SHARE
TWEET

rule34.paheal.net

a guest Mar 5th, 2015 901 Never
  1. /*
  2.     A program to download images from rule34.paheal.net.
  3.     Copyright (C) 2015  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.         "regexp"
  27.         "flag"
  28.         "io"
  29.         "io/ioutil"
  30.         "strings"
  31.         "strconv"
  32.         "errors"
  33. )
  34.  
  35. const BASE_URL = `http://rule34.paheal.net`
  36.  
  37. type Post struct {
  38.         Id int
  39.         Url string
  40. }
  41.  
  42. var cat string
  43. var start int
  44. var end int
  45.  
  46.  
  47. func main() {
  48.         initFlags()
  49.  
  50.         for i:=start; i<=end; i++ {
  51.                 fmt.Println("Downloading page", i)
  52.                 r, httpErr:=requestPage(i)
  53.                
  54.                 if httpErr!=nil {
  55.                         fmt.Println("Failed at page", i)
  56.                         fmt.Fprintln(os.Stderr, httpErr)
  57.                         os.Exit(1)
  58.                 }
  59.                
  60.                 if r.Status != "200 OK" {
  61.                         fmt.Println("Failed at page", i)
  62.                         fmt.Fprintln(os.Stderr, r.Status)
  63.                         os.Exit(1)
  64.                 }
  65.                
  66.                 body, readErr := ioutil.ReadAll(r.Body)
  67.                 r.Body.Close()
  68.                
  69.                 if readErr!=nil {
  70.                         fmt.Println("Failed at page", i)
  71.                         fmt.Fprintln(os.Stderr, readErr)
  72.                         os.Exit(1)
  73.                 }
  74.                
  75.                 posts:=getPosts(string(body))
  76.                
  77.                 for _, p:=range(posts) {
  78.                         if p.Url!="" {
  79.                                 fmt.Println("Downloading post", p.Id)
  80.                                 dlErr:=downloadFromUrl(p.Url)
  81.                        
  82.                                 if dlErr!=nil {
  83.                                         fmt.Fprintln(os.Stderr, dlErr)
  84.                                 }
  85.                         }
  86.                 }
  87.         }
  88. }
  89.  
  90. func initFlags() {
  91.         flag.StringVar(&cat, "c", "", "The category to download")
  92.         flag.IntVar(&start, "s", 1, "The first page to download")
  93.         flag.IntVar(&end, "e", 1, "The last page to download") 
  94.  
  95.         flag.Parse()
  96. }
  97.  
  98.  
  99. func downloadFromUrl(url string) error {
  100.         tokens := strings.Split(url, "/")
  101.         fileName := tokens[len(tokens)-1]
  102.  
  103.         if _, err := os.Stat(fileName); err == nil {
  104.                 return errors.New("File already exists")
  105.         }
  106.        
  107.         output, err := os.Create(fileName)
  108.         if err != nil {
  109.                 return err
  110.         }
  111.         defer output.Close()
  112.  
  113.         response, err := http.Get(url)
  114.         if err != nil {
  115.                 return err
  116.         }
  117.         defer response.Body.Close()
  118.  
  119.         _, copyErr := io.Copy(output, response.Body)
  120.         if copyErr != nil {
  121.                 return copyErr
  122.         }
  123.         return nil
  124. }
  125.  
  126. func requestPage(page int) (*http.Response, error) {
  127.         client := new(http.Client)
  128.        
  129.         url:=BASE_URL+`/post/list/`+cat+`/`+strconv.Itoa(page)
  130.        
  131.         r, err:=http.NewRequest("GET", url, nil)
  132.        
  133.         if err!=nil {
  134.                 return nil, err
  135.         }
  136.        
  137.         r.Header.Add("User-Agent", `Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0`)
  138.        
  139.         return client.Do(r)
  140. }
  141.  
  142. func getPosts(site string) []Post {
  143.         postRegex:=regexp.MustCompile(`class='shm-thumb-link' href='/post/view/([0-9]*)'.*<a href="(.*)"`)
  144.         postMatches:=postRegex.FindAllStringSubmatch(site, -1)
  145.        
  146.         posts:=make([]Post, len(postMatches))
  147.  
  148.         for i, s:=range(postMatches) {
  149.                 postNr, err := strconv.Atoi(s[1])
  150.                 if err==nil {
  151.                         posts[i].Id=postNr
  152.                         posts[i].Url=s[2]
  153.                 } else {
  154.                         posts[i].Id=-1
  155.                 }
  156.         }
  157.         return posts
  158. }
RAW Paste Data
Top