removed redditbooru (site closed)
This commit is contained in:
@@ -14,7 +14,6 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
auth Auth
|
auth Auth
|
||||||
redditbooru_sources []string
|
|
||||||
|
|
||||||
// cache settings
|
// cache settings
|
||||||
cache_location string = "cache/"
|
cache_location string = "cache/"
|
||||||
@@ -28,9 +27,6 @@ func init() {
|
|||||||
// build the authentication struct
|
// build the authentication struct
|
||||||
build_auth()
|
build_auth()
|
||||||
|
|
||||||
// build the redditbooru sources slice
|
|
||||||
build_redditbooru_sources()
|
|
||||||
|
|
||||||
// attempt to make our cached
|
// attempt to make our cached
|
||||||
if _, err := os.Stat(cache_location); os.IsNotExist(err) {
|
if _, err := os.Stat(cache_location); os.IsNotExist(err) {
|
||||||
os.Mkdir(cache_location, 0777)
|
os.Mkdir(cache_location, 0777)
|
||||||
|
129
images.go
129
images.go
@@ -11,7 +11,6 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/buger/jsonparser"
|
"github.com/buger/jsonparser"
|
||||||
@@ -45,47 +44,6 @@ func getObjectLen(value []byte) (int, error) {
|
|||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// build our sources list for redditbooru
|
|
||||||
func build_redditbooru_sources() {
|
|
||||||
// the url for the sources from redditbooru
|
|
||||||
url := "https://redditbooru.com/sources/"
|
|
||||||
|
|
||||||
// set 5 second timeout on request
|
|
||||||
client := http.Client {
|
|
||||||
Timeout: 5 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the content of the page
|
|
||||||
resp, err := client.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Print("Error getting redditbooru sources, ")
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
// read response
|
|
||||||
out, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Print("Error reading response from redditbooru, ")
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the length of the sources
|
|
||||||
outlen, err := getArrayLen(out)
|
|
||||||
|
|
||||||
// set our slice to the appropriate size
|
|
||||||
redditbooru_sources = make([]string, outlen)
|
|
||||||
|
|
||||||
// set our sources into the slice
|
|
||||||
for i := 0; i < outlen; i++ {
|
|
||||||
// pull out the title
|
|
||||||
title,_ := jsonparser.GetString(out, "[" + strconv.Itoa(i) + "]", "title")
|
|
||||||
|
|
||||||
// set to lowercase then into our slice
|
|
||||||
redditbooru_sources[i] = strings.ToLower(title)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// search based on the most appropriate location
|
// search based on the most appropriate location
|
||||||
func get_image(sub string) <-chan string {
|
func get_image(sub string) <-chan string {
|
||||||
// make the channell
|
// make the channell
|
||||||
@@ -96,7 +54,6 @@ func get_image(sub string) <-chan string {
|
|||||||
|
|
||||||
// setup variable
|
// setup variable
|
||||||
var image string
|
var image string
|
||||||
var found bool = false
|
|
||||||
var cache_exists bool = false
|
var cache_exists bool = false
|
||||||
|
|
||||||
// check if a cached version exists
|
// check if a cached version exists
|
||||||
@@ -123,24 +80,12 @@ func get_image(sub string) <-chan string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check to see if it is in the redditbooru sources
|
// check reddit
|
||||||
for _,title := range redditbooru_sources {
|
image = <-get_subreddit_image(sub, cache_exists)
|
||||||
if title == sub {
|
ret <- image
|
||||||
image = <-get_redditbooru_image(sub, cache_exists)
|
return
|
||||||
found = true
|
|
||||||
ret <- image
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// if not in redditbooru, check reddit
|
|
||||||
if found == false {
|
|
||||||
image = <-get_subreddit_image(sub, cache_exists)
|
|
||||||
ret <- image
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// nothing found
|
// nothing found
|
||||||
ret <- ""
|
ret <- ""
|
||||||
|
|
||||||
@@ -149,72 +94,6 @@ func get_image(sub string) <-chan string {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// redditbooru request
|
|
||||||
func get_redditbooru_image(sub string, cache bool) <-chan string{
|
|
||||||
// make the channel
|
|
||||||
ret := make(chan string)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer close(ret)
|
|
||||||
|
|
||||||
// information variable
|
|
||||||
var out []byte
|
|
||||||
|
|
||||||
// if a cached version exists, use that instead
|
|
||||||
if cache == true {
|
|
||||||
// read our cached file
|
|
||||||
outdata, err := ioutil.ReadFile(cache_location + sub)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error reading from cached reddit file, ", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy to our info var
|
|
||||||
out = outdata
|
|
||||||
} else {
|
|
||||||
// create the proper url with the subreddit
|
|
||||||
url := "https://" + sub + ".redditbooru.com/images/?limit=1000"
|
|
||||||
|
|
||||||
// set 5 second timeout on request
|
|
||||||
client := http.Client {
|
|
||||||
Timeout: 5 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the content of the page
|
|
||||||
resp, err := client.Get(url)
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
// read response
|
|
||||||
outdata, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error reading response from redditbooru, ", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy to our info var
|
|
||||||
out = outdata
|
|
||||||
|
|
||||||
// attempt to make a cache file, don't do anything if it doesn't get created (who cares?)
|
|
||||||
os.Create(cache_location + sub)
|
|
||||||
var file, _ = os.OpenFile(cache_location + sub, os.O_RDWR, 0777)
|
|
||||||
defer file.Close()
|
|
||||||
file.Write(outdata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// get a random number for the image
|
|
||||||
outlen,_ := getArrayLen(out)
|
|
||||||
random_img := rand.Intn(outlen)
|
|
||||||
|
|
||||||
// select a random url from our list
|
|
||||||
img_url,_ := jsonparser.GetString(out, "[" + strconv.Itoa(random_img) + "]", "cdnUrl")
|
|
||||||
|
|
||||||
// set the return value
|
|
||||||
ret <- img_url
|
|
||||||
}()
|
|
||||||
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
// imgur request
|
// imgur request
|
||||||
func get_imgur_image(sub string) <-chan string {
|
func get_imgur_image(sub string) <-chan string {
|
||||||
// make channel
|
// make channel
|
||||||
|
Reference in New Issue
Block a user