Created
December 5, 2017 12:51
-
-
Save yyano/8ddbda508f050bf2230f21e2b344e9a7 to your computer and use it in GitHub Desktop.
Qiita API v2で記事一覧を取得して、Markdown記法で日付ごとのファイルの先頭に追記していく。
This file contains 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 ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
) | |
type structTags struct { | |
Name string `json:"name"` | |
Version []string `json:"versions"` | |
} | |
type structUser struct { | |
Id string `json:"id"` | |
Name string `json:"name"` | |
} | |
type Post struct { | |
Created_at string `json:"created_at"` | |
Updated_at string `json:"updated_at"` | |
Title string `json:"title"` | |
Url string `json:"url"` | |
Likes_count int `json:"likes_count"` | |
Comments_count int `json:"comments_count"` | |
User structUser `json:"user"` | |
Tags []structTags `json:"tags"` | |
} | |
const ( | |
url_base = "http://qiita.com/api/v2/items?per_page=10" | |
path_base = "./github/" | |
) | |
func main() { | |
jst := time.FixedZone("Asia/Tokyo", 9*60*60) | |
beginTime := time.Now().Add(-2 * time.Hour).In(jst) | |
beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), beginTime.Hour(), 0, 0, 0, jst) | |
toTime := time.Now().Add(-1 * time.Hour).In(jst) | |
toTime = time.Date(toTime.Year(), toTime.Month(), toTime.Day(), toTime.Hour(), 0, 0, 0, jst) | |
log.Printf("%#v %#v \n", beginTime.Format("2006-01-02T15:04:05-07:00"), toTime.Format("2006-01-02T15:04:05-07:00")) | |
textmd := fmt.Sprintf("# %s\n", beginTime.Format("2006-01-02 15:00~")) | |
for page := 1; page < 3; page++ { | |
url := fmt.Sprintf("%s&page=%d", url_base, page) | |
resp, err := http.Get(url) | |
if err != nil { | |
log.Fatal(err.Error()) | |
} | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Fatal(err.Error()) | |
} | |
resp.Body.Close() | |
var postsTemp []Post | |
err = json.Unmarshal(body, &postsTemp) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, post := range postsTemp { | |
t, _ := time.Parse("2006-01-02T15:04:05-07:00", post.Created_at) | |
fmt.Println(beginTime, ", ", t, ", ", toTime) | |
if t.After(beginTime) && t.Before(toTime) { | |
log.Printf("%#v \n", post) | |
textmd += fmt.Sprintf("## [%s](%s)\n", post.Title, post.Url) | |
textmd += fmt.Sprintf("##### ") | |
for _, tag := range post.Tags { | |
textmd += fmt.Sprintf("[%s](https://qiita.com/tags/%s), ", tag.Name, tag.Name) | |
} | |
textmd += fmt.Sprintf("\n") | |
textmd += fmt.Sprintf("###### update:%s\n", t.Format("2006-01-02 15:04")) | |
textmd += fmt.Sprintf("---\n") | |
} | |
} | |
} | |
filename := path_base + beginTime.Format("2006-01-02") + ".md" | |
oldfile, _ := ioutil.ReadFile(filename) | |
ioutil.WriteFile(filename, []byte(textmd), 0666) | |
addfile, _ := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND, 0666) | |
defer addfile.Close() | |
fmt.Fprintln(addfile, "\n\n\n") | |
fmt.Fprintln(addfile, string(oldfile)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment