Skip to content

Instantly share code, notes, and snippets.

@abihf
Created July 6, 2024 19:36
Show Gist options
  • Select an option

  • Save abihf/5168e5d7e7d82d85e05084d5701af502 to your computer and use it in GitHub Desktop.

Select an option

Save abihf/5168e5d7e7d82d85e05084d5701af502 to your computer and use it in GitHub Desktop.
Download latest torrents from onejav
package main
import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"path/filepath"
"slices"
"strings"
"github.com/PuerkitoBio/goquery"
)
const (
baseURL = "https://onejav.com"
mainTag = "FC2" // they upload uncensored videos
maxPage = 5
maxItem = 10
)
var additionalTags = []string{
"Uncensored",
}
func main() {
if err := mainE(); err != nil {
panic(err)
}
}
func mainE() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ch := make(chan string, 10)
go getUrls(ctx, ch)
length := 0
for url := range ch {
if length > maxItem {
break
}
err := downloadFile(ctx, url)
if err != nil {
slog.Warn("failed to download file", "err", err)
continue
}
}
return nil
}
func downloadFile(ctx context.Context, url string) error {
fileName := filepath.Base(url)
_, err := os.Stat(fileName)
if err == nil || !os.IsNotExist(err) {
return fmt.Errorf("file already exists: %s", fileName)
}
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to get response: %w", err)
}
defer resp.Body.Close()
file, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}
func getUrls(ctx context.Context, ch chan string) {
defer close(ch)
page := 1
for {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://onejav.com/tag/FC2?page=%d", page), nil)
if err != nil {
slog.Error("failed to create request", "err", err)
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
slog.Error("failed to get response", "err", err)
return
}
parseDocument(resp.Body, ch)
if page > maxPage {
break
}
page++
}
}
func parseDocument(r io.ReadCloser, ch chan string) {
defer r.Close()
doc, err := goquery.NewDocumentFromReader(r)
if err != nil {
slog.Error("failed to parse document", "err", err)
return
}
nodes := doc.Find("div.card.mb-3")
nodes.Each(func(i int, s *goquery.Selection) {
shouldDownload := len(additionalTags) == 0
if !shouldDownload {
s.Find("div.tags a").Each(func(i int, s *goquery.Selection) {
tag := strings.TrimSpace(s.Text())
if slices.Contains(additionalTags, tag) {
shouldDownload = true
}
})
}
if !shouldDownload {
return
}
link := s.Find("a.button.is-primary").AttrOr("href", "")
ch <- baseURL + link
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment