3 Commits
0.2 ... 0.3

Author SHA1 Message Date
Justin Hawkins
13589535a8 Add timeouts for uploads and version check. 2017-02-21 14:57:10 +10:30
Justin Hawkins
cb1f1d1a05 Version and help commands. 2017-02-21 12:28:26 +10:30
Justin Hawkins
699ca9fcfc Add username support, clean up command line parsing, help and output 2017-02-21 12:24:14 +10:30
2 changed files with 58 additions and 18 deletions

View File

@@ -42,12 +42,19 @@ and the directory to watch:
`--directory /some/path/here` - the directory that screenshots will appear in.
You will have to quote the path on windows, or anywhere where the directory path contains spaces.
You will have to quote the path on windows, or anywhere where the directory path contains spaces. Note that
subdirectories will also be scanned.
Other parameters are:
`--watch xx` - specify how many seconds to wait between scanning the directory. The default is 10 seconds.
`--username <username>` - an arbitrary string to show as the bot's username in the channel.
`--help` - show command line help.
`--version` - show the version.
## Limitations/bugs
* Only files ending jpg, gif or png are uploaded.

67
dau.go
View File

@@ -16,10 +16,13 @@ import (
"io/ioutil"
)
var current_version = "0.2"
var last_check = time.Now()
var current_version = "0.3"
var last_check = time.Now()
var new_last_check = time.Now()
var webhook_url string
var webhook_url string
var username string
type webhook_response struct {
Test string
@@ -31,11 +34,14 @@ func keepLines(s string, n int) string {
}
func main() {
webhook, path, watch := parse_options()
webhook_url = webhook
webhook_opt, path, watch, username_opt := parse_options()
webhook_url = webhook_opt
username = username_opt
check_updates()
log.Print("Waiting for images to appear in ", path)
// wander the path, forever
for {
err := filepath.Walk(path, check_file)
@@ -55,9 +61,10 @@ func check_updates() {
Body string
}
resp, err := http.Get("https://api.github.com/repos/tardisx/discord-auto-upload/releases/latest")
client := &http.Client{ Timeout: time.Second * 5 }
resp, err := client.Get("https://api.github.com/repos/tardisx/discord-auto-upload/releases/latest")
if (err != nil) {
log.Fatal("could not check for updates")
log.Fatal("could not check for updates:", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
@@ -69,10 +76,10 @@ func check_updates() {
err = json.Unmarshal(body, &latest)
if (err != nil) {
log.Fatal("could not parse JSON", err)
log.Fatal("could not parse JSON: ", err)
}
if (current_version != latest.Tag_name) {
if (current_version < latest.Tag_name) {
fmt.Println("A new version is available:", latest.Tag_name)
fmt.Println("----------- Release Info -----------")
fmt.Println(latest.Body)
@@ -83,21 +90,43 @@ func check_updates() {
}
func parse_options() (webhook_url string, path string, watch int) {
func parse_options() (webhook_url string, path string, watch int, username string) {
// Declare the flags to be used
// helpFlag := getopt.Bool('h', "display help")
webhookFlag := getopt.StringLong("webhook", 'w', "", "webhook URL")
pathFlag := getopt.StringLong("directory", 'd', "", "directory")
watchFlag := getopt.Int16Long("watch", 's', 10, "time between scans")
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")
versionFlag := getopt.BoolLong ("version", 'v', "show version")
getopt.SetParameters("")
getopt.Parse()
return *webhookFlag, *pathFlag, int(*watchFlag)
if (*helpFlag) {
getopt.PrintUsage(os.Stderr)
os.Exit(0)
}
if (*versionFlag) {
fmt.Printf("Version: %s\n", current_version)
os.Exit(0)
}
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
}
func check_file(path string, f os.FileInfo, err error) error {
// fmt.Println("Comparing", f.ModTime(), "to", last_check, "for", path)
if f.ModTime().After(last_check) && f.Mode().IsRegular() {
@@ -129,6 +158,10 @@ func process_file(file string) {
// "username": "Some username",
}
if (username != "") {
extraParams["username"] = username
}
type DiscordAPIResponseAttachment struct {
Url string
Proxy_url string
@@ -147,7 +180,7 @@ func process_file(file string) {
if err != nil {
log.Fatal(err)
}
client := &http.Client{}
client := &http.Client{ Timeout: time.Second * 30 }
resp, err := client.Do(request)
if err != nil {
@@ -170,7 +203,7 @@ func process_file(file string) {
err = json.Unmarshal(res_body, &res)
if (err != nil) {
log.Fatal("could not parse JSON", err)
log.Print("could not parse JSON: ", err)
fmt.Println("Response was:", res_body)
return
}