Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions server/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ func unmarshalActivity(key, value []byte) (Activity, error) {
if err != nil {
return nil, err
}
if post.Host == "" {
post.Host = "www.pathofexile.com"
}
if post.Id != 0 {
return post, nil
}
Expand Down
2 changes: 0 additions & 2 deletions server/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ func testDatabase_ForumPosts(t *testing.T, db Database) {
Id: 9000,
Poster: "Chris",
Time: time.Unix(1486332365, 0),
Host: locale.ForumHost(),
}

post2 := &ForumPost{
Id: 9001,
Poster: "Chris",
Time: time.Unix(1486332364, 0),
Host: locale.ForumHost(),
}

db.AddActivity([]Activity{post1, post2})
Expand Down
69 changes: 34 additions & 35 deletions server/forum_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"time"

"github.com/PuerkitoBio/goquery"
Expand Down Expand Up @@ -73,33 +72,37 @@ func (indexer *ForumIndexer) run() {
}
}

var wg sync.WaitGroup
wg.Add(len(Locales))

for _, l := range Locales {
l := l
go func() {
for {
for _, account := range accounts {
select {
case <-indexer.closeSignal:
return
default:
if err := indexer.index(l, account, timezone); err != nil {
log.WithError(err).Error("error indexing forum account: " + account)
}
time.Sleep(time.Second)
}
for {
for _, locale := range Locales {
select {
case <-indexer.closeSignal:
return
default:
logger := log.WithField("host", locale.ForumHost())
if err := locale.RefreshForumIds(); err != nil {
logger.WithError(err).Error("error refreshing forum ids")
} else {
logger.Info("refreshed forum ids")
}
time.Sleep(time.Second)
}
}()
}
for _, account := range accounts {
select {
case <-indexer.closeSignal:
return
default:
if err := indexer.index(account, timezone); err != nil {
log.WithError(err).Error("error indexing forum account: " + account)
}
time.Sleep(time.Second)
}
}
}

wg.Wait()
}

func (indexer *ForumIndexer) requestDocument(host, resource string) (*goquery.Document, error) {
urlString := fmt.Sprintf("https://%v/%v", host, strings.TrimPrefix(resource, "/"))
func (indexer *ForumIndexer) requestDocument(resource string) (*goquery.Document, error) {
urlString := fmt.Sprintf("https://www.pathofexile.com/%v", strings.TrimPrefix(resource, "/"))
jar, _ := cookiejar.New(nil)
u, _ := url.Parse(urlString)
jar.SetCookies(u, []*http.Cookie{
Expand Down Expand Up @@ -127,7 +130,7 @@ var postURLExpression = regexp.MustCompile("^/forum/view-post/([0-9]+)")
var threadURLExpression = regexp.MustCompile("^/forum/view-thread/([0-9]+)")
var forumURLExpression = regexp.MustCompile("^/forum/view-forum/([0-9]+)")

func ScrapeForumPosts(doc *goquery.Document, locale *Locale, timezone *time.Location) ([]*ForumPost, error) {
func ScrapeForumPosts(doc *goquery.Document, timezone *time.Location) ([]*ForumPost, error) {
posts := []*ForumPost(nil)

err := error(nil)
Expand All @@ -145,7 +148,7 @@ func ScrapeForumPosts(doc *goquery.Document, locale *Locale, timezone *time.Loca

timeText := sel.Find(".post_date").Text()

if post.Time, err = locale.ParseTime(timeText, timezone); err != nil {
if post.Time, err = time.ParseInLocation("Jan _2, 2006, 3:04:05 PM", timeText, timezone); err != nil {
log.WithField("text", timeText).Error("unable to parse time")
return false
}
Expand Down Expand Up @@ -177,24 +180,20 @@ func ScrapeForumPosts(doc *goquery.Document, locale *Locale, timezone *time.Loca
return posts, nil
}

func (indexer *ForumIndexer) forumPosts(locale *Locale, poster string, page int, timezone *time.Location) ([]*ForumPost, error) {
doc, err := indexer.requestDocument(locale.ForumHost(), fmt.Sprintf("/account/view-posts/%v/page/%v", poster, page))
func (indexer *ForumIndexer) forumPosts(poster string, page int, timezone *time.Location) ([]*ForumPost, error) {
doc, err := indexer.requestDocument(fmt.Sprintf("/account/view-posts/%v/page/%v", poster, page))
if err != nil {
return nil, err
}
posts, err := ScrapeForumPosts(doc, locale, timezone)
posts, err := ScrapeForumPosts(doc, timezone)
if err != nil {
return nil, err
}
for _, post := range posts {
post.Host = locale.ForumHost()
}
return posts, nil
}

func (indexer *ForumIndexer) index(locale *Locale, poster string, timezone *time.Location) error {
func (indexer *ForumIndexer) index(poster string, timezone *time.Location) error {
logger := log.WithFields(log.Fields{
"host": locale.ForumHost(),
"poster": poster,
})

Expand All @@ -203,7 +202,7 @@ func (indexer *ForumIndexer) index(locale *Locale, poster string, timezone *time
activity := []Activity(nil)

for page := 1; ; page++ {
posts, err := indexer.forumPosts(locale, poster, page, timezone)
posts, err := indexer.forumPosts(poster, page, timezone)
if err != nil {
logger.WithError(err).Error("error requesting forum posts")
}
Expand Down Expand Up @@ -243,7 +242,7 @@ func ScrapeForumTimezone(doc *goquery.Document) (*time.Location, error) {
}

func (indexer *ForumIndexer) sessionTimezone() (*time.Location, error) {
doc, err := indexer.requestDocument("www.pathofexile.com", "/my-account/preferences")
doc, err := indexer.requestDocument("/my-account/preferences")
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion server/forum_indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestScrapeForumPosts(t *testing.T) {
tz, err := time.LoadLocation("America/Los_Angeles")
require.NoError(t, err)

posts, err := ScrapeForumPosts(doc, Locales[0], tz)
posts, err := ScrapeForumPosts(doc, tz)
require.NoError(t, err)
assert.Equal(t, 10, len(posts))

Expand Down
39 changes: 37 additions & 2 deletions server/forum_post.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package server

import (
"encoding/json"
"fmt"
"time"
)

type ForumPost struct {
Id int `json:"id"`
Host string `json:"host"`
BodyHTML string `json:"body_html"`
Time time.Time `json:"time"`
Poster string `json:"poster"`
Expand All @@ -17,6 +17,32 @@ type ForumPost struct {
ForumName string `json:"forum_name"`
}

type forumPostWithHost struct {
Id int `json:"id"`
BodyHTML string `json:"body_html"`
Time time.Time `json:"time"`
Poster string `json:"poster"`
ThreadId int `json:"thread_id"`
ThreadTitle string `json:"thread_title"`
ForumId int `json:"forum_id"`
ForumName string `json:"forum_name"`
Host string `json:"host"`
}

func (p ForumPost) MarshalJSON() ([]byte, error) {
return json.Marshal(&forumPostWithHost{
Id: p.Id,
BodyHTML: p.BodyHTML,
Time: p.Time,
Poster: p.Poster,
ThreadId: p.ThreadId,
ThreadTitle: p.ThreadTitle,
ForumId: p.ForumId,
ForumName: p.ForumName,
Host: p.Host(),
})
}

func (p *ForumPost) ActivityTime() time.Time {
return p.Time
}
Expand All @@ -25,6 +51,15 @@ func (p *ForumPost) ActivityKey() uint32 {
return uint32(p.Id)
}

func (p *ForumPost) Host() string {
for _, l := range Locales {
if l.ForumIds()[p.ForumId] {
return l.ForumHost()
}
}
return "www.pathofexile.com"
}

func (p *ForumPost) PostURL() string {
return fmt.Sprintf("https://%v/forum/view-post/%v", p.Host, p.Id)
return fmt.Sprintf("https://%v/forum/view-post/%v", p.Host(), p.Id)
}
Loading