Skip to content

Instantly share code, notes, and snippets.

@Nuxij
Created June 1, 2022 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nuxij/ceb43792a3346692f31c4463703a4039 to your computer and use it in GitHub Desktop.
Save Nuxij/ceb43792a3346692f31c4463703a4039 to your computer and use it in GitHub Desktop.
Get latest trailers from filmjabber.com
package main
import (
"fmt"
"io"
"log"
"net/url"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/kennygrant/sanitize"
youtube "github.com/kkdai/youtube/v2"
"github.com/mmcdole/gofeed"
)
var (
MoviesPath = "M:/Films"
)
func main() {
fp := gofeed.NewParser()
feed, _ := fp.ParseURL("http://feeds.filmjabber.com/Movie-sourcecomMovieTrailers")
fmt.Println(feed.Title)
for _, item := range feed.Items {
film := strings.TrimSpace(strings.Replace(item.Title, "Movie Trailer", "", -1))
film = strings.TrimSpace(strings.Replace(film, ":", " -", -1))
err := os.MkdirAll(MoviesPath+"/"+film+"/Trailers", 0777)
if err != nil {
log.Fatal(err)
}
fmt.Println(film)
fmt.Println(item.Link)
postScrape(film, item.Link)
return
}
}
func postScrape(film, rssFeed string) {
doc, err := goquery.NewDocument(rssFeed)
if err != nil {
log.Fatal(err)
}
doc.Find("#wrapper .video-container iframe").Each(func(index int, item *goquery.Selection) {
link, _ := item.Attr("src")
linkUrl, err := url.Parse(link)
if err != nil {
return
}
fmt.Printf("Post #%d: %s\n", index, linkUrl.Path)
ExampleClient(film, index, strings.Replace(linkUrl.Path, "/embed/", "", 1))
})
}
func ExampleClient(film string, trailer int, videoID string) error {
client := youtube.Client{}
video, err := client.GetVideo(videoID)
if err != nil {
fmt.Println(err)
return err
}
formats := video.Formats.WithAudioChannels() // only get videos with audio
stream, _, err := client.GetStream(video, &formats[0])
if err != nil {
fmt.Println(err)
return err
}
file, err := os.Create(fmt.Sprintf("%s/%s/Trailers/%s #%d.mp4", MoviesPath, film, sanitize.BaseName(video.Title), trailer))
if err != nil {
fmt.Println(err)
return err
}
defer file.Close()
_, err = io.Copy(file, stream)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment