tgchannel.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package getter
  2. import (
  3. "fmt"
  4. "sync"
  5. "github.com/zu1k/proxypool/tool"
  6. "github.com/gocolly/colly"
  7. "github.com/zu1k/proxypool/proxy"
  8. )
  9. func init() {
  10. Register("tgchannel", NewTGChannelGetter)
  11. }
  12. type TGChannelGetter struct {
  13. c *colly.Collector
  14. NumNeeded int
  15. results []string
  16. Url string
  17. }
  18. func NewTGChannelGetter(options tool.Options) Getter {
  19. num, found := options["num"]
  20. if !found || int(num.(float64)) <= 0 {
  21. num = 200
  22. }
  23. url, found := options["channel"]
  24. if found {
  25. return &TGChannelGetter{
  26. c: colly.NewCollector(),
  27. NumNeeded: int(num.(float64)),
  28. Url: "https://t.me/s/" + url.(string),
  29. }
  30. }
  31. return nil
  32. }
  33. func (g *TGChannelGetter) Get() []proxy.Proxy {
  34. g.results = make([]string, 0)
  35. // 找到所有的文字消息
  36. g.c.OnHTML("div.tgme_widget_message_text", func(e *colly.HTMLElement) {
  37. g.results = append(g.results, GrepLinksFromString(e.Text)...)
  38. })
  39. // 找到之前消息页面的链接,加入访问队列
  40. g.c.OnHTML("link[rel=prev]", func(e *colly.HTMLElement) {
  41. if len(g.results) < g.NumNeeded {
  42. _ = e.Request.Visit(e.Attr("href"))
  43. }
  44. })
  45. g.results = make([]string, 0)
  46. err := g.c.Visit(g.Url)
  47. if err != nil {
  48. _ = fmt.Errorf("%s", err.Error())
  49. }
  50. return StringArray2ProxyArray(g.results)
  51. }
  52. func (g *TGChannelGetter) Get2Chan(pc chan proxy.Proxy, wg *sync.WaitGroup) {
  53. wg.Add(1)
  54. nodes := g.Get()
  55. for _, node := range nodes {
  56. pc <- node
  57. }
  58. wg.Done()
  59. }