Skip to content

Commit

Permalink
update config, stat making discord work again
Browse files Browse the repository at this point in the history
  • Loading branch information
seemywingz committed May 14, 2024
1 parent b5cc3a3 commit 17aa0b6
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 36 deletions.
52 changes: 52 additions & 0 deletions cmd/Printify_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"net/http"

"github.com/spf13/viper"
)

func printify_UploadImage(fileName, imageURL string) {
// Create the JSON Request and Response Objects
responseJson := PRINTIFY_ImageUploadResponse{}
requestJson := PRINTIFY_ImageUploadRequest{
FileName: fileName,
URL: imageURL,
}

// Marshal the JSON Request Body
requestBody, err := json.Marshal(requestJson)
catchErr(err)

// Create the HTTP Request
req, err := http.NewRequest("POST", viper.GetString("printify_endpoint")+"/uploads/images.json", bytes.NewBuffer(requestBody))
catchErr(err)

// Set the HTTP Request Headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+PRINTIFY_API_KEY)

// Verbose Output
if verbose {
trace()
fmt.Println("🗂️ Request JSON:", requestJson)
fmt.Println("🌐 HTTP Request", req)
}

// Make the HTTP Request
httpMakeRequest(req, &responseJson)

fmt.Println("📤 Image Uploaded to Printify")
fmt.Println("📁 ID:", responseJson.ID)
fmt.Println("📁 Name:", responseJson.FileName)
fmt.Println("📁 Height:", responseJson.Height)
fmt.Println("📁 Width:", responseJson.Width)
fmt.Println("📁 Size:", responseJson.Size)
fmt.Println("📁 MimeType:", responseJson.MimeType)
fmt.Println("📁 PreviewURL:", responseJson.PreviewURL)
fmt.Println("📁 UploadTime:", responseJson.UploadTime)

}
17 changes: 17 additions & 0 deletions cmd/Printify_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

type PRINTIFY_ImageUploadRequest struct {
FileName string `json:"file_name"`
URL string `json:"url"`
}

type PRINTIFY_ImageUploadResponse struct {
ID string `json:"id"`
FileName string `json:"file_name"`
Height int `json:"height"`
Width int `json:"width"`
Size int `json:"size"`
MimeType string `json:"mime_type"`
PreviewURL string `json:"preview_url"`
UploadTime string `json:"upload_time"`
}
20 changes: 0 additions & 20 deletions cmd/consts.go

This file was deleted.

56 changes: 56 additions & 0 deletions cmd/http.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,70 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)

// Create HTTP Client
var httpClient = &http.Client{
Timeout: time.Second * 60,
}

func httpMakeRequest(request *http.Request, responseJson interface{}) {

// Make the HTTP Request
resp, err := httpClient.Do(request)
catchErr(err)

// Read the JSON Response Body
jsonString, err := io.ReadAll(resp.Body)
catchErr(err)

// Check for HTTP Errors
httpCatchErr(resp, jsonString)
if verbose {
b, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
fmt.Println("🌐 HTTP Response", b)
}

// Unmarshal the JSON Response Body into provided responseJson
err = json.Unmarshal([]byte(jsonString), &responseJson)
catchErr(err)
if verbose {
trace()
fmt.Println("🌐 HTTP Response String", string(jsonString))
fmt.Println("🌐 HTTP Response JSON", responseJson)
}
// Close the HTTP Response Body
defer resp.Body.Close()
}

func httpCatchErr(resp *http.Response, jsonString []byte) {
// Check for HTTP Response Errors
if resp.StatusCode != 200 {
catchErr(errors.New("API Error: " + strconv.Itoa(resp.StatusCode) + "\n" + string(jsonString)))
}
}

// func httpDumpRequest(r *http.Request) {
// // Dump the HTTP Request
// dump, err := httputil.DumpRequest(r, true)
// catchErr(err)
// fmt.Println("🌐 HTTP Request", string(dump))
// }

// download file from url and save to local directory
func httpDownloadFile(url string, filePath string) string {
// Replace spaces with underscores
Expand Down
16 changes: 15 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ var verbose,

var prompt,
configFile,
OPENAI_API_KEY string
OPENAI_API_KEY,
DISCORD_API_KEY,
PRINTIFY_API_KEY string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -84,6 +86,16 @@ func init() {
if OPENAI_API_KEY == "" && verbose {
fmt.Println("⚠️ OPENAI_API_KEY environment variable is not set, continuing without OpenAI API Key")
}

DISCORD_API_KEY = os.Getenv("DISCORD_API_KEY")
if DISCORD_API_KEY == "" && verbose {
fmt.Println("⚠️ DISCORD_API_KEY environment variable is not set, continuing without Discord API Key")
}

PRINTIFY_API_KEY = os.Getenv("PRINTIFY_API_KEY")
if PRINTIFY_API_KEY == "" && verbose {
fmt.Println("⚠️ PRINTIFY_API_KEY environment variable is not set, continuing without Printify API Key")
}
}

func viperConfig() {
Expand Down Expand Up @@ -115,6 +127,8 @@ func viperConfig() {

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

viper.SetDefault("printify_endpoint", "https://api.printify.com/v1/")

viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED the config file does not have an extension
viper.AddConfigPath(".") // look for config in the working directory
Expand Down
28 changes: 14 additions & 14 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ func syntaxHighlight(message string) {
}
}

func fileNameFromURL(urlStr string) string {
u, err := url.Parse(urlStr)
catchErr(err)
// Get the last path component of the URL
filename := filepath.Base(u.Path)
// Replace any characters that are not letters, numbers, or underscores with dashes
filename = regexp.MustCompile(`[^a-zA-Z0-9_]+`).ReplaceAllString(filename, "-")
// Limit the filename to 255 characters
if len(filename) >= 255 {
filename = filename[:255]
}
return filename
}

func catchErr(err error, level ...string) {
if err != nil {
// Default level is "warn" if none is provided
Expand All @@ -164,20 +178,6 @@ func formatPrompt(prompt string) string {
return regexp.MustCompile(`[^a-zA-Z0-9_]+`).ReplaceAllString(prompt, "-")
}

func fileNameFromURL(urlStr string) string {
u, err := url.Parse(urlStr)
catchErr(err)
// Get the last path component of the URL
filename := filepath.Base(u.Path)
// Replace any characters that are not letters, numbers, or underscores with dashes
filename = regexp.MustCompile(`[^a-zA-Z0-9_]+`).ReplaceAllString(filename, "-")
// Limit the filename to 255 characters
if len(filename) >= 255 {
filename = filename[:255]
}
return filename
}

func trace() {
pc := make([]uintptr, 10) // at least 1 entry needed
runtime.Callers(2, pc)
Expand Down
4 changes: 3 additions & 1 deletion files/config
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ discord_bot_systemMessage: |
Ponder is here to help you with your Discord needs.
Please be respectful and courteous when interacting with Ponder.
Ponder will not tolerate any form of harassment, bullying, or discrimination.
If you have any questions or concerns, please let us know. Thank you for using Ponder!
If you have any questions or concerns, please let us know. Thank you for using Ponder!

printify_endpoint: "https://api.printify.com/v1"

0 comments on commit 17aa0b6

Please sign in to comment.