5 Commits
0.3 ... 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
3 changed files with 81 additions and 42 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

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;
}

47
dau.go
View File

@@ -16,7 +16,7 @@ import (
"io/ioutil"
)
var current_version = "0.3"
const current_version = "0.4"
var last_check = time.Now()
var new_last_check = time.Now()
@@ -24,20 +24,12 @@ var new_last_check = time.Now()
var webhook_url string
var username 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)
}
func main() {
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)
@@ -45,13 +37,23 @@ func main() {
// 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 {
@@ -80,20 +82,17 @@ func check_updates() {
}
if (current_version < latest.Tag_name) {
fmt.Println("A new version is available:", 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, username string) {
// Declare the flags to be used
// helpFlag := getopt.Bool('h', "display help")
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")
@@ -110,6 +109,7 @@ func parse_options() (webhook_url string, path string, watch int, username strin
}
if (*versionFlag) {
fmt.Println("dau - https://github.com/tardisx/discord-auto-upload")
fmt.Printf("Version: %s\n", current_version)
os.Exit(0)
}
@@ -154,9 +154,7 @@ 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
@@ -173,13 +171,14 @@ 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)
}
start := time.Now()
client := &http.Client{ Timeout: time.Second * 30 }
resp, err := client.Do(request)
if err != nil {
@@ -204,7 +203,7 @@ func process_file(file string) {
if (err != nil) {
log.Print("could not parse JSON: ", err)
fmt.Println("Response was:", res_body)
fmt.Println("Response was:", string(res_body[:]))
return
}
if (len(res.Attachments) < 1) {
@@ -212,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)
}
}