In acquiring the number of LGTM and View of Qiita I was able to refer to it because the API specifications have changed, so I couldn't get it. I will describe the acquisition method.
Go 1.15.2
Select read_qiita and write_qiita as the scope
GET /api/v2/authenticated_user/items
Returns a list of articles for authenticated users in descending order of creation date and time.
GET /api/v2/items/:item_id
Get the article.
UserInfo.go
package data
type UserInfo struct {
Id string `json:"id"`
Likes_count int `json:"likes_count"`
Title string `json:"title"`
Page_views_count int `json:"page_views_count"`
}
web.go
package qiita
import (
"../data"
"../exporter"
"encoding/json"
"io/ioutil"
"net/http"
)
func GetQiitaViews() { // main.Call from go
url := "https://qiita.com/api/v2/authenticated_user/items?page=1&per_page=20"
resp, err := doHttpRequest(url)
defer resp.Body.Close() //Close Body
body, err := ioutil.ReadAll(resp.Body) //Get from the body of the response
var userInfos []data.UserInfo
if err = json.Unmarshal(body, &userInfos); err != nil { //Read json
return
}
index := 0
for _, user := range userInfos { //Read one article at a time
url = "https://qiita.com/api/v2/items/" + user.Id //Get the number of views using the article ID
resp, err := doHttpRequest(url)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err := json.Unmarshal(body, &user); err != nil {return}
userInfos[index].Page_views_count = user.Page_views_count
index += 1
}
exporter.ToCsv(userInfos) //csv conversion
}
func doHttpRequest(url string) (*http.Response, error) { //Common request
req, _ := http.NewRequest("GET", url, nil)
buf, _ := ioutil.ReadFile("token.txt")
token := string(buf) //Manage tokens in a separate file
req.Header.Set("content-type", "application/json") //Application in header/Specify json
req.Header.Set("Authorization", "Bearer " + token) //Specify token in the header
client := new(http.Client)
resp, err := client.Do(req)
return resp, err
}
It seems that it can not be obtained only with the following API, At first I didn't realize that I needed to query the URL and couldn't get it. Also, since page_view_count returns null (0 when put in struct with Go), It seems that the number of views needs to be obtained for each article. [[Qiita API] Like! I referred to the article of "Automatic counting of views" (https://qiita.com/Naoto9282/items/252c4b386aeafc0052ba).
GET /api/v2/authenticated_user/items
Returns a list of articles for authenticated users in descending order of creation date and time.
Therefore, the following API is used.
GET /api/v2/items/:item_id
Get the article.
export.go
package exporter
import (
"../data"
"encoding/csv"
"os"
"strconv"
)
func ToCsv(userInfos []data.UserInfo) {
file, _ := os.OpenFile("result.csv", os.O_WRONLY|os.O_CREATE, 0600) //Open file
defer file.Close()
writer := csv.NewWriter(file)
writer.Write([]string{"title", "like_count", "page_views_count"}) //Add a header
for _, user := range userInfos {
likesCount := strconv.Itoa(user.Likes_count) //Convert int to string
pageViewsCount := strconv.Itoa(user.Page_views_count)
writer.Write([]string{user.Title, likesCount, pageViewsCount}) //writing
}
writer.Flush()
}
The source can be found here (https://github.com/kurramkurram/QiitaViewCounts). Due to uploading the source to GitHub, token information is managed in a separate file in the same directory as main.go. Some error handling is omitted.
Official [Qiita API] Like! Automatic counting of views Export CSV in Go language! SJIS version for Excel! Go's import cycle not allowed
Recommended Posts