|
| 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 | +// CoolMoviez : An Engine for CoolMoviez |
| 16 | +type CoolMoviez struct { |
| 17 | + Props |
| 18 | +} |
| 19 | + |
| 20 | +// NewCoolMoviezEngine : create a new engine for scraping mynewcoolmovies |
| 21 | +func NewCoolMoviezEngine() *CoolMoviez { |
| 22 | + base := "https://coolmoviez.buzz" |
| 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 = "/mobile/search" |
| 33 | + |
| 34 | + // List URL |
| 35 | + listURL, err := url.Parse(base) |
| 36 | + if err != nil { |
| 37 | + log.Fatal(err) |
| 38 | + } |
| 39 | + listURL.Path = "/movielist/13/Hollywood_movies/default" |
| 40 | + |
| 41 | + coolMoviesEngine := CoolMoviez{} |
| 42 | + coolMoviesEngine.Name = "CoolMoviez" |
| 43 | + coolMoviesEngine.BaseURL = baseURL |
| 44 | + coolMoviesEngine.Description = `Self reported best download site for mobile, tablets and pc` |
| 45 | + coolMoviesEngine.SearchURL = searchURL |
| 46 | + coolMoviesEngine.ListURL = listURL |
| 47 | + return &coolMoviesEngine |
| 48 | +} |
| 49 | + |
| 50 | +// Engine Interface Methods |
| 51 | + |
| 52 | +func (engine *CoolMoviez) String() string { |
| 53 | + st := fmt.Sprintf("%s (%s)", engine.Name, engine.BaseURL) |
| 54 | + return st |
| 55 | +} |
| 56 | + |
| 57 | +func (engine *CoolMoviez) getParseAttrs() (string, string, error) { |
| 58 | + return "div.list", "div.fl", nil |
| 59 | +} |
| 60 | + |
| 61 | +func (engine *CoolMoviez) parseSingleMovie(el *colly.HTMLElement, index int) (Movie, error) { |
| 62 | + movie := Movie{ |
| 63 | + Index: index, |
| 64 | + IsSeries: false, |
| 65 | + Source: engine.Name, |
| 66 | + } |
| 67 | + movie.Title = strings.TrimSpace(el.ChildText("a.fileName")) |
| 68 | + appendage := el.ChildText("span") |
| 69 | + if strings.HasSuffix(movie.Title, appendage) { |
| 70 | + movie.Title = strings.TrimSuffix(movie.Title, appendage) |
| 71 | + } |
| 72 | + re := regexp.MustCompile(`(\d+)`) |
| 73 | + stringsub := re.FindStringSubmatch(movie.Title) |
| 74 | + if len(stringsub) > 0 { |
| 75 | + year, _ := strconv.Atoi(stringsub[0]) |
| 76 | + movie.Year = year |
| 77 | + } |
| 78 | + downloadLink, err := url.Parse(el.Request.AbsoluteURL(el.ChildAttr("a", "href"))) |
| 79 | + movie.CoverPhotoLink = el.ChildAttr("img", "src") |
| 80 | + |
| 81 | + if err != nil { |
| 82 | + log.Fatal(err) |
| 83 | + } |
| 84 | + |
| 85 | + movie.DownloadLink = downloadLink |
| 86 | + return movie, nil |
| 87 | +} |
| 88 | + |
| 89 | +func (engine *CoolMoviez) updateDownloadProps(downloadCollector *colly.Collector, movies *[]Movie) { |
| 90 | + |
| 91 | + downloadCollector.OnHTML("div.M1,div.M2", func(e *colly.HTMLElement) { |
| 92 | + reArray := []string{"Quality", "Genre", "Description", "Starcast"} |
| 93 | + movie := &(*movies)[getMovieIndexFromCtx(e.Request)] |
| 94 | + for _, reString := range reArray { |
| 95 | + re := regexp.MustCompile(reString + `:\s+(.*)`) |
| 96 | + stringsub := re.FindStringSubmatch(e.Text) |
| 97 | + if len(stringsub) > 1 { |
| 98 | + value := stringsub[1] |
| 99 | + switch reString { |
| 100 | + case "Quality": |
| 101 | + movie.Quality = value |
| 102 | + case "Description": |
| 103 | + movie.Description = value |
| 104 | + case "Genre": |
| 105 | + movie.Category = value |
| 106 | + case "Starcast": |
| 107 | + movie.Cast = value |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + downloadCollector.OnHTML("a.fileName", func(e *colly.HTMLElement) { |
| 114 | + movie := &(*movies)[getMovieIndexFromCtx(e.Request)] |
| 115 | + initialLink := e.Attr("href") |
| 116 | + |
| 117 | + replacePrefix := "https://www.coolmoviez.buzz/file" |
| 118 | + if strings.HasPrefix(initialLink, replacePrefix) { |
| 119 | + downloadLink, err := url.Parse("https://www.coolmoviez.buzz/server" + strings.TrimPrefix(initialLink, replacePrefix)) |
| 120 | + if err == nil { |
| 121 | + movie.DownloadLink = downloadLink |
| 122 | + downloadCollector.Visit(downloadLink.String()) |
| 123 | + } |
| 124 | + } |
| 125 | + }) |
| 126 | + |
| 127 | + downloadCollector.OnHTML("a.dwnLink", func(e *colly.HTMLElement) { |
| 128 | + movie := &(*movies)[getMovieIndexFromCtx(e.Request)] |
| 129 | + downloadLink, err := url.Parse(e.Attr("href")) |
| 130 | + if err == nil { |
| 131 | + movie.DownloadLink = downloadLink |
| 132 | + } |
| 133 | + }) |
| 134 | +} |
| 135 | + |
| 136 | +// List : list all the movies on a page |
| 137 | +func (engine *CoolMoviez) List(page int) SearchResult { |
| 138 | + engine.mode = ListMode |
| 139 | + result := SearchResult{ |
| 140 | + Query: "List of Recent Uploads - Page " + strconv.Itoa(page), |
| 141 | + } |
| 142 | + pageParam := fmt.Sprintf("%v.html", strconv.Itoa(page)) |
| 143 | + engine.ListURL.Path = path.Join(engine.ListURL.Path, pageParam) + "/" |
| 144 | + movies, err := Scrape(engine) |
| 145 | + if err != nil { |
| 146 | + log.Fatal(err) |
| 147 | + } |
| 148 | + result.Movies = movies |
| 149 | + return result |
| 150 | +} |
| 151 | + |
| 152 | +// Search : Searches fzmovies for a particular query and return an array of movies |
| 153 | +func (engine *CoolMoviez) Search(param ...string) SearchResult { |
| 154 | + query := param[0] |
| 155 | + engine.mode = SearchMode |
| 156 | + result := SearchResult{ |
| 157 | + Query: query, |
| 158 | + } |
| 159 | + q := engine.SearchURL.Query() |
| 160 | + q.Set("find", query) |
| 161 | + q.Set("per_page", "1") |
| 162 | + engine.SearchURL.RawQuery = q.Encode() |
| 163 | + movies, err := Scrape(engine) |
| 164 | + if err != nil { |
| 165 | + log.Fatal(err) |
| 166 | + } |
| 167 | + result.Movies = movies |
| 168 | + return result |
| 169 | +} |
0 commit comments