2017-02-20 21:16:44 +10:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"github.com/pborman/getopt"
|
|
|
|
"path/filepath"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
"net/http"
|
2017-02-20 21:54:16 +10:30
|
|
|
"log"
|
|
|
|
"io"
|
|
|
|
"bytes"
|
|
|
|
"mime/multipart"
|
2017-02-21 11:15:12 +10:30
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2017-02-20 21:16:44 +10:30
|
|
|
)
|
|
|
|
|
2017-02-21 16:22:34 +10:30
|
|
|
var current_version = "0.4"
|
2017-02-21 12:24:14 +10:30
|
|
|
|
|
|
|
var last_check = time.Now()
|
2017-02-20 21:16:44 +10:30
|
|
|
var new_last_check = time.Now()
|
2017-02-21 12:24:14 +10:30
|
|
|
|
|
|
|
var webhook_url string
|
|
|
|
var username string
|
2017-02-20 21:54:16 +10:30
|
|
|
|
|
|
|
type webhook_response struct {
|
|
|
|
Test string
|
|
|
|
}
|
2017-02-20 21:16:44 +10:30
|
|
|
|
2017-02-21 11:15:12 +10:30
|
|
|
func keepLines(s string, n int) string {
|
|
|
|
result := strings.Join(strings.Split(s, "\n")[:n], "\n")
|
|
|
|
return strings.Replace(result, "\r", "", -1)
|
|
|
|
}
|
|
|
|
|
2017-02-20 21:16:44 +10:30
|
|
|
func main() {
|
2017-02-21 12:24:14 +10:30
|
|
|
webhook_opt, path, watch, username_opt := parse_options()
|
|
|
|
webhook_url = webhook_opt
|
|
|
|
username = username_opt
|
2017-02-20 21:16:44 +10:30
|
|
|
|
2017-02-21 11:15:12 +10:30
|
|
|
check_updates()
|
|
|
|
|
2017-02-21 14:57:10 +10:30
|
|
|
log.Print("Waiting for images to appear in ", path)
|
|
|
|
|
2017-02-20 21:16:44 +10:30
|
|
|
// wander the path, forever
|
|
|
|
for {
|
|
|
|
err := filepath.Walk(path, check_file)
|
2017-02-20 21:54:16 +10:30
|
|
|
if err != nil { log.Fatal("oh dear") }
|
2017-02-20 21:16:44 +10:30
|
|
|
//fmt.Printf("filepath.Walk() returned %v\n", err)
|
|
|
|
last_check = new_last_check
|
2017-02-20 21:54:16 +10:30
|
|
|
time.Sleep(time.Duration(watch)*time.Second)
|
2017-02-20 21:16:44 +10:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 11:15:12 +10:30
|
|
|
func check_updates() {
|
|
|
|
|
|
|
|
type GithubRelease struct {
|
|
|
|
Html_url string
|
|
|
|
Tag_name string
|
|
|
|
Name string
|
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
2017-02-21 14:57:10 +10:30
|
|
|
client := &http.Client{ Timeout: time.Second * 5 }
|
|
|
|
resp, err := client.Get("https://api.github.com/repos/tardisx/discord-auto-upload/releases/latest")
|
2017-02-21 11:15:12 +10:30
|
|
|
if (err != nil) {
|
2017-02-21 14:57:10 +10:30
|
|
|
log.Fatal("could not check for updates:", err)
|
2017-02-21 11:15:12 +10:30
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if (err != nil) {
|
|
|
|
log.Fatal("could not check read update response")
|
|
|
|
}
|
|
|
|
|
|
|
|
var latest GithubRelease
|
|
|
|
err = json.Unmarshal(body, &latest)
|
|
|
|
|
|
|
|
if (err != nil) {
|
2017-02-21 12:24:14 +10:30
|
|
|
log.Fatal("could not parse JSON: ", err)
|
2017-02-21 11:15:12 +10:30
|
|
|
}
|
|
|
|
|
2017-02-21 12:24:14 +10:30
|
|
|
if (current_version < latest.Tag_name) {
|
2017-02-21 11:15:12 +10:30
|
|
|
fmt.Println("A new version is available:", latest.Tag_name)
|
|
|
|
fmt.Println("----------- Release Info -----------")
|
|
|
|
fmt.Println(latest.Body)
|
|
|
|
fmt.Println("------------------------------------")
|
|
|
|
fmt.Println("( You are currently on version:", current_version, ")")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-21 12:24:14 +10:30
|
|
|
func parse_options() (webhook_url string, path string, watch int, username string) {
|
2017-02-20 21:16:44 +10:30
|
|
|
|
|
|
|
// Declare the flags to be used
|
|
|
|
// helpFlag := getopt.Bool('h', "display help")
|
2017-02-21 12:24:14 +10:30
|
|
|
webhookFlag := getopt.StringLong("webhook", 'w', "", "discord webhook URL")
|
|
|
|
pathFlag := getopt.StringLong("directory", 'd', "", "directory to scan, optional, defaults to current directory")
|
|
|
|
watchFlag := getopt.Int16Long ("watch", 's', 10, "time between scans")
|
|
|
|
usernameFlag := getopt.StringLong("username", 'u', "", "username for the bot upload")
|
|
|
|
helpFlag := getopt.BoolLong ("help", 'h', "help")
|
2017-02-21 12:28:26 +10:30
|
|
|
versionFlag := getopt.BoolLong ("version", 'v', "show version")
|
2017-02-21 12:24:14 +10:30
|
|
|
getopt.SetParameters("")
|
2017-02-20 21:16:44 +10:30
|
|
|
|
|
|
|
getopt.Parse()
|
|
|
|
|
2017-02-21 12:24:14 +10:30
|
|
|
if (*helpFlag) {
|
|
|
|
getopt.PrintUsage(os.Stderr)
|
2017-02-21 12:28:26 +10:30
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*versionFlag) {
|
|
|
|
fmt.Printf("Version: %s\n", current_version)
|
|
|
|
os.Exit(0)
|
2017-02-21 12:24:14 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
if ! getopt.IsSet("directory") {
|
|
|
|
*pathFlag = "./"
|
|
|
|
log.Println("Defaulting to current directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ! getopt.IsSet("webhook") {
|
|
|
|
log.Fatal("ERROR: You must specify a --webhook URL")
|
|
|
|
}
|
|
|
|
|
|
|
|
return *webhookFlag, *pathFlag, int(*watchFlag), *usernameFlag
|
2017-02-20 21:16:44 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
func check_file(path string, f os.FileInfo, err error) error {
|
|
|
|
|
|
|
|
if f.ModTime().After(last_check) && f.Mode().IsRegular() {
|
|
|
|
|
|
|
|
if file_eligible(path) {
|
|
|
|
// process file
|
|
|
|
process_file(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if new_last_check.Before(f.ModTime()) {
|
|
|
|
new_last_check = f.ModTime()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func file_eligible(file string) (bool) {
|
|
|
|
extension := strings.ToLower(filepath.Ext(file))
|
|
|
|
if extension == ".png" || extension == ".jpg" || extension == ".gif" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func process_file(file string) {
|
2017-02-21 11:15:12 +10:30
|
|
|
log.Print("Uploading ", file)
|
2017-02-20 21:54:16 +10:30
|
|
|
|
|
|
|
extraParams := map[string]string{
|
|
|
|
// "username": "Some username",
|
|
|
|
}
|
|
|
|
|
2017-02-21 12:24:14 +10:30
|
|
|
if (username != "") {
|
|
|
|
extraParams["username"] = username
|
|
|
|
}
|
|
|
|
|
2017-02-21 11:15:12 +10:30
|
|
|
type DiscordAPIResponseAttachment struct {
|
|
|
|
Url string
|
|
|
|
Proxy_url string
|
|
|
|
Size int
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
Filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
type DiscordAPIResponse struct {
|
|
|
|
Attachments []DiscordAPIResponseAttachment
|
|
|
|
id int64
|
|
|
|
}
|
|
|
|
|
2017-02-20 21:54:16 +10:30
|
|
|
request, err := newfileUploadRequest(webhook_url, extraParams, "file", file)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-02-21 16:22:34 +10:30
|
|
|
start := time.Now()
|
2017-02-21 14:57:10 +10:30
|
|
|
client := &http.Client{ Timeout: time.Second * 30 }
|
2017-02-20 21:54:16 +10:30
|
|
|
resp, err := client.Do(request)
|
|
|
|
if err != nil {
|
2017-02-21 11:15:12 +10:30
|
|
|
|
|
|
|
log.Fatal("Error performing request:", err)
|
|
|
|
|
2017-02-20 21:54:16 +10:30
|
|
|
} else {
|
2017-02-21 11:15:12 +10:30
|
|
|
|
|
|
|
if (resp.StatusCode != 200) {
|
|
|
|
log.Print("Bad response from server:", resp.StatusCode)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res_body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if (err != nil) {
|
|
|
|
log.Fatal("could not deal with body", err)
|
2017-02-20 21:54:16 +10:30
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
2017-02-21 11:15:12 +10:30
|
|
|
var res DiscordAPIResponse
|
|
|
|
err = json.Unmarshal(res_body, &res)
|
|
|
|
|
|
|
|
if (err != nil) {
|
2017-02-21 12:24:14 +10:30
|
|
|
log.Print("could not parse JSON: ", err)
|
2017-02-21 11:15:12 +10:30
|
|
|
fmt.Println("Response was:", res_body)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (len(res.Attachments) < 1) {
|
|
|
|
log.Print("bad response - no attachments?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var a = res.Attachments[0]
|
2017-02-21 16:22:34 +10:30
|
|
|
elapsed := time.Since(start)
|
|
|
|
rate := float64(a.Size) / elapsed.Seconds() / 1024.0
|
|
|
|
|
|
|
|
log.Printf("Uploaded to %s %dx%d", a.Url, a.Width, a.Height)
|
|
|
|
log.Printf("%d bytes transferred in %.2f seconds (%.2f KiB/s)", a.Size, elapsed.Seconds(), rate)
|
2017-02-20 21:54:16 +10:30
|
|
|
}
|
2017-02-21 11:15:12 +10:30
|
|
|
|
2017-02-20 21:54:16 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
body := &bytes.Buffer{}
|
|
|
|
writer := multipart.NewWriter(body)
|
|
|
|
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(part, file)
|
|
|
|
|
|
|
|
for key, val := range params {
|
|
|
|
_ = writer.WriteField(key, val)
|
|
|
|
}
|
|
|
|
err = writer.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", uri, body)
|
|
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
|
|
return req, err
|
2017-02-20 21:16:44 +10:30
|
|
|
}
|