point-tools-crawler/point_post_crawler.go
2016-03-22 23:12:37 +03:00

102 lines
2.2 KiB
Go

package main
import (
"bitbucket.org/skobkin/point-tools-crawler/point"
"bitbucket.org/skobkin/point-tools-crawler/point_tools"
"flag"
"fmt"
"log"
"strconv"
"time"
)
func main() {
var pointLogin, pointPassword, pointToolsToken string
flag.StringVar(&pointLogin, "l", "", "Account login")
flag.StringVar(&pointPassword, "p", "", "Account password")
flag.StringVar(&pointToolsToken, "t", "", "Point Tools crawler API token")
flag.Parse()
if len(pointLogin) < 1 || len(pointPassword) < 1 {
fmt.Println("Login and password must be defined")
return
}
if len(pointToolsToken) < 1 {
fmt.Println("Point Tools token must be defined")
return
}
pointClient := point.NewClient("https://point.im/api/")
pointToolsClient := point_tools.NewClient("http://point-tools.local:8000/api/crawler/", pointToolsToken)
_, loginErr := pointClient.Login(pointLogin, pointPassword)
if loginErr != nil {
fmt.Printf("Login error %s", loginErr)
return
} else {
fmt.Printf("Successfully authenticated!\n")
}
page, reqErr := pointClient.GetRecentAllPostsPage()
if reqErr != nil {
log.Fatal(reqErr)
return
}
fmt.Printf("1 page requested\n")
if len(page.Posts) > 0 {
fmt.Println("Last uid", strconv.Itoa(page.Posts[len(page.Posts)-1].Uid))
}
sendResp, sendErr := pointToolsClient.SendPage(page)
if sendErr != nil {
log.Fatal(sendErr)
}
if point_tools.STATUS_SUCCESS != sendResp.Status || false == sendResp.Data.Continue {
fmt.Println("API rejected next page request")
fmt.Println("Exiting.")
return
}
pageNumber := 1
for page.HasNext {
pageNumber++
page, reqErr = pointClient.GetNextAllPostsPage(page)
if reqErr != nil {
log.Fatal(reqErr)
return
}
fmt.Printf("%d page requested", pageNumber)
if len(page.Posts) > 0 {
fmt.Printf(", last uid %d", page.Posts[len(page.Posts)-1].Uid)
}
fmt.Printf(" -> %d posts\n", len(page.Posts))
sendResp, sendErr := pointToolsClient.SendPage(page)
if sendErr != nil {
log.Fatal(sendErr)
}
if point_tools.STATUS_SUCCESS != sendResp.Status || false == sendResp.Data.Continue {
fmt.Println("API rejected next page request")
break
}
time.Sleep(time.Second)
}
fmt.Println("Exiting.")
}