Skip to content

Commit 17aa0b6

Browse files
committed
update config, stat making discord work again
1 parent b5cc3a3 commit 17aa0b6

File tree

7 files changed

+157
-36
lines changed

7 files changed

+157
-36
lines changed

cmd/Printify_api.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
9+
"github.com/spf13/viper"
10+
)
11+
12+
func printify_UploadImage(fileName, imageURL string) {
13+
// Create the JSON Request and Response Objects
14+
responseJson := PRINTIFY_ImageUploadResponse{}
15+
requestJson := PRINTIFY_ImageUploadRequest{
16+
FileName: fileName,
17+
URL: imageURL,
18+
}
19+
20+
// Marshal the JSON Request Body
21+
requestBody, err := json.Marshal(requestJson)
22+
catchErr(err)
23+
24+
// Create the HTTP Request
25+
req, err := http.NewRequest("POST", viper.GetString("printify_endpoint")+"/uploads/images.json", bytes.NewBuffer(requestBody))
26+
catchErr(err)
27+
28+
// Set the HTTP Request Headers
29+
req.Header.Set("Content-Type", "application/json")
30+
req.Header.Set("Authorization", "Bearer "+PRINTIFY_API_KEY)
31+
32+
// Verbose Output
33+
if verbose {
34+
trace()
35+
fmt.Println("🗂️ Request JSON:", requestJson)
36+
fmt.Println("🌐 HTTP Request", req)
37+
}
38+
39+
// Make the HTTP Request
40+
httpMakeRequest(req, &responseJson)
41+
42+
fmt.Println("📤 Image Uploaded to Printify")
43+
fmt.Println("📁 ID:", responseJson.ID)
44+
fmt.Println("📁 Name:", responseJson.FileName)
45+
fmt.Println("📁 Height:", responseJson.Height)
46+
fmt.Println("📁 Width:", responseJson.Width)
47+
fmt.Println("📁 Size:", responseJson.Size)
48+
fmt.Println("📁 MimeType:", responseJson.MimeType)
49+
fmt.Println("📁 PreviewURL:", responseJson.PreviewURL)
50+
fmt.Println("📁 UploadTime:", responseJson.UploadTime)
51+
52+
}

cmd/Printify_json.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cmd
2+
3+
type PRINTIFY_ImageUploadRequest struct {
4+
FileName string `json:"file_name"`
5+
URL string `json:"url"`
6+
}
7+
8+
type PRINTIFY_ImageUploadResponse struct {
9+
ID string `json:"id"`
10+
FileName string `json:"file_name"`
11+
Height int `json:"height"`
12+
Width int `json:"width"`
13+
Size int `json:"size"`
14+
MimeType string `json:"mime_type"`
15+
PreviewURL string `json:"preview_url"`
16+
UploadTime string `json:"upload_time"`
17+
}

cmd/consts.go

-20
This file was deleted.

cmd/http.go

+56
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,70 @@
11
package cmd
22

33
import (
4+
"encoding/json"
5+
"errors"
46
"fmt"
57
"io"
8+
"log"
69
"net/http"
710
"os"
811
"path/filepath"
12+
"strconv"
913
"strings"
14+
"time"
1015
)
1116

17+
// Create HTTP Client
18+
var httpClient = &http.Client{
19+
Timeout: time.Second * 60,
20+
}
21+
22+
func httpMakeRequest(request *http.Request, responseJson interface{}) {
23+
24+
// Make the HTTP Request
25+
resp, err := httpClient.Do(request)
26+
catchErr(err)
27+
28+
// Read the JSON Response Body
29+
jsonString, err := io.ReadAll(resp.Body)
30+
catchErr(err)
31+
32+
// Check for HTTP Errors
33+
httpCatchErr(resp, jsonString)
34+
if verbose {
35+
b, err := io.ReadAll(resp.Body)
36+
if err != nil {
37+
log.Fatalln(err)
38+
}
39+
fmt.Println("🌐 HTTP Response", b)
40+
}
41+
42+
// Unmarshal the JSON Response Body into provided responseJson
43+
err = json.Unmarshal([]byte(jsonString), &responseJson)
44+
catchErr(err)
45+
if verbose {
46+
trace()
47+
fmt.Println("🌐 HTTP Response String", string(jsonString))
48+
fmt.Println("🌐 HTTP Response JSON", responseJson)
49+
}
50+
// Close the HTTP Response Body
51+
defer resp.Body.Close()
52+
}
53+
54+
func httpCatchErr(resp *http.Response, jsonString []byte) {
55+
// Check for HTTP Response Errors
56+
if resp.StatusCode != 200 {
57+
catchErr(errors.New("API Error: " + strconv.Itoa(resp.StatusCode) + "\n" + string(jsonString)))
58+
}
59+
}
60+
61+
// func httpDumpRequest(r *http.Request) {
62+
// // Dump the HTTP Request
63+
// dump, err := httputil.DumpRequest(r, true)
64+
// catchErr(err)
65+
// fmt.Println("🌐 HTTP Request", string(dump))
66+
// }
67+
1268
// download file from url and save to local directory
1369
func httpDownloadFile(url string, filePath string) string {
1470
// Replace spaces with underscores

cmd/root.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ var verbose,
2424

2525
var prompt,
2626
configFile,
27-
OPENAI_API_KEY string
27+
OPENAI_API_KEY,
28+
DISCORD_API_KEY,
29+
PRINTIFY_API_KEY string
2830

2931
// rootCmd represents the base command when called without any subcommands
3032
var rootCmd = &cobra.Command{
@@ -84,6 +86,16 @@ func init() {
8486
if OPENAI_API_KEY == "" && verbose {
8587
fmt.Println("⚠️ OPENAI_API_KEY environment variable is not set, continuing without OpenAI API Key")
8688
}
89+
90+
DISCORD_API_KEY = os.Getenv("DISCORD_API_KEY")
91+
if DISCORD_API_KEY == "" && verbose {
92+
fmt.Println("⚠️ DISCORD_API_KEY environment variable is not set, continuing without Discord API Key")
93+
}
94+
95+
PRINTIFY_API_KEY = os.Getenv("PRINTIFY_API_KEY")
96+
if PRINTIFY_API_KEY == "" && verbose {
97+
fmt.Println("⚠️ PRINTIFY_API_KEY environment variable is not set, continuing without Printify API Key")
98+
}
8799
}
88100

89101
func viperConfig() {
@@ -115,6 +127,8 @@ func viperConfig() {
115127

116128
viper.SetDefault("radio_notificationSound", "~/.ponder/audio/notify.mp3")
117129

130+
viper.SetDefault("printify_endpoint", "https://api.printify.com/v1/")
131+
118132
viper.SetConfigName("config") // name of config file (without extension)
119133
viper.SetConfigType("yaml") // REQUIRED the config file does not have an extension
120134
viper.AddConfigPath(".") // look for config in the working directory

cmd/utils.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ func syntaxHighlight(message string) {
141141
}
142142
}
143143

144+
func fileNameFromURL(urlStr string) string {
145+
u, err := url.Parse(urlStr)
146+
catchErr(err)
147+
// Get the last path component of the URL
148+
filename := filepath.Base(u.Path)
149+
// Replace any characters that are not letters, numbers, or underscores with dashes
150+
filename = regexp.MustCompile(`[^a-zA-Z0-9_]+`).ReplaceAllString(filename, "-")
151+
// Limit the filename to 255 characters
152+
if len(filename) >= 255 {
153+
filename = filename[:255]
154+
}
155+
return filename
156+
}
157+
144158
func catchErr(err error, level ...string) {
145159
if err != nil {
146160
// Default level is "warn" if none is provided
@@ -164,20 +178,6 @@ func formatPrompt(prompt string) string {
164178
return regexp.MustCompile(`[^a-zA-Z0-9_]+`).ReplaceAllString(prompt, "-")
165179
}
166180

167-
func fileNameFromURL(urlStr string) string {
168-
u, err := url.Parse(urlStr)
169-
catchErr(err)
170-
// Get the last path component of the URL
171-
filename := filepath.Base(u.Path)
172-
// Replace any characters that are not letters, numbers, or underscores with dashes
173-
filename = regexp.MustCompile(`[^a-zA-Z0-9_]+`).ReplaceAllString(filename, "-")
174-
// Limit the filename to 255 characters
175-
if len(filename) >= 255 {
176-
filename = filename[:255]
177-
}
178-
return filename
179-
}
180-
181181
func trace() {
182182
pc := make([]uintptr, 10) // at least 1 entry needed
183183
runtime.Callers(2, pc)

files/config

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ discord_bot_systemMessage: |
3939
Ponder is here to help you with your Discord needs.
4040
Please be respectful and courteous when interacting with Ponder.
4141
Ponder will not tolerate any form of harassment, bullying, or discrimination.
42-
If you have any questions or concerns, please let us know. Thank you for using Ponder!
42+
If you have any questions or concerns, please let us know. Thank you for using Ponder!
43+
44+
printify_endpoint: "https://api.printify.com/v1"

0 commit comments

Comments
 (0)