Guest User

Untitled

a guest
Jul 27th, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.64 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "sort"
  6.     "sync"
  7.     "time"
  8.  
  9.     "github.com/geeksbaek/goinside"
  10. )
  11.  
  12. type Pair struct {
  13.     Key   string
  14.     Value int
  15. }
  16.  
  17. type PairList []Pair
  18.  
  19. func (p PairList) Len() int           { return len(p) }
  20. func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
  21. func (p PairList) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
  22.  
  23. func sortByValue(m map[string]int) PairList {
  24.     pl := make(PairList, len(m))
  25.     i := 0
  26.     for k, v := range m {
  27.         pl[i] = Pair{k, v}
  28.         i++
  29.     }
  30.     sort.Sort(sort.Reverse(pl))
  31.     return pl
  32. }
  33.  
  34. func main() {
  35.     m := map[string]goinside.Articles{}
  36.  
  37. Loop:
  38.     for i := 1; ; i += 100 {
  39.         articlesSlice := make([]goinside.Articles, 100)
  40.         URL := "http://gall.dcinside.com/board/lists/?id=programming"
  41.  
  42.         wg := new(sync.WaitGroup)
  43.         wg.Add(100)
  44.         for j := i; j < i+100; j++ {
  45.             go func(URL string, page int) {
  46.                 defer wg.Done()
  47.                 if list, err := goinside.GetList(URL, page); err == nil {
  48.                     articlesSlice[page-i] = list.Articles
  49.                 }
  50.             }(URL, j)
  51.         }
  52.         wg.Wait()
  53.  
  54.         for _, articles := range articlesSlice {
  55.             for _, article := range articles {
  56.                 if !isThisWeek(article) {
  57.                     break Loop
  58.                 }
  59.                 m[article.AuthorInfo.Name] = append(
  60.                     m[article.AuthorInfo.Name],
  61.                     article,
  62.                 )
  63.             }
  64.         }
  65.     }
  66.  
  67.     dummy := map[string]int{}
  68.     for _, v := range m {
  69.         for _, vv := range v {
  70.             dummy[vv.Name]++
  71.         }
  72.     }
  73.  
  74.     for _, v := range sortByValue(dummy) {
  75.         fmt.Printf("이름: {%s}, 글 개수: {%d}\n", v.Key, v.Value)
  76.     }
  77. }
  78.  
  79. func isThisWeek(article *goinside.Article) bool {
  80.     if article.Date.Weekday() != time.Now().Weekday() {
  81.         return false
  82.     }
  83.     return true
  84. }
Add Comment
Please, Sign In to add comment