LoginSignup
0
0

More than 3 years have passed since last update.

本の新刊データをJSONで取得できるようにする。

Posted at

はじめに

 コミックやラノベの新刊データ取得で、JSON形式で無料で取得しようとするAPIが見つからなかったので、ゲーム、アニメ、CD発売日一覧というサイトからスクレイピングして、JSONで出力するようなプログラムを作る

プログラム概要

ゲーム、アニメ、CD発売日一覧というサイトから、
新刊の発売日、名称、作者、画像が引っ張ってこれるので、これらのデータをJSONで出力するようにする
また、実行結果は標準出力するようにする。

プログラム内容

calendarscriping.go
package main

import (
    "fmt"
    "strings"

    "github.com/PuerkitoBio/goquery"
)

type BookList struct {
    Type   string `json:type`
    Months string `json:months`
    Days   string `json:days`
    Img    string `json:img`
    Title  string `json:title`
    Writer string `json:writer`
    Bround string `json:bround`
    Ext    string `json:ext`
}

const (
    BOOKURL      string = "https://calendar.gameiroiro.com/"
    COMICURL     string = "manga.php"
    LITENOVELURL string = "litenovel.php"
    MAGAZINEURL  string = "magazine.php"
)
const (
    COMIC     = 0
    LITENOVEL = 1
    MAGAZINE  = 2
)

func GetComicList(year, month string, booktype int) []BookList {
    var output []BookList
    mounth_tmp := month

    url := BOOKURL
    switch booktype {
    case COMIC:
        url += COMICURL
    case LITENOVEL:
        url += LITENOVELURL
    case MAGAZINE:
        url += MAGAZINEURL
    }
    if (year != "") && (month != "") {
        url += "?year=" + year + "&month=" + month
    }
    doc, err := goquery.NewDocument(url)
    if err != nil {
        fmt.Println(err.Error())
        return output
    }

    if mounth_tmp == "" {
        mounth_tmp, _ = doc.Find("input.month").Attr("value")
    }
    doc.Find("div#content-inner").Each(func(i int, s *goquery.Selection) {
        s.Find("tr").Each(func(j int, ss *goquery.Selection) {
            days := ss.Find("td.day-td").Text()
            ss.Find("div.div-wrap").Each(func(k int, sss *goquery.Selection) {
                var tmp BookList
                tmp.Days = days
                tmp.Months = mounth_tmp
                sss.Find("div.product-image-left").Each(func(l int, image *goquery.Selection) {
                    tmp.Img, _ = image.Find("img").Attr("src")
                })
                sss.Find("div.product-description-right").Each(func(k int, data *goquery.Selection) {
                    tmp.Type = strings.TrimSpace(data.Find("p.p-genre").Text())
                    tmp.Title = strings.TrimSpace(data.Find("a").Text())
                    data.Find("p.p-company").Each(func(i int, data2 *goquery.Selection) {
                        if tmp.Bround == "" {
                            tmp.Bround = strings.TrimSpace(data2.Text())
                        } else if tmp.Writer == "" {
                            tmp.Writer = strings.TrimSpace(data2.Text())
                        } else if tmp.Ext == "" {
                            tmp.Ext = strings.TrimSpace(data2.Text())
                        } else {
                            tmp.Ext += "," + strings.TrimSpace(data2.Text())
                        }

                    })
                })
                output = append(output, tmp)
            })

        })
    })
    return output
}

main.go
package main
import (
    "fmt"
)
func main(){
    fmt.Println(GetComicList("", "", COMIC))
}

実行すると、今月のコミックスの取得結果がJSONで得られる

まとめ

これらのプログラムを応用して、WebAPIにするとブラウザ処理が楽になる。
また、SQLサーバと連携して、ローカルにキャッシュを持つことで高速化や
細かい出力に変更できるように改造すると楽になる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0