|
| 1 | +package engine |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + "path" |
| 7 | + "regexp" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/gocolly/colly/v2" |
| 12 | + log "github.com/sirupsen/logrus" |
| 13 | +) |
| 14 | + |
| 15 | +// DramaCool : An Engine for DramaCool |
| 16 | +type DramaCool struct { |
| 17 | + Props |
| 18 | +} |
| 19 | + |
| 20 | +// NewDramaCoolEngine : create a new engine for scraping latest korean drama |
| 21 | +func NewDramaCoolEngine() *DramaCool { |
| 22 | + base := "https://www.dramacool9.co" |
| 23 | + baseURL, err := url.Parse(base) |
| 24 | + if err != nil { |
| 25 | + log.Fatal(err) |
| 26 | + } |
| 27 | + // Search URL |
| 28 | + searchURL, err := url.Parse(base) |
| 29 | + if err != nil { |
| 30 | + log.Fatal(err) |
| 31 | + } |
| 32 | + searchURL.Path = "/" |
| 33 | + |
| 34 | + // List URL |
| 35 | + listURL, err := url.Parse(base) |
| 36 | + if err != nil { |
| 37 | + log.Fatal(err) |
| 38 | + } |
| 39 | + listURL.Path = "/category/latest-asian-drama-releases/" |
| 40 | + |
| 41 | + dramaCoolEngine := DramaCool{} |
| 42 | + dramaCoolEngine.Name = "DramaCool" |
| 43 | + dramaCoolEngine.BaseURL = baseURL |
| 44 | + dramaCoolEngine.Description = `Search from over 1000's of encoded anime available` |
| 45 | + dramaCoolEngine.SearchURL = searchURL |
| 46 | + dramaCoolEngine.ListURL = listURL |
| 47 | + return &dramaCoolEngine |
| 48 | +} |
| 49 | + |
| 50 | +// Engine Interface Methods |
| 51 | + |
| 52 | +func (engine *DramaCool) String() string { |
| 53 | + st := fmt.Sprintf("%s (%s)", engine.Name, engine.BaseURL) |
| 54 | + return st |
| 55 | +} |
| 56 | + |
| 57 | +func (engine *DramaCool) getParseAttrs() (string, string, error) { |
| 58 | + var ( |
| 59 | + article string |
| 60 | + main string |
| 61 | + ) |
| 62 | + switch engine.mode { |
| 63 | + case SearchMode: |
| 64 | + main = "ul.list-thumb" |
| 65 | + article = "li" |
| 66 | + case ListMode: |
| 67 | + main = "div#drama" |
| 68 | + article = "li" |
| 69 | + default: |
| 70 | + return "", "", fmt.Errorf("Invalid mode %v", engine.mode) |
| 71 | + } |
| 72 | + return main, article, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (engine *DramaCool) parseSingleMovie(el *colly.HTMLElement, index int) (Movie, error) { |
| 76 | + movie := Movie{ |
| 77 | + Index: index, |
| 78 | + IsSeries: true, |
| 79 | + Source: engine.Name, |
| 80 | + Size: "---MB", |
| 81 | + } |
| 82 | + movie.Title = strings.TrimSpace(el.ChildAttr("a", "title")) |
| 83 | + movie.CoverPhotoLink = el.ChildAttr("img", "src") |
| 84 | + re := regexp.MustCompile(`(-episode-.+)`) |
| 85 | + link := el.Request.AbsoluteURL(el.ChildAttr("a", "href")) |
| 86 | + downloadLink, err := url.Parse(link) |
| 87 | + remove := re.FindStringSubmatch(link) |
| 88 | + |
| 89 | + if len(remove) > 0 { |
| 90 | + downloadLink, err = url.Parse(strings.TrimSuffix(link, remove[0])) |
| 91 | + if err != nil { |
| 92 | + log.Fatal(err) |
| 93 | + } |
| 94 | + } |
| 95 | + movie.DownloadLink = downloadLink |
| 96 | + return movie, nil |
| 97 | +} |
| 98 | + |
| 99 | +func (engine *DramaCool) updateDownloadProps(downloadCollector *colly.Collector, movies *[]Movie) { |
| 100 | + downloadCollector.OnHTML("div#all-episodes", func(e *colly.HTMLElement) { |
| 101 | + log.Debug("got into all episodes") |
| 102 | + // movie := &(*movies)[getMovieIndexFromCtx(e.Request)] |
| 103 | + // episodeMap := map[string]*url.URL{} |
| 104 | + }) |
| 105 | + |
| 106 | + downloadCollector.OnHTML("div.drama-details", func(e *colly.HTMLElement) { |
| 107 | + log.Debug("got into all episodes") |
| 108 | + // episodeMap := map[string]*url.URL{} |
| 109 | + movie := &(*movies)[getMovieIndexFromCtx(e.Request)] |
| 110 | + description := e.ChildText("div.synopsis") |
| 111 | + re := regexp.MustCompile(`Genres: (.+)`) |
| 112 | + genres := re.FindStringSubmatch(e.ChildText("p.genres")) |
| 113 | + if len(genres) > 1 { |
| 114 | + movie.Category = genres[1] |
| 115 | + } |
| 116 | + re = regexp.MustCompile(`Release Year: (.+)`) |
| 117 | + year := re.FindStringSubmatch(e.ChildText("p.release-year")) |
| 118 | + if len(year) > 1 { |
| 119 | + year, err := strconv.Atoi(year[1]) |
| 120 | + if err == nil { |
| 121 | + movie.Year = year |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + re = regexp.MustCompile(`(?s)Synopsis:(.+)Also known as:`) |
| 126 | + desc := re.FindStringSubmatch(description) |
| 127 | + if len(desc) > 1 { |
| 128 | + movie.Description = strings.TrimSpace(desc[1]) |
| 129 | + } |
| 130 | + log.Debug(movie.Description) |
| 131 | + }) |
| 132 | +} |
| 133 | + |
| 134 | +// List : list all the movies on a page |
| 135 | +func (engine *DramaCool) List(page int) SearchResult { |
| 136 | + engine.mode = ListMode |
| 137 | + result := SearchResult{ |
| 138 | + Query: "List of Recent Uploads - Page " + strconv.Itoa(page), |
| 139 | + } |
| 140 | + pageParam := fmt.Sprintf("page/%v", strconv.Itoa(page)) |
| 141 | + engine.ListURL.Path = path.Join(engine.ListURL.Path, pageParam) |
| 142 | + movies, err := Scrape(engine) |
| 143 | + if err != nil { |
| 144 | + log.Fatal(err) |
| 145 | + } |
| 146 | + result.Movies = movies |
| 147 | + return result |
| 148 | +} |
| 149 | + |
| 150 | +// Search : Searches fzmovies for a particular query and return an array of movies |
| 151 | +func (engine *DramaCool) Search(param ...string) SearchResult { |
| 152 | + query := param[0] |
| 153 | + engine.mode = SearchMode |
| 154 | + result := SearchResult{ |
| 155 | + Query: query, |
| 156 | + } |
| 157 | + q := engine.SearchURL.Query() |
| 158 | + q.Set("s", query) |
| 159 | + engine.SearchURL.RawQuery = q.Encode() |
| 160 | + movies, err := Scrape(engine) |
| 161 | + if err != nil { |
| 162 | + log.Fatal(err) |
| 163 | + } |
| 164 | + result.Movies = movies |
| 165 | + return result |
| 166 | +} |
0 commit comments