|
| 1 | +package threads |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + "net/http" |
| 7 | + netURL "net/url" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/gocolly/colly/v2" |
| 12 | + "github.com/pkg/errors" |
| 13 | + |
| 14 | + "github.com/iawia002/lux/extractors" |
| 15 | + "github.com/iawia002/lux/request" |
| 16 | + "github.com/iawia002/lux/utils" |
| 17 | +) |
| 18 | + |
| 19 | +func init() { |
| 20 | + extractors.Register("threads", New()) |
| 21 | +} |
| 22 | + |
| 23 | +type extractor struct { |
| 24 | + client *http.Client |
| 25 | +} |
| 26 | + |
| 27 | +// New returns a instagram extractor. |
| 28 | +func New() extractors.Extractor { |
| 29 | + return &extractor{ |
| 30 | + client: &http.Client{ |
| 31 | + Timeout: 10 * time.Second, |
| 32 | + Transport: &http.Transport{ |
| 33 | + Dial: (&net.Dialer{ |
| 34 | + Timeout: 5 * time.Second, |
| 35 | + }).Dial, |
| 36 | + TLSHandshakeTimeout: 5 * time.Second, |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +type media struct { |
| 43 | + URL string |
| 44 | + Type extractors.DataType |
| 45 | +} |
| 46 | + |
| 47 | +// Extract is the main function to extract the data. |
| 48 | +func (e *extractor) Extract(url string, option extractors.Options) ([]*extractors.Data, error) { |
| 49 | + URL, err := netURL.Parse(url) |
| 50 | + if err != nil { |
| 51 | + return nil, errors.WithStack(err) |
| 52 | + } |
| 53 | + |
| 54 | + paths := strings.Split(URL.Path, "/") |
| 55 | + if len(paths) < 3 { |
| 56 | + return nil, errors.New("invalid URL format") |
| 57 | + } |
| 58 | + |
| 59 | + poster := paths[1] |
| 60 | + shortCode := paths[3] |
| 61 | + |
| 62 | + medias := make([]media, 0) |
| 63 | + |
| 64 | + title := fmt.Sprintf("Threads %s - %s", poster, shortCode) |
| 65 | + |
| 66 | + collector := colly.NewCollector() |
| 67 | + collector.SetClient(e.client) |
| 68 | + |
| 69 | + // case single image or video |
| 70 | + collector.OnHTML("div.SingleInnerMediaContainer", func(e *colly.HTMLElement) { |
| 71 | + if src := e.ChildAttr("img", "src"); src != "" { |
| 72 | + medias = append(medias, media{ |
| 73 | + URL: src, |
| 74 | + Type: extractors.DataTypeImage, |
| 75 | + }) |
| 76 | + } |
| 77 | + if src := e.ChildAttr("video > source", "src"); src != "" { |
| 78 | + medias = append(medias, media{ |
| 79 | + URL: src, |
| 80 | + Type: extractors.DataTypeVideo, |
| 81 | + }) |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + // case multiple image or video |
| 86 | + collector.OnHTML("div.MediaScrollImageContainer", func(e *colly.HTMLElement) { |
| 87 | + if src := e.ChildAttr("img", "src"); src != "" { |
| 88 | + medias = append(medias, media{ |
| 89 | + URL: src, |
| 90 | + Type: extractors.DataTypeImage, |
| 91 | + }) |
| 92 | + } |
| 93 | + if src := e.ChildAttr("video > source", "src"); src != "" { |
| 94 | + medias = append(medias, media{ |
| 95 | + URL: src, |
| 96 | + Type: extractors.DataTypeVideo, |
| 97 | + }) |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + // title with caption |
| 102 | + // collector.OnHTML("span.BodyTextContainer", func(e *colly.HTMLElement) { |
| 103 | + // title = e.Text |
| 104 | + // }) |
| 105 | + |
| 106 | + if err := collector.Visit(URL.JoinPath("embed").String()); err != nil { |
| 107 | + return nil, fmt.Errorf("failed to send HTTP request to the Threads: %w", errors.WithStack(err)) |
| 108 | + } |
| 109 | + |
| 110 | + var totalSize int64 |
| 111 | + var parts []*extractors.Part |
| 112 | + |
| 113 | + for _, m := range medias { |
| 114 | + _, ext, err := utils.GetNameAndExt(m.URL) |
| 115 | + if err != nil { |
| 116 | + return nil, errors.WithStack(err) |
| 117 | + } |
| 118 | + fileSize, err := request.Size(m.URL, url) |
| 119 | + if err != nil { |
| 120 | + return nil, errors.WithStack(err) |
| 121 | + } |
| 122 | + |
| 123 | + part := &extractors.Part{ |
| 124 | + URL: m.URL, |
| 125 | + Size: fileSize, |
| 126 | + Ext: ext, |
| 127 | + } |
| 128 | + parts = append(parts, part) |
| 129 | + } |
| 130 | + |
| 131 | + for _, part := range parts { |
| 132 | + totalSize += part.Size |
| 133 | + } |
| 134 | + |
| 135 | + streams := map[string]*extractors.Stream{ |
| 136 | + "default": { |
| 137 | + Parts: parts, |
| 138 | + Size: totalSize, |
| 139 | + }, |
| 140 | + } |
| 141 | + |
| 142 | + return []*extractors.Data{ |
| 143 | + { |
| 144 | + Site: "Threads www.threads.net", |
| 145 | + Title: title, |
| 146 | + Type: extractors.DataTypeImage, |
| 147 | + Streams: streams, |
| 148 | + URL: url, |
| 149 | + }, |
| 150 | + }, nil |
| 151 | +} |
0 commit comments