Skip to content

Commit

Permalink
feat: search usage type, function and command
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsm-dev committed Jan 3, 2022
1 parent 82576df commit 864eabb
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
20 changes: 20 additions & 0 deletions commands/search_usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package commands

import (
"github.com/lpmatos/loli/internal/trace"
"github.com/spf13/cobra"
)

// SearchMeCmd represents the search command
var SearchMeCmd = &cobra.Command{
Use: "usage",
Short: "Check the search quota and limit for your account (with API key) or IP address (without API key)",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
trace.SearchUsage(searchPretty)
},
}

func init() {
SearchCmd.AddCommand(SearchMeCmd)
}
3 changes: 3 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const (
// TraceMoeSearchAnimeByLink default trace.moe URL
TraceMoeSearchAnimeByLink = "https://api.trace.moe/search?anilistInfo&url="

// TraceMoeUsage default trace.moe URL
TraceMoeUsage = "https://api.trace.moe/me"

// ReleaseURL default release URL.
ReleaseURL = "https://api.github.com/repos/lpmatos/loli/releases"

Expand Down
61 changes: 61 additions & 0 deletions internal/trace/search_usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package trace

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"

"github.com/fatih/color"
"github.com/jedib0t/go-pretty/table"
"github.com/lpmatos/loli/internal/constants"
log "github.com/lpmatos/loli/internal/log"
"github.com/lpmatos/loli/internal/types"
)

// SearchUsage function
func SearchUsage(pretty bool) {
searchURL := constants.TraceMoeUsage
log.Infoln(searchURL)

resp, error := http.Get(searchURL)
if error != nil {
log.Fatalln(error)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
log.Fatalf("Bad status code - %d", resp.StatusCode)
}

content, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}

var usageResp types.UsageTraceMoe
json.Unmarshal(content, &usageResp)

if pretty {
versionTable := table.NewWriter()
versionTable.SetOutputMirror(os.Stdout)
versionTable.AppendHeader(table.Row{"Info", "Content"})
versionTable.AppendRows([]table.Row{
{"πŸ’» IP", usageResp.ID},
{"🧾 Priority", strconv.Itoa(usageResp.Priority)},
{"πŸ“š Concurrency", strconv.Itoa(usageResp.Concurrency)},
{"πŸ“‚ Quota", strconv.Itoa(usageResp.Quota)},
{"πŸ“ QuotaUsed", color.MagentaString(strconv.Itoa(usageResp.QuotaUsed))},
})
versionTable.SetStyle(table.StyleColoredBlueWhiteOnBlack)
versionTable.Render()
} else {
fmt.Println("πŸ’» IP: " + usageResp.ID)
fmt.Println("🧾 Priority: " + strconv.Itoa(usageResp.Priority))
fmt.Println("πŸ“š Concurrency: " + strconv.Itoa(usageResp.Concurrency))
fmt.Println("πŸ“‚ Quota: " + strconv.Itoa(usageResp.Quota))
fmt.Println("πŸ“ QuotaUsed: " + color.MagentaString(strconv.Itoa(usageResp.QuotaUsed)))
}
}
9 changes: 9 additions & 0 deletions internal/types/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ type Response struct {
Image string `json:"image"`
} `json:"result"`
}

// UsageTraceMoe struct - content of request usage to trace moe.
type UsageTraceMoe struct {
ID string `json:"id"`
Priority int `json:"priority"`
Concurrency int `json:"concurrency"`
Quota int `json:"quota"`
QuotaUsed int `json:"quotaUsed"`
}

0 comments on commit 864eabb

Please sign in to comment.