Skip to content

Commit 248074c

Browse files
fix: handle is_official type changes in comix provider and fix manga image rendering
1 parent f711520 commit 248074c

File tree

3 files changed

+135
-10
lines changed

3 files changed

+135
-10
lines changed

internal/providers/manga/comix/comix.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (c *Comix) GetInfo(id string) (interface{}, error) {
192192
ChapterID int `json:"chapter_id"`
193193
Number interface{} `json:"number"`
194194
Name string `json:"name"`
195-
IsOfficial int `json:"is_official"`
195+
IsOfficial any `json:"is_official"`
196196
ScanlationGroup struct {
197197
Name string `json:"name"`
198198
} `json:"scanlation_group"`
@@ -219,7 +219,7 @@ func (c *Comix) GetInfo(id string) (interface{}, error) {
219219
ChapterID int `json:"chapter_id"`
220220
Number interface{} `json:"number"`
221221
Name string `json:"name"`
222-
IsOfficial int `json:"is_official"`
222+
IsOfficial any `json:"is_official"`
223223
ScanlationGroup struct {
224224
Name string `json:"name"`
225225
} `json:"scanlation_group"`
@@ -248,18 +248,28 @@ func (c *Comix) GetInfo(id string) (interface{}, error) {
248248
ChapterID int
249249
Number interface{}
250250
Name string
251-
IsOfficial int
251+
IsOfficial bool
252252
ScanlationGroup string
253253
}
254254
chapterMap := make(map[string][]ChapterItem)
255255

256256
for _, item := range allChapters {
257+
isOfficial := false
258+
switch v := item.IsOfficial.(type) {
259+
case bool:
260+
isOfficial = v
261+
case float64:
262+
isOfficial = v == 1
263+
case int:
264+
isOfficial = v == 1
265+
}
266+
257267
numStr := fmt.Sprintf("%v", item.Number)
258268
chapterMap[numStr] = append(chapterMap[numStr], ChapterItem{
259269
ChapterID: item.ChapterID,
260270
Number: item.Number,
261271
Name: item.Name,
262-
IsOfficial: item.IsOfficial,
272+
IsOfficial: isOfficial,
263273
ScanlationGroup: item.ScanlationGroup.Name,
264274
})
265275
}
@@ -288,7 +298,7 @@ func (c *Comix) GetInfo(id string) (interface{}, error) {
288298

289299
// 1. Try Official
290300
for _, item := range items {
291-
if item.IsOfficial == 1 {
301+
if item.IsOfficial {
292302
selectedItem = item
293303
found = true
294304
break
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package comix
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/justchokingaround/greg/internal/providers"
9+
)
10+
11+
func TestChainsawMan(t *testing.T) {
12+
c := New()
13+
14+
ctx := context.Background()
15+
16+
// Step 1: Search for Chainsaw Man
17+
t.Log("=== Step 1: Searching for Chainsaw Man ===")
18+
results, err := c.Search(ctx, "chainsaw man")
19+
if err != nil {
20+
t.Fatalf("Search failed: %v", err)
21+
}
22+
if len(results) == 0 {
23+
t.Fatal("No results found")
24+
}
25+
26+
var chainsawMan providers.Media
27+
for _, r := range results {
28+
t.Logf("Found: %s (ID: %s)", r.Title, r.ID)
29+
if r.Title == "Chainsaw Man" {
30+
chainsawMan = r
31+
break
32+
}
33+
}
34+
if chainsawMan.Title == "" {
35+
chainsawMan = results[0]
36+
}
37+
t.Logf("Selected: %s (ID: %s)", chainsawMan.Title, chainsawMan.ID)
38+
39+
// Step 2: Get manga details (includes chapters)
40+
t.Log("=== Step 2: Getting manga details ===")
41+
details, err := c.GetMediaDetails(ctx, chainsawMan.ID)
42+
if err != nil {
43+
t.Fatalf("GetMediaDetails failed: %v", err)
44+
}
45+
t.Logf("Title: %s", details.Title)
46+
t.Logf("Synopsis: %s", details.Synopsis)
47+
t.Logf("Status: %s", details.Status)
48+
t.Logf("Seasons: %d", len(details.Seasons))
49+
50+
// Step 3: Get chapters (episodes)
51+
t.Log("=== Step 3: Getting chapters ===")
52+
episodes, err := c.GetEpisodes(ctx, chainsawMan.ID)
53+
if err != nil {
54+
t.Fatalf("GetEpisodes failed: %v", err)
55+
}
56+
t.Logf("Found %d chapters", len(episodes))
57+
if len(episodes) == 0 {
58+
t.Fatal("No chapters found")
59+
}
60+
61+
// Print first 5 chapters
62+
for i, ep := range episodes {
63+
if i >= 5 {
64+
break
65+
}
66+
t.Logf(" Chapter %d: %s (ID: %s)", ep.Number, ep.Title, ep.ID)
67+
}
68+
69+
// Step 4: Get manga pages for first chapter
70+
t.Log("=== Step 4: Getting manga pages ===")
71+
pages, err := c.GetMangaPages(ctx, episodes[0].ID)
72+
if err != nil {
73+
t.Fatalf("GetMangaPages failed: %v", err)
74+
}
75+
t.Logf("Found %d pages", len(pages))
76+
if len(pages) == 0 {
77+
t.Fatal("No pages found")
78+
}
79+
80+
// Print first page URL
81+
fmt.Printf("First page URL: %s\n", pages[0])
82+
83+
t.Log("=== All tests passed! ===")
84+
}

internal/tui/components/manga/model.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,23 +580,54 @@ func (m *Model) renderPage() tea.Cmd {
580580

581581
return func() tea.Msg {
582582
// Create a temp file
583-
tmpFile, err := os.CreateTemp("", "greg-manga-*.jpg")
583+
tmpFile, err := os.CreateTemp("", "greg-manga-*.png")
584584
if err != nil {
585585
return PageRenderedMsg{Err: fmt.Errorf("failed to create temp file: %w", err)}
586586
}
587-
defer func() { _ = os.Remove(tmpFile.Name()) }()
587+
tmpFileName := tmpFile.Name()
588+
defer func() { _ = os.Remove(tmpFileName) }()
588589

589590
// Download the image
590-
resp, err := http.Get(url)
591+
client := &http.Client{}
592+
req, err := http.NewRequest("GET", url, nil)
593+
if err != nil {
594+
return PageRenderedMsg{Err: fmt.Errorf("failed to create request: %w", err)}
595+
}
596+
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
597+
req.Header.Set("Accept", "image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8")
598+
req.Header.Set("Accept-Language", "en-US,en;q=0.9")
599+
req.Header.Set("Referer", "https://comix.to/")
600+
601+
resp, err := client.Do(req)
591602
if err != nil {
592603
return PageRenderedMsg{Err: fmt.Errorf("failed to download image: %w", err)}
593604
}
594605
defer func() { _ = resp.Body.Close() }()
595606

596-
_, err = io.Copy(tmpFile, resp.Body)
607+
if resp.StatusCode != http.StatusOK {
608+
return PageRenderedMsg{Err: fmt.Errorf("image download returned status %d", resp.StatusCode)}
609+
}
610+
611+
downloadFile, err := os.CreateTemp("", "greg-manga-download-*.img")
612+
if err != nil {
613+
return PageRenderedMsg{Err: fmt.Errorf("failed to create download temp file: %w", err)}
614+
}
615+
downloadFileName := downloadFile.Name()
616+
defer func() { _ = os.Remove(downloadFileName) }()
617+
618+
_, err = io.Copy(downloadFile, resp.Body)
597619
if err != nil {
598620
return PageRenderedMsg{Err: fmt.Errorf("failed to save image: %w", err)}
599621
}
622+
_ = downloadFile.Close()
623+
624+
// Convert image to PNG if needed (WebP, AVIF, etc.)
625+
convertCmd := exec.Command("magick", downloadFileName, tmpFileName)
626+
if err := convertCmd.Run(); err != nil {
627+
// If convert fails, try using the original file
628+
tmpFileName = downloadFileName
629+
}
630+
600631
_ = tmpFile.Close()
601632

602633
// Run chafa
@@ -621,7 +652,7 @@ func (m *Model) renderPage() tea.Cmd {
621652
// "symbols" or others will use default (no -f flag or specific one if needed, but chafa defaults to symbols)
622653

623654
// Add file path
624-
args = append(args, tmpFile.Name())
655+
args = append(args, tmpFileName)
625656

626657
cmd := exec.Command("chafa", args...)
627658
output, err := cmd.Output()

0 commit comments

Comments
 (0)