@@ -22,6 +22,25 @@ func NewScraper(db loadb.DB) *Scraper {
2222 return & Scraper {db : db , rateLimiter : rate .NewLimiter (rate .Every (time .Second ), 1 )}
2323}
2424
25+ func (s * Scraper ) ScrapeItem () error {
26+ categories , err := s .getCategoriesToScrape ()
27+ if err != nil {
28+ return fmt .Errorf ("failed to get market item categories: %w" , err )
29+ }
30+
31+ itemsToSave , err := s .getItemsToSave (categories )
32+ if err != nil {
33+ return fmt .Errorf ("failed to get market items: %w" , err )
34+ }
35+
36+ err = s .saveItems (itemsToSave )
37+ if err != nil {
38+ return fmt .Errorf ("failed to save market items: %w" , err )
39+ }
40+
41+ return nil
42+ }
43+
2544func (s * Scraper ) ScrapeStat () error {
2645 items , err := s .getItemsToScrape ()
2746 if err != nil {
@@ -114,3 +133,77 @@ func (s *Scraper) getItemStatToCreate(category *loadb.MarketItemCategory, item l
114133
115134 return & stat , nil
116135}
136+
137+ func (s * Scraper ) getCategoriesToScrape () ([]loadb.MarketItemCategory , error ) {
138+ categories , err := s .db .MarketItemCategory ().FindItemScraperEnabledAll ()
139+ if err != nil {
140+ return nil , err
141+ }
142+
143+ if len (categories ) == 0 {
144+ return nil , fmt .Errorf ("no market item categories found" )
145+ }
146+
147+ return categories , nil
148+ }
149+
150+ func (s * Scraper ) getItemsToSave (categories []loadb.MarketItemCategory ) ([]loadb.MarketItem , error ) {
151+ var itemsToUpsert []loadb.MarketItem
152+ seenItems := make (map [string ]bool )
153+
154+ for _ , category := range categories {
155+ pageNo := 1
156+
157+ for {
158+ resp , err := request .GetMarketItemList (& loaApi.GetMarketItemListParams {
159+ CategoryCode : category .Code ,
160+ PageNo : pageNo ,
161+ })
162+ if err != nil {
163+ return nil , fmt .Errorf ("failed to get market items for category %d: %w" , category .Code , err )
164+ }
165+
166+ itemCounts := len (resp .Items )
167+
168+ if itemCounts > 0 {
169+ for _ , item := range resp .Items {
170+ uniqueKey := fmt .Sprintf ("%s-%s" , item .Name , item .Grade )
171+
172+ if seenItems [uniqueKey ] {
173+ log .Printf ("중복 아이템 스킵: 이름=%s, 등급=%s, ID=%d" ,
174+ item .Name , item .Grade , item .ID )
175+ continue
176+ }
177+
178+ seenItems [uniqueKey ] = true
179+ itemsToUpsert = append (itemsToUpsert , loadb.MarketItem {
180+ BundleCount : item .BundleCount ,
181+ Grade : item .Grade ,
182+ MarketItemCategoryID : category .ID ,
183+ Name : item .Name ,
184+ ImageUrl : item .Icon ,
185+ RefID : item .ID ,
186+ })
187+ }
188+ }
189+
190+ if itemCounts < resp .PageSize {
191+ break
192+ }
193+
194+ pageNo ++
195+ }
196+ }
197+
198+ if len (itemsToUpsert ) == 0 {
199+ return nil , fmt .Errorf ("no market items found" )
200+ }
201+
202+ return itemsToUpsert , nil
203+ }
204+
205+ func (s * Scraper ) saveItems (items []loadb.MarketItem ) error {
206+ log .Println ("Market items saved successfully" )
207+
208+ return s .db .MarketItem ().UpsertMany (items )
209+ }
0 commit comments