Commit 0851addf64
Unsigned
Layout: unified · split
internal/curd.go +98 −1
| @@ -1121,7 +1121,11 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni | ||
| 1121 | 1121 | |
| 1122 | 1122 | // If it has a decent score (or there are very few options), let's verify with the exact AniList meta tag |
| 1123 | 1123 | if score >= 2 || len(animeList) <= 3 { |
| 1124 | animeUrl := fmt.Sprintf("https://animepahe.pw/anime/%s", opt.Key) | |
| 1124 | animeRef := parseAnimepaheProviderID(opt.Key) | |
| 1125 | if animeRef.Session == "" { | |
| 1126 | continue | |
| 1127 | } | |
| 1128 | animeUrl := fmt.Sprintf("https://animepahe.pw/anime/%s", animeRef.Session) | |
| 1125 | 1129 | Log(fmt.Sprintf("Fetching %s to check exact AniList meta tag...", animeUrl)) |
| 1126 | 1130 | req, _ := http.NewRequest("GET", animeUrl, nil) |
| 1127 | 1131 | req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") |
| @@ -1428,6 +1432,11 @@ func WriteTokenToFile(token string, filePath string) error { | ||
| 1428 | 1432 | } |
| 1429 | 1433 | |
| 1430 | 1434 | func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { |
| 1435 | if err := resolveRuntimeProviderID(userCurdConfig, anime); err != nil { | |
| 1436 | Log(fmt.Sprintf("Failed to resolve provider id: %v", err)) | |
| 1437 | CurdOut("Failed to resolve anime provider id: " + err.Error()) | |
| 1438 | os.Exit(1) | |
| 1439 | } | |
| 1431 | 1440 | |
| 1432 | 1441 | // Validate inputs |
| 1433 | 1442 | if anime.ProviderId == "" { |
| @@ -1450,6 +1459,16 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { | ||
| 1450 | 1459 | if err != nil { |
| 1451 | 1460 | linkErr := err |
| 1452 | 1461 | Log(fmt.Sprintf("GetEpisodeURL failed: %v", linkErr)) |
| 1462 | if reselectProviderAnime(userCurdConfig, anime, linkErr) { | |
| 1463 | link, err = GetEpisodeURL(*userCurdConfig, anime.ProviderId, anime.Ep.Number) | |
| 1464 | if err == nil { | |
| 1465 | Log(fmt.Sprintf("Successfully retrieved episode link after provider reselect. Links count: %d", len(link))) | |
| 1466 | anime.Ep.Links = link | |
| 1467 | goto episodeLinksReady | |
| 1468 | } | |
| 1469 | linkErr = err | |
| 1470 | Log(fmt.Sprintf("GetEpisodeURL still failed after provider reselect: %v", linkErr)) | |
| 1471 | } | |
| 1453 | 1472 | // If unable to get episode link automatically get manually |
| 1454 | 1473 | episodeList, listErr := EpisodesList(anime.ProviderId, userCurdConfig.SubOrDub) |
| 1455 | 1474 | if listErr != nil { |
| @@ -1486,6 +1505,7 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { | ||
| 1486 | 1505 | anime.Ep.Links = link |
| 1487 | 1506 | } |
| 1488 | 1507 | |
| 1508 | episodeLinksReady: | |
| 1489 | 1509 | if len(anime.Ep.Links) == 0 { |
| 1490 | 1510 | CurdOut("No episode links found") |
| 1491 | 1511 | os.Exit(1) |
| @@ -1561,6 +1581,83 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { | ||
| 1561 | 1581 | return mpvSocketPath |
| 1562 | 1582 | } |
| 1563 | 1583 | |
| 1584 | func resolveRuntimeProviderID(userCurdConfig *CurdConfig, anime *Anime) error { | |
| 1585 | if GetProvider().Name() != "animepahe" || anime == nil || anime.ProviderId == "" { | |
| 1586 | return nil | |
| 1587 | } | |
| 1588 | ||
| 1589 | provider, ok := GetProvider().(*AnimepaheProvider) | |
| 1590 | if !ok { | |
| 1591 | return nil | |
| 1592 | } | |
| 1593 | ||
| 1594 | query := GetAnimeName(*anime) | |
| 1595 | if query == "" { | |
| 1596 | query = anime.Title.Romaji | |
| 1597 | } | |
| 1598 | if query == "" { | |
| 1599 | query = anime.Title.English | |
| 1600 | } | |
| 1601 | ||
| 1602 | resolved, err := provider.ResolveProviderID(anime.ProviderId, query) | |
| 1603 | if err != nil { | |
| 1604 | return err | |
| 1605 | } | |
| 1606 | if resolved != "" && resolved != anime.ProviderId { | |
| 1607 | Log(fmt.Sprintf("Resolved Animepahe provider id %s to runtime id %s", anime.ProviderId, resolved)) | |
| 1608 | anime.ProviderId = resolved | |
| 1609 | anime.ProviderName = GetProvider().Name() | |
| 1610 | } | |
| 1611 | ||
| 1612 | return nil | |
| 1613 | } | |
| 1614 | ||
| 1615 | func reselectProviderAnime(userCurdConfig *CurdConfig, anime *Anime, reason error) bool { | |
| 1616 | if GetProvider().Name() != "animepahe" { | |
| 1617 | return false | |
| 1618 | } | |
| 1619 | ||
| 1620 | if reason != nil { | |
| 1621 | Log(fmt.Sprintf("Attempting Animepahe provider reselect after error: %v", reason)) | |
| 1622 | } | |
| 1623 | ||
| 1624 | query := GetAnimeName(*anime) | |
| 1625 | if query == "" { | |
| 1626 | query = anime.Title.Romaji | |
| 1627 | } | |
| 1628 | if query == "" { | |
| 1629 | query = anime.Title.English | |
| 1630 | } | |
| 1631 | if query == "" { | |
| 1632 | return false | |
| 1633 | } | |
| 1634 | ||
| 1635 | options, err := SearchAnime(query, userCurdConfig.SubOrDub) | |
| 1636 | if err != nil { | |
| 1637 | Log(fmt.Sprintf("Animepahe provider reselect search failed for %q: %v", query, err)) | |
| 1638 | return false | |
| 1639 | } | |
| 1640 | if len(options) == 0 { | |
| 1641 | Log(fmt.Sprintf("Animepahe provider reselect found no results for %q", query)) | |
| 1642 | return false | |
| 1643 | } | |
| 1644 | ||
| 1645 | CurdOut("The saved Animepahe mapping is stale. Please select the anime again.") | |
| 1646 | selected, err := DynamicSelect(options) | |
| 1647 | if err != nil || selected.Key == "-1" || selected.Key == "-2" || selected.Key == "" { | |
| 1648 | if err != nil { | |
| 1649 | Log(fmt.Sprintf("Animepahe provider reselect failed: %v", err)) | |
| 1650 | } | |
| 1651 | return false | |
| 1652 | } | |
| 1653 | ||
| 1654 | anime.ProviderId = selected.Key | |
| 1655 | anime.ProviderName = GetProvider().Name() | |
| 1656 | anime.Ep.NextEpisode = NextEpisode{} | |
| 1657 | Log(fmt.Sprintf("Updated Animepahe ProviderId to %s after stale mapping", anime.ProviderId)) | |
| 1658 | return true | |
| 1659 | } | |
| 1660 | ||
| 1564 | 1661 | func CheckAndDownloadFiles(storagePath string, filesToCheck []string) error { |
| 1565 | 1662 | // Create storage directory if it doesn't exist |
| 1566 | 1663 | storagePath = os.ExpandEnv(storagePath) |
internal/localTracking.go +23 −2
| @@ -38,6 +38,23 @@ func LocalAddAnime(databaseFile string, anilistID int, allanimeID string, watchi | ||
| 38 | 38 | } |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | func normalizeLocalProviderID(providerName, providerID, animeName string) string { | |
| 42 | if providerName != "animepahe" { | |
| 43 | return providerID | |
| 44 | } | |
| 45 | ||
| 46 | if providerID == "" { | |
| 47 | return "" | |
| 48 | } | |
| 49 | ||
| 50 | stableID := stableAnimepaheProviderID(providerID) | |
| 51 | if stableID != "" { | |
| 52 | return stableID | |
| 53 | } | |
| 54 | ||
| 55 | return providerID | |
| 56 | } | |
| 57 | ||
| 41 | 58 | // Function to delete an anime entry by Anilist ID and Allanime ID |
| 42 | 59 | func LocalDeleteAnime(databaseFile string, anilistID int, allanimeID string) { |
| 43 | 60 | animeList := [][]string{} |
| @@ -184,16 +201,19 @@ func GetAnimeName(anime Anime) string { | ||
| 184 | 201 | |
| 185 | 202 | // Function to update or add a new anime entry |
| 186 | 203 | func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, watchingEpisode int, playbackTime int, animeDuration int, animeName string, providerName string) error { |
| 204 | allanimeID = normalizeLocalProviderID(providerName, allanimeID, animeName) | |
| 187 | 205 | // Read existing entries |
| 188 | 206 | animeList := LocalGetAllAnime(databaseFile) |
| 189 | 207 | |
| 190 | 208 | // Find and update existing entry or add new one |
| 191 | 209 | updated := false |
| 192 | 210 | for i, anime := range animeList { |
| 193 | if anime.AnilistId == anilistID && anime.ProviderId == allanimeID { | |
| 211 | existingProviderID := normalizeLocalProviderID(anime.ProviderName, anime.ProviderId, GetAnimeName(anime)) | |
| 212 | if anime.AnilistId == anilistID && existingProviderID == allanimeID { | |
| 194 | 213 | animeList[i].Ep.Number = watchingEpisode |
| 195 | 214 | animeList[i].Ep.Player.PlaybackTime = playbackTime |
| 196 | 215 | animeList[i].Ep.Duration = animeDuration |
| 216 | animeList[i].ProviderId = allanimeID | |
| 197 | 217 | animeList[i].Title.English = animeName |
| 198 | 218 | animeList[i].Title.Romaji = animeName |
| 199 | 219 | animeList[i].ProviderName = providerName |
| @@ -234,9 +254,10 @@ func LocalUpdateAnime(databaseFile string, anilistID int, allanimeID string, wat | ||
| 234 | 254 | defer writer.Flush() |
| 235 | 255 | |
| 236 | 256 | for _, anime := range animeList { |
| 257 | providerID := normalizeLocalProviderID(anime.ProviderName, anime.ProviderId, GetAnimeName(anime)) | |
| 237 | 258 | record := []string{ |
| 238 | 259 | strconv.Itoa(anime.AnilistId), |
| 239 | anime.ProviderId, | |
| 260 | providerID, | |
| 240 | 261 | strconv.Itoa(anime.Ep.Number), |
| 241 | 262 | strconv.Itoa(anime.Ep.Player.PlaybackTime), |
| 242 | 263 | strconv.Itoa(anime.Ep.Duration), |
internal/provider_animepahe.go +110 −5
| @@ -143,6 +143,100 @@ type AnimepaheSearchItem struct { | ||
| 143 | 143 | Session string `json:"session"` |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | type animepaheAnimeRef struct { | |
| 147 | ReleaseID string | |
| 148 | Session string | |
| 149 | } | |
| 150 | ||
| 151 | func (r animepaheAnimeRef) apiID() string { | |
| 152 | if r.Session != "" { | |
| 153 | return r.Session | |
| 154 | } | |
| 155 | return r.ReleaseID | |
| 156 | } | |
| 157 | ||
| 158 | func formatAnimepaheProviderID(item AnimepaheSearchItem) string { | |
| 159 | if item.ID > 0 && item.Session != "" { | |
| 160 | return fmt.Sprintf("%d:%s", item.ID, item.Session) | |
| 161 | } | |
| 162 | if item.Session != "" { | |
| 163 | return item.Session | |
| 164 | } | |
| 165 | if item.ID > 0 { | |
| 166 | return strconv.Itoa(item.ID) | |
| 167 | } | |
| 168 | return "" | |
| 169 | } | |
| 170 | ||
| 171 | func parseAnimepaheProviderID(providerID string) animepaheAnimeRef { | |
| 172 | providerID = strings.TrimSpace(providerID) | |
| 173 | if providerID == "" { | |
| 174 | return animepaheAnimeRef{} | |
| 175 | } | |
| 176 | ||
| 177 | parts := strings.SplitN(providerID, ":", 2) | |
| 178 | if len(parts) == 2 && parts[0] != "" && parts[1] != "" { | |
| 179 | if _, err := strconv.Atoi(parts[0]); err == nil { | |
| 180 | return animepaheAnimeRef{ReleaseID: parts[0], Session: parts[1]} | |
| 181 | } | |
| 182 | } | |
| 183 | ||
| 184 | if _, err := strconv.Atoi(providerID); err == nil { | |
| 185 | return animepaheAnimeRef{ReleaseID: providerID} | |
| 186 | } | |
| 187 | ||
| 188 | return animepaheAnimeRef{Session: providerID} | |
| 189 | } | |
| 190 | ||
| 191 | func stableAnimepaheProviderID(providerID string) string { | |
| 192 | animeRef := parseAnimepaheProviderID(providerID) | |
| 193 | if animeRef.ReleaseID != "" { | |
| 194 | return animeRef.ReleaseID | |
| 195 | } | |
| 196 | return providerID | |
| 197 | } | |
| 198 | ||
| 199 | func (p *AnimepaheProvider) ResolveProviderID(providerID, query string) (string, error) { | |
| 200 | animeRef := parseAnimepaheProviderID(providerID) | |
| 201 | query = strings.TrimSpace(query) | |
| 202 | if query == "" { | |
| 203 | if animeRef.Session != "" { | |
| 204 | return providerID, nil | |
| 205 | } | |
| 206 | return "", fmt.Errorf("cannot resolve animepahe provider id %q without an anime title", providerID) | |
| 207 | } | |
| 208 | ||
| 209 | options, err := p.SearchAnime(query, "") | |
| 210 | if err != nil { | |
| 211 | if animeRef.Session != "" { | |
| 212 | Log(fmt.Sprintf("Animepahe provider id refresh failed for %q, using existing session: %v", providerID, err)) | |
| 213 | return providerID, nil | |
| 214 | } | |
| 215 | return "", err | |
| 216 | } | |
| 217 | ||
| 218 | for _, option := range options { | |
| 219 | item, ok := option.ExtraData.(AnimepaheSearchItem) | |
| 220 | if !ok { | |
| 221 | continue | |
| 222 | } | |
| 223 | ||
| 224 | if animeRef.ReleaseID != "" && strconv.Itoa(item.ID) == animeRef.ReleaseID { | |
| 225 | return formatAnimepaheProviderID(item), nil | |
| 226 | } | |
| 227 | if animeRef.Session != "" && item.Session == animeRef.Session { | |
| 228 | return formatAnimepaheProviderID(item), nil | |
| 229 | } | |
| 230 | } | |
| 231 | ||
| 232 | if animeRef.Session != "" { | |
| 233 | Log(fmt.Sprintf("Animepahe provider id %q was not found in search results for %q, using existing session", providerID, query)) | |
| 234 | return providerID, nil | |
| 235 | } | |
| 236 | ||
| 237 | return "", fmt.Errorf("animepahe id %q was not found in search results for %q", providerID, query) | |
| 238 | } | |
| 239 | ||
| 146 | 240 | type animepaheSearchResponse struct { |
| 147 | 241 | Total int `json:"total"` |
| 148 | 242 | PerPage int `json:"per_page"` |
| @@ -187,7 +281,7 @@ func (p *AnimepaheProvider) SearchAnime(query, mode string) ([]SelectionOption, | ||
| 187 | 281 | result = append(result, SelectionOption{ |
| 188 | 282 | Title: item.Title, |
| 189 | 283 | Label: label, |
| 190 | Key: item.Session, // Using session as ID | |
| 284 | Key: formatAnimepaheProviderID(item), | |
| 191 | 285 | Thumbnail: item.Poster, |
| 192 | 286 | ExtraData: item, |
| 193 | 287 | }) |
| @@ -213,12 +307,17 @@ func (p *AnimepaheProvider) EpisodesList(showID, mode string) ([]string, error) | ||
| 213 | 307 | if err := p.ensureBypass(); err != nil { |
| 214 | 308 | return nil, err |
| 215 | 309 | } |
| 216 | // showID here is the session ID for animepahe | |
| 310 | animeRef := parseAnimepaheProviderID(showID) | |
| 311 | apiID := animeRef.apiID() | |
| 312 | if apiID == "" { | |
| 313 | return nil, fmt.Errorf("animepahe provider id %q is invalid; please reselect this anime", showID) | |
| 314 | } | |
| 315 | ||
| 217 | 316 | var allEpisodes []int |
| 218 | 317 | page := 1 |
| 219 | 318 | |
| 220 | 319 | for { |
| 221 | epUrl := fmt.Sprintf("https://animepahe.pw/api?m=release&id=%s&sort=episode_asc&page=%d", showID, page) | |
| 320 | epUrl := fmt.Sprintf("https://animepahe.pw/api?m=release&id=%s&sort=episode_asc&page=%d", apiID, page) | |
| 222 | 321 | req, _ := http.NewRequest("GET", epUrl, nil) |
| 223 | 322 | req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") |
| 224 | 323 | req.Header.Set("Referer", "https://animepahe.pw/") |
| @@ -265,6 +364,12 @@ func (p *AnimepaheProvider) GetEpisodeURL(config CurdConfig, id string, epNo int | ||
| 265 | 364 | if err := p.ensureBypass(); err != nil { |
| 266 | 365 | return nil, err |
| 267 | 366 | } |
| 367 | animeRef := parseAnimepaheProviderID(id) | |
| 368 | if animeRef.Session == "" { | |
| 369 | return nil, fmt.Errorf("animepahe provider id %q is missing session id; please reselect this anime", id) | |
| 370 | } | |
| 371 | apiID := animeRef.apiID() | |
| 372 | ||
| 268 | 373 | // 1. Get episode session ID |
| 269 | 374 | // Map the 1-based Anilist episode number to Animepahe's actual episode number |
| 270 | 375 | eps, err := p.EpisodesList(id, "") |
| @@ -284,7 +389,7 @@ func (p *AnimepaheProvider) GetEpisodeURL(config CurdConfig, id string, epNo int | ||
| 284 | 389 | var episodeSession string |
| 285 | 390 | page := 1 |
| 286 | 391 | for { |
| 287 | reqUrl := fmt.Sprintf("https://animepahe.pw/api?m=release&id=%s&sort=episode_asc&page=%d", id, page) | |
| 392 | reqUrl := fmt.Sprintf("https://animepahe.pw/api?m=release&id=%s&sort=episode_asc&page=%d", apiID, page) | |
| 288 | 393 | req, _ := http.NewRequest("GET", reqUrl, nil) |
| 289 | 394 | req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") |
| 290 | 395 | req.Header.Set("Referer", "https://animepahe.pw/") |
| @@ -327,7 +432,7 @@ func (p *AnimepaheProvider) GetEpisodeURL(config CurdConfig, id string, epNo int | ||
| 327 | 432 | |
| 328 | 433 | // player page: https://animepahe.pw/play/<anime_session>/<episode_session> |
| 329 | 434 | // stream links in player page: <button type=\"button\" data-src=\"https://kwik.cx/e/Jwd0hMNswksj\" |
| 330 | playerUrl := fmt.Sprintf("https://animepahe.pw/play/%s/%s", id, episodeSession) | |
| 435 | playerUrl := fmt.Sprintf("https://animepahe.pw/play/%s/%s", animeRef.Session, episodeSession) | |
| 331 | 436 | req, _ := http.NewRequest("GET", playerUrl, nil) |
| 332 | 437 | req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") |
| 333 | 438 | req.Header.Set("Referer", "https://animepahe.pw/") |