Pastebin PRO Accounts EASTER SPECIAL! For a limited time only get 40% discount on a LIFETIME PRO account! Offer Ends April 2nd!
SHARE
TWEET
rule34.paheal.net
a guest
Mar 5th, 2015
901
Never
- /*
- A program to download images from rule34.paheal.net.
- Copyright (C) 2015 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"
- "regexp"
- "flag"
- "io"
- "io/ioutil"
- "strings"
- "strconv"
- "errors"
- )
- const BASE_URL = `http://rule34.paheal.net`
- type Post struct {
- Id int
- Url string
- }
- 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)
- }
- posts:=getPosts(string(body))
- for _, p:=range(posts) {
- if p.Url!="" {
- fmt.Println("Downloading post", p.Id)
- dlErr:=downloadFromUrl(p.Url)
- if dlErr!=nil {
- fmt.Fprintln(os.Stderr, dlErr)
- }
- }
- }
- }
- }
- func initFlags() {
- flag.StringVar(&cat, "c", "", "The category to download")
- flag.IntVar(&start, "s", 1, "The first page to download")
- flag.IntVar(&end, "e", 1, "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)
- url:=BASE_URL+`/post/list/`+cat+`/`+strconv.Itoa(page)
- r, err:=http.NewRequest("GET", url, 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)
- }
- func getPosts(site string) []Post {
- postRegex:=regexp.MustCompile(`class='shm-thumb-link' href='/post/view/([0-9]*)'.*<a href="(.*)"`)
- postMatches:=postRegex.FindAllStringSubmatch(site, -1)
- posts:=make([]Post, len(postMatches))
- for i, s:=range(postMatches) {
- postNr, err := strconv.Atoi(s[1])
- if err==nil {
- posts[i].Id=postNr
- posts[i].Url=s[2]
- } else {
- posts[i].Id=-1
- }
- }
- return posts
- }
RAW Paste Data
