Skip to content

Instantly share code, notes, and snippets.

@AmarnathCJD
Last active October 12, 2022 17:59
Show Gist options
  • Save AmarnathCJD/64a414cd12fbe3dd26758ecab412c136 to your computer and use it in GitHub Desktop.
Save AmarnathCJD/64a414cd12fbe3dd26758ecab412c136 to your computer and use it in GitHub Desktop.
// (c) RoseLoverX 2022
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
)
type Anime struct {
Title string `json:"title,omitempty"`
Session string `json:"session_id,omitempty"`
SnapShot string `json:"snapshot,omitempty"`
ID float64 `json:"id,omitempty"`
}
type URL struct {
URL string `json:"url"`
Quality string `json:"quality"`
Size string `json:"size"`
}
type AnimePahe struct {
baseURL string
h *http.Client
}
func (a *Anime) GetLinks() ([]URL, error) {
return NewAnimePahe().GetAnimeLinks(*a)
}
func (u *URL) M3U8() (string, error) {
req, err := http.NewRequest("GET", u.URL, nil)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0")
req.Header.Set("Referer", "https://animepahe.com/")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
video_id_re := regexp.MustCompile(`\|uwu\|([a-zA-Z0-9]+)\|`)
video_id := video_id_re.FindStringSubmatch(string(body))
if len(video_id) < 2 {
return "", fmt.Errorf("video id not found")
}
return fmt.Sprintf("https://eu-991.files.nextcdn.org/stream/01/%s/uwu.m3u8", video_id[1]), nil
}
func NewAnimePahe() *AnimePahe {
return &AnimePahe{
baseURL: "https://animepahe.com",
h: &http.Client{},
}
}
func (p *AnimePahe) GetLatestAnime() ([]Anime, error) {
url := fmt.Sprintf("%s/api?m=airing&page=1", p.baseURL)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0")
resp, err := p.h.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var animes []Anime
var data map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
for _, v := range data["data"].([]interface{}) {
anime := v.(map[string]interface{})
animes = append(animes, Anime{
Title: anime["anime_title"].(string),
Session: anime["session"].(string),
ID: anime["anime_id"].(float64),
SnapShot: anime["snapshot"].(string),
})
}
return animes, nil
}
func (p *AnimePahe) GetAnimeLinks(a Anime) ([]URL, error) {
URI, _ := url.Parse(fmt.Sprintf("%s/api", p.baseURL))
q := URI.Query()
q.Set("m", "links")
q.Set("id", a.Session)
q.Set("p", "kwik")
URI.RawQuery = q.Encode()
req, err := http.NewRequest("GET", URI.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0")
resp, err := p.h.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
var urls []URL
for _, v := range data["data"].([]interface{}) {
vx := v.(map[string]interface{})
// key is the quality
for k, v := range vx {
urls = append(urls, URL{
URL: v.(map[string]interface{})["kwik"].(string),
Quality: k,
})
}
}
return urls, nil
}
func main() {
p := NewAnimePahe()
animes, _ := p.GetLatestAnime()
for _, anime := range animes {
fmt.Println(anime.Title)
links, _ := anime.GetLinks()
for _, link := range links {
q, _ := link.M3U8()
fmt.Println("Quality:", link.Quality, ", URL:", q)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment