Created
November 19, 2015 16:37
-
-
Save deris/761c309e7eabad5d8c8d to your computer and use it in GitHub Desktop.
vim.orgのScriptを新しい順に取得しratingが10以上をコンソールに出力
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/PuerkitoBio/goquery" | |
"os" | |
"strconv" | |
) | |
const topURL = "http://www.vim.org/scripts/" | |
const vimURL = "http://www.vim.org/scripts/script_search_results.php?&show_me=%d&result_ptr=%d" | |
const count = 1000 | |
type Plugin struct { | |
url string | |
name string | |
typ string | |
rating int | |
downloads int | |
summary string | |
} | |
func (pl Plugin) String() string { | |
return fmt.Sprintf("raging:%v, downloads:%v, name:%v, url:%v summary:%v", pl.rating, pl.downloads, pl.name, pl.url, pl.summary) | |
} | |
func main() { | |
doc, err := goquery.NewDocument(fmt.Sprintf(vimURL, count, 0)) | |
if err != nil { | |
panic("parse error") | |
} | |
plugins := make([]Plugin, 0, 100) | |
doc.Find("tr").Each(func(_ int, s *goquery.Selection) { | |
pl := Plugin{} | |
s.Find("td.rowodd,td.roweven").Each(func(i int, s *goquery.Selection) { | |
switch i { | |
case 0: | |
url, ok := s.Find("a").Attr("href") | |
if !ok { | |
fmt.Fprintf(os.Stderr, "error1\n") | |
} | |
name := s.Text() | |
pl.url = topURL + url | |
pl.name = name | |
case 1: | |
pl.typ = s.Text() | |
case 2: | |
rating, err := strconv.Atoi(s.Text()) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "error2") | |
} | |
pl.rating = rating | |
case 3: | |
downloads, err := strconv.Atoi(s.Text()) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "error3") | |
} | |
pl.downloads = downloads | |
case 4: | |
pl.summary = s.Text() | |
} | |
}) | |
if pl.rating > 10 { | |
switch pl.typ { | |
case "color scheme", "syntax", "patch": | |
default: | |
plugins = append(plugins, pl) | |
} | |
} | |
}) | |
for _, pl := range plugins { | |
fmt.Printf("%v\n", pl) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment