-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfywatch.go
139 lines (110 loc) · 3.11 KB
/
gfywatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"log"
"os"
"path"
"strings"
"time"
"github.com/bmhatfield/gfywatch/files"
"github.com/bmhatfield/gfywatch/gfycat"
"github.com/bmhatfield/gfywatch/notifications"
"github.com/bmhatfield/gfywatch/overwatch"
"github.com/fsnotify/fsnotify"
)
func metadataFromFilename(filepath string) *gfycat.UploadFile {
filename := path.Base(filepath)
fileparts := strings.Split(filename, "_")
title := strings.Title(fileparts[0])
return &gfycat.UploadFile{
Title: title,
Description: "Overwatch Play! Automatically uploaded by GfyWatch (github.com/bmhatfield/gfywatch)",
Tags: overwatch.TagsFromTitle(title),
NoMd5: true,
}
}
func handleNewUpload(grant *gfycat.GFYClientGrant, filepath string) {
waitForWriteComplete(filepath)
client, err := gfycat.NewGFYClient(grant)
if err != nil {
fmt.Println(err)
}
metadata := metadataFromFilename(filepath)
notifications.UploadStarted(path.Base(filepath))
current, err := client.LocalUpload(filepath, metadata)
if err != nil {
fmt.Println(err)
notifications.UploadError(err)
}
notifications.UploadComplete(path.Base(filepath), current.GfyName)
go watchUploadStatus(client, current)
}
func waitForWriteComplete(filepath string) {
watcher := files.Watch(path.Dir(filepath))
defer watcher.Close()
timer := time.NewTimer(time.Duration(300 * time.Second))
log.Println("Waiting 5 minutes for rendering to complete...")
for {
select {
case event := <-watcher.Events:
if event.Op == fsnotify.Write && event.Name == filepath {
log.Println("Detected final write of file, reducing wait...")
timer.Reset(time.Duration(10 * time.Second))
}
case err := <-watcher.Errors:
if err != nil {
log.Println("Error watching for events:", err)
return
}
case <-timer.C:
log.Println("Finished waiting for writes")
return
}
}
}
func watchUploadStatus(client *gfycat.GFYClient, current *gfycat.FileDrop) {
status, err := client.UploadStatus(current)
if err != nil {
fmt.Println("Error checking for upload status: ", err)
return
}
for status.Time != 0 {
time.Sleep(time.Duration(status.Time) * time.Second)
status, err = client.UploadStatus(current)
if err != nil {
fmt.Println("Error checking for upload status: ", err)
return
}
if status.Task == "encoding" && status.Progress != 0 {
log.Printf("Encoding is %d%% complete", int(status.Progress*100))
}
}
notifications.ProcessingComplete(current.GfyName)
}
func main() {
grant, err := gfycat.NewClientGrantFromFile("grant.json")
if err != nil {
log.Printf("Unable to load credentials: %s\n", err)
os.Exit(1)
}
watcher := files.Watch(".")
defer watcher.Close()
log.Println("Watching for new files...")
tracker := files.NewTracker(5)
for {
select {
case event := <-watcher.Events:
if event.Op == fsnotify.Create && path.Ext(event.Name) == ".mp4" {
if !tracker.In(event.Name) {
tracker.Add(event.Name)
log.Println("New file detected:", event.Name)
go handleNewUpload(grant, event.Name)
}
}
case err := <-watcher.Errors:
if err != nil {
log.Println("Error watching for events:", err)
}
}
}
}