|
| 1 | +package forum |
| 2 | + |
| 3 | +import ( |
| 4 | + "darkbot/app/settings/logus" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + |
| 8 | + "github.com/anaskhan96/soup" |
| 9 | +) |
| 10 | + |
| 11 | +type Url string |
| 12 | + |
| 13 | +type ThreadLink Url |
| 14 | + |
| 15 | +func (u ThreadLink) GetUrl() Url { return Url(u) } |
| 16 | + |
| 17 | +type ThreadShortName string |
| 18 | +type ThreadID string |
| 19 | +type ForumTimestamp string |
| 20 | + |
| 21 | +type PostAuthorLink Url |
| 22 | + |
| 23 | +func (u PostAuthorLink) GetUrl() Url { return Url(u) } |
| 24 | + |
| 25 | +type PostAuthorName string |
| 26 | + |
| 27 | +type LatestThread struct { |
| 28 | + ThreadLink ThreadLink |
| 29 | + ThreadName ThreadShortName |
| 30 | + ThreadID ThreadID |
| 31 | + LastUpdated ForumTimestamp |
| 32 | + PostAuthorLink PostAuthorLink |
| 33 | + PostAuthorName PostAuthorName |
| 34 | +} |
| 35 | + |
| 36 | +type ThreadsPage struct { |
| 37 | + Threads []LatestThread |
| 38 | + requester func(MethodType, Url) (*QueryResult, error) |
| 39 | +} |
| 40 | + |
| 41 | +type threadPageParam func(thread_page *ThreadsPage) |
| 42 | + |
| 43 | +func WithMockedPageRequester( |
| 44 | + requester func(MethodType, Url) (*QueryResult, error), |
| 45 | +) threadPageParam { |
| 46 | + return func(thread_page *ThreadsPage) { |
| 47 | + thread_page.requester = requester |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +const ThreadPageURL Url = "https://discoverygc.com/forums/portal.php" |
| 52 | + |
| 53 | +func GetLatestThreads(opts ...threadPageParam) (*ThreadsPage, error) { |
| 54 | + thread_page := &ThreadsPage{ |
| 55 | + requester: NewQuery, |
| 56 | + } |
| 57 | + for _, opt := range opts { |
| 58 | + opt(thread_page) |
| 59 | + } |
| 60 | + |
| 61 | + query, err := thread_page.requester(GET, ThreadPageURL) |
| 62 | + if logus.CheckError(err, "Failed to make query") { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + content := query.GetContent() |
| 67 | + doc := soup.HTMLParse(content) |
| 68 | + forum_posts := doc.FindAll("tr", "class", "latestthreads_portal") |
| 69 | + |
| 70 | + for _, forum_post := range forum_posts { |
| 71 | + thread := forum_post.Find("strong").Find("a") |
| 72 | + if logus.CheckError(thread.Error, "failed to get thread object") { |
| 73 | + return nil, thread.Error |
| 74 | + } |
| 75 | + |
| 76 | + thread_link := thread.Attrs()["href"] |
| 77 | + thread_name := thread.Text() |
| 78 | + span_section := forum_post.Find("span") |
| 79 | + if logus.CheckError(span_section.Error, "failed to get span_section object") { |
| 80 | + return nil, span_section.Error |
| 81 | + } |
| 82 | + |
| 83 | + forum_timestamp := span_section.Find("span").Attrs()["title"] |
| 84 | + |
| 85 | + author := span_section.Find("a") |
| 86 | + if logus.CheckError(author.Error, "failed to get author object") { |
| 87 | + return nil, author.Error |
| 88 | + } |
| 89 | + author_link := author.Attrs()["href"] |
| 90 | + author_name := author.Text() |
| 91 | + |
| 92 | + myUrl, _ := url.Parse(thread_link) |
| 93 | + params, _ := url.ParseQuery(myUrl.RawQuery) |
| 94 | + |
| 95 | + latest_thread := LatestThread{ |
| 96 | + ThreadLink: ThreadLink(thread_link), |
| 97 | + ThreadName: ThreadShortName(thread_name), |
| 98 | + LastUpdated: ForumTimestamp(forum_timestamp), |
| 99 | + PostAuthorLink: PostAuthorLink(author_link), |
| 100 | + PostAuthorName: PostAuthorName(author_name), |
| 101 | + ThreadID: ThreadID(params["tid"][0]), |
| 102 | + } |
| 103 | + thread_page.Threads = append(thread_page.Threads, latest_thread) |
| 104 | + |
| 105 | + logus.Debug(fmt.Sprintf("latest_thread=%v", latest_thread)) |
| 106 | + } |
| 107 | + return thread_page, nil |
| 108 | +} |
0 commit comments