8 Commits
0.2 ... 0.4

Author SHA1 Message Date
Justin Hawkins
72588642b6 Fix .gitignore 2017-02-22 15:52:28 +10:30
Justin Hawkins
7ff4685a70 Simple release build script 2017-02-22 15:47:51 +10:30
Justin Hawkins
f6b92ee8bd Show github link in --version 2017-02-22 15:47:26 +10:30
Justin Hawkins
68d9ab7859 Check path before starting to prevent crash. Show id of upload. 2017-02-21 17:10:00 +10:30
Justin Hawkins
d2d7843b6f Show upload rate and speed 2017-02-21 16:22:34 +10:30
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
4 changed files with 136 additions and 57 deletions

24
.gitignore vendored
View File

@@ -1,20 +1,4 @@
/blib/
/.build/
_build/
cover_db/
inc/
Build
!Build/
Build.bat
.last_cover_stats
/Makefile
/Makefile.old
/MANIFEST.bak
/META.yml
/META.json
/MYMETA.*
nytprof.out
/pm_to_blib
*.o
*.bs
/_eumm/
dist
release
discord-auto-upload
discord-auto-upload.exe

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.

52
build-release.pl Executable file
View File

@@ -0,0 +1,52 @@
#!/usr/bin/env perl
use strict;
use warnings;
open my $fh, "<", "dau.go" || die $!;
my $version;
while (<$fh>) {
$version = $1 if /^const\s+current_version.*?"([\d\.]+)"/;
}
close $fh;
die "no version?" unless defined $version;
# so lazy
system "rm", "-rf", "release", "dist";
system "mkdir", "release";
system "mkdir", "dist";
my %build = (
win => { env => { GOOS => 'windows', GOARCH => '386' }, filename => 'dau.exe' },
linux => { env => { GOOS => 'linux', GOARCH => '386' }, filename => 'dau' },
mac => { env => { GOOS => 'darwin', GOARCH => '386' }, filename => 'dau' },
);
foreach my $type (keys %build) {
mkdir "release/$type";
}
add_extras();
foreach my $type (keys %build) {
local $ENV{GOOS} = $build{$type}->{env}->{GOOS};
local $ENV{GOARCH} = $build{$type}->{env}->{GOARCH};
system "go", "build", "-o", "release/$type/" . $build{$type}->{filename};
system "zip", "-j", "dist/dau-$type-$version.zip", ( glob "release/$type/*" );
}
sub add_extras {
# bat file for windows
open (my $fh, ">", "release/win/dau.bat") || die $!;
print $fh 'set WEBHOOK_URL=https://yourdiscordwebhookURLhere' . "\r\n";
print $fh 'set SCREENSHOTS="C:\your\screenshot\directory\here"' ."\r\n";
print $fh 'set USERNAME="Posted by Joe Bloggs"' . "\r\n";
print $fh 'set WATCH=10' . "\r\n";
print $fh 'dau.exe --webhook %WEBHOOK_URL% --directory %SCREENSHOTS% --username %USERNAME% --watch %WATCH%' . "\r\n";
print $fh 'pause' . "\r\n";
close $fh;
}

108
dau.go
View File

@@ -16,36 +16,44 @@ import (
"io/ioutil"
)
var current_version = "0.2"
var last_check = time.Now()
const current_version = "0.4"
var last_check = time.Now()
var new_last_check = time.Now()
var webhook_url string
type webhook_response struct {
Test string
}
func keepLines(s string, n int) string {
result := strings.Join(strings.Split(s, "\n")[:n], "\n")
return strings.Replace(result, "\r", "", -1)
}
var webhook_url string
var username 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_path(path)
check_updates()
log.Print("Waiting for images to appear in ", path)
// wander the path, forever
for {
err := filepath.Walk(path, check_file)
if err != nil { log.Fatal("oh dear") }
//fmt.Printf("filepath.Walk() returned %v\n", err)
if err != nil { log.Fatal("could not watch path", err) }
last_check = new_last_check
time.Sleep(time.Duration(watch)*time.Second)
}
}
func check_path(path string) {
src, err := os.Stat(path)
if err != nil {
log.Fatal(err)
}
if !src.IsDir() {
log.Fatal(path, " is not a directory")
os.Exit(1)
}
}
func check_updates() {
type GithubRelease struct {
@@ -55,9 +63,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,35 +78,55 @@ 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) {
fmt.Println("A new version is available:", latest.Tag_name)
if (current_version < latest.Tag_name) {
fmt.Printf("You are currently on version %s, but version %s is available\n", current_version, latest.Tag_name)
fmt.Println("----------- Release Info -----------")
fmt.Println(latest.Body)
fmt.Println("------------------------------------")
fmt.Println("( You are currently on version:", current_version, ")")
}
}
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.Println("dau - https://github.com/tardisx/discord-auto-upload")
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() {
@@ -125,8 +154,10 @@ func file_eligible(file string) (bool) {
func process_file(file string) {
log.Print("Uploading ", file)
extraParams := map[string]string{
// "username": "Some username",
extraParams := map[string]string{ }
if (username != "") {
extraParams["username"] = username
}
type DiscordAPIResponseAttachment struct {
@@ -140,14 +171,15 @@ func process_file(file string) {
type DiscordAPIResponse struct {
Attachments []DiscordAPIResponseAttachment
id int64
Id int64 `json:",string"`
}
request, err := newfileUploadRequest(webhook_url, extraParams, "file", file)
if err != nil {
log.Fatal(err)
}
client := &http.Client{}
start := time.Now()
client := &http.Client{ Timeout: time.Second * 30 }
resp, err := client.Do(request)
if err != nil {
@@ -170,8 +202,8 @@ func process_file(file string) {
err = json.Unmarshal(res_body, &res)
if (err != nil) {
log.Fatal("could not parse JSON", err)
fmt.Println("Response was:", res_body)
log.Print("could not parse JSON: ", err)
fmt.Println("Response was:", string(res_body[:]))
return
}
if (len(res.Attachments) < 1) {
@@ -179,7 +211,11 @@ func process_file(file string) {
return
}
var a = res.Attachments[0]
log.Printf("Uploaded to %s %dx%d, %d bytes\n", a.Url, a.Width, a.Height, a.Size)
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("id: %d, %d bytes transferred in %.2f seconds (%.2f KiB/s)", res.Id, a.Size, elapsed.Seconds(), rate)
}
}