99 "regexp"
1010 "strconv"
1111 "strings"
12- "sync"
1312 "time"
1413
1514 "github.com/PuerkitoBio/goquery"
@@ -73,33 +72,37 @@ func (indexer *ForumIndexer) run() {
7372 }
7473 }
7574
76- var wg sync.WaitGroup
77- wg .Add (len (Locales ))
78-
79- for _ , l := range Locales {
80- l := l
81- go func () {
82- for {
83- for _ , account := range accounts {
84- select {
85- case <- indexer .closeSignal :
86- return
87- default :
88- if err := indexer .index (l , account , timezone ); err != nil {
89- log .WithError (err ).Error ("error indexing forum account: " + account )
90- }
91- time .Sleep (time .Second )
92- }
75+ for {
76+ for _ , locale := range Locales {
77+ select {
78+ case <- indexer .closeSignal :
79+ return
80+ default :
81+ logger := log .WithField ("host" , locale .ForumHost ())
82+ if err := locale .RefreshForumIds (); err != nil {
83+ logger .WithError (err ).Error ("error refreshing forum ids" )
84+ } else {
85+ logger .Info ("refreshed forum ids" )
9386 }
87+ time .Sleep (time .Second )
9488 }
95- }()
89+ }
90+ for _ , account := range accounts {
91+ select {
92+ case <- indexer .closeSignal :
93+ return
94+ default :
95+ if err := indexer .index (account , timezone ); err != nil {
96+ log .WithError (err ).Error ("error indexing forum account: " + account )
97+ }
98+ time .Sleep (time .Second )
99+ }
100+ }
96101 }
97-
98- wg .Wait ()
99102}
100103
101- func (indexer * ForumIndexer ) requestDocument (host , resource string ) (* goquery.Document , error ) {
102- urlString := fmt .Sprintf ("https://%v /%v" , host , strings .TrimPrefix (resource , "/" ))
104+ func (indexer * ForumIndexer ) requestDocument (resource string ) (* goquery.Document , error ) {
105+ urlString := fmt .Sprintf ("https://www.pathofexile.com /%v" , strings .TrimPrefix (resource , "/" ))
103106 jar , _ := cookiejar .New (nil )
104107 u , _ := url .Parse (urlString )
105108 jar .SetCookies (u , []* http.Cookie {
@@ -127,7 +130,7 @@ var postURLExpression = regexp.MustCompile("^/forum/view-post/([0-9]+)")
127130var threadURLExpression = regexp .MustCompile ("^/forum/view-thread/([0-9]+)" )
128131var forumURLExpression = regexp .MustCompile ("^/forum/view-forum/([0-9]+)" )
129132
130- func ScrapeForumPosts (doc * goquery.Document , locale * Locale , timezone * time.Location ) ([]* ForumPost , error ) {
133+ func ScrapeForumPosts (doc * goquery.Document , timezone * time.Location ) ([]* ForumPost , error ) {
131134 posts := []* ForumPost (nil )
132135
133136 err := error (nil )
@@ -145,7 +148,7 @@ func ScrapeForumPosts(doc *goquery.Document, locale *Locale, timezone *time.Loca
145148
146149 timeText := sel .Find (".post_date" ).Text ()
147150
148- if post .Time , err = locale . ParseTime ( timeText , timezone ); err != nil {
151+ if post .Time , err = time . ParseInLocation ( "Jan _2, 2006, 3:04:05 PM" , timeText , timezone ); err != nil {
149152 log .WithField ("text" , timeText ).Error ("unable to parse time" )
150153 return false
151154 }
@@ -177,24 +180,20 @@ func ScrapeForumPosts(doc *goquery.Document, locale *Locale, timezone *time.Loca
177180 return posts , nil
178181}
179182
180- func (indexer * ForumIndexer ) forumPosts (locale * Locale , poster string , page int , timezone * time.Location ) ([]* ForumPost , error ) {
181- doc , err := indexer .requestDocument (locale . ForumHost (), fmt .Sprintf ("/account/view-posts/%v/page/%v" , poster , page ))
183+ func (indexer * ForumIndexer ) forumPosts (poster string , page int , timezone * time.Location ) ([]* ForumPost , error ) {
184+ doc , err := indexer .requestDocument (fmt .Sprintf ("/account/view-posts/%v/page/%v" , poster , page ))
182185 if err != nil {
183186 return nil , err
184187 }
185- posts , err := ScrapeForumPosts (doc , locale , timezone )
188+ posts , err := ScrapeForumPosts (doc , timezone )
186189 if err != nil {
187190 return nil , err
188191 }
189- for _ , post := range posts {
190- post .Host = locale .ForumHost ()
191- }
192192 return posts , nil
193193}
194194
195- func (indexer * ForumIndexer ) index (locale * Locale , poster string , timezone * time.Location ) error {
195+ func (indexer * ForumIndexer ) index (poster string , timezone * time.Location ) error {
196196 logger := log .WithFields (log.Fields {
197- "host" : locale .ForumHost (),
198197 "poster" : poster ,
199198 })
200199
@@ -203,7 +202,7 @@ func (indexer *ForumIndexer) index(locale *Locale, poster string, timezone *time
203202 activity := []Activity (nil )
204203
205204 for page := 1 ; ; page ++ {
206- posts , err := indexer .forumPosts (locale , poster , page , timezone )
205+ posts , err := indexer .forumPosts (poster , page , timezone )
207206 if err != nil {
208207 logger .WithError (err ).Error ("error requesting forum posts" )
209208 }
@@ -243,7 +242,7 @@ func ScrapeForumTimezone(doc *goquery.Document) (*time.Location, error) {
243242}
244243
245244func (indexer * ForumIndexer ) sessionTimezone () (* time.Location , error ) {
246- doc , err := indexer .requestDocument ("www.pathofexile.com" , " /my-account/preferences" )
245+ doc , err := indexer .requestDocument ("/my-account/preferences" )
247246 if err != nil {
248247 return nil , err
249248 }
0 commit comments