go实现github爬虫

发布于:2021-11-30 16:28:12

package main

import (
"time"
"net/http"
"fmt"
"encoding/json"
"github.com/urfave/cli"
"os"
"log"
"flag"
"bufio"
)

const IssuesURL = "https://api.github.com/search/issues"
var save bool

type IssuesSearchResult struct {
TotalCount int `json:"total_count"`
Items []*Issue
}

type Issue struct {
Number int
HTMLURL string `json:"html_url"`
Title string
State string
User *User
CreatedAt time.Time `json:"created_at"`
Body string // in Markdown format
}

type User struct {
Login string
HTMLURL string `json:"html_url"`
}

var result IssuesSearchResult

func SearchIssue(terms string ) (IssuesSearchResult, error){
resp, err := http.Get(IssuesURL + "?q=" + terms)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close();

if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
log.Fatal(err);
}


return result,nil;
}


func search(ctx *cli.Context){
if len(os.Args) < 2{
fmt.Println("please input your question")
}

result, err := SearchIssue(os.Args[3])
if err != nil {
log.Fatal(err)
}

var r *bufio.Writer
if save {
f,_ := os.Open("input.txt")
r = bufio.NewWriter(f);
}


fmt.Printf("%d result
"
,result.TotalCount)
for _,item := range result.Items {
fmt.Println(item.CreatedAt,item.Title,item.User.Login)
if save {
r.WriteString(item.Title);
r.WriteString("
"
)
}
}
}

func init(){

}

func main() {
flag.BoolVar(&save,"save",false,"save results to file");
flag.Parse()
app := cli.NewApp();
app.Name = "githubsearch";
app.Usage = "search github issue by github open api"
app.Commands = []cli.Command{
{
Name: "find",
ShortName: "f",
Usage: "search your problem by github api",
Action: search,
},
}
fmt.Println(save)
//app.Run(os.Args)
}

相关推荐

最新更新

猜你喜欢