Exemplo n.º 1
0
// LoadJSONFile is to load json file
func LoadJSONFile(filePath string) *SiteInfo {
	lg.Debug("load json file")
	//initialize
	siteInfo = SiteInfo{}

	//test(get current dir)
	//dir := path.Dir(os.Args[0])
	//lg.Debugf("path.Dir(os.Args[0]): %s", dir)

	// Loading jsonfile
	if filePath == "" {
		//dir := path.Dir(os.Args[0])
		//lg.Debugf("path.Dir(os.Args[0]): %s", dir)
		//filePath = fmt.Sprintf("%s/json/teachers.json", dir)
		lg.Fatal("json filepath have to be set.")
		return nil
	}

	file, _ := ioutil.ReadFile(filePath)
	err := json.Unmarshal(file, &siteInfo)

	if err != nil {
		lg.Fatalf("json format is invalid: %v, filepath is %s", err, filePath)
		return nil
	}
	lg.Debugf("SiteInfo.Url: %v", siteInfo.URL)
	lg.Debugf("SiteInfo.Teachers[0].Id: %d, Name: %s, Country: %s", siteInfo.Teachers[0].ID, siteInfo.Teachers[0].Name, siteInfo.Teachers[0].Country)

	return &siteInfo
}
Exemplo n.º 2
0
// New is to create bold instance
func New(path string) {
	var err error
	db, err = bolt.Open(path, 0600, nil)
	if err != nil {
		lg.Fatal(err)
	}
}
Exemplo n.º 3
0
// GetHTML is to get scraped HTML from web page
func (t *Info) GetHTML(url string) {
	var flg = false

	//HTTP connection
	doc, err := goquery.NewDocument(fmt.Sprintf("%steacher/index/%d/", url, t.ID))
	if err != nil {
		lg.Fatal(err)
		return
	} else if isTeacherActive(doc) {
		parsedHTML := perseHTML(doc)

		//show teacher's id, name, date
		if printOn {
			fmt.Printf("----------- %s / %s / %d ----------- \n", t.Name, t.Country, t.ID)
		}
		for _, dt := range parsedHTML {
			if printOn {
				fmt.Println(dt)
			}
			flg = true
		}
		//save teacher
		if flg {
			saveTeacer(t)
		}
	} else {
		//no teacher
		if printOn {
			fmt.Printf("teacher [%d]%s quit \n", t.ID, t.Name)
		}
	}
}
Exemplo n.º 4
0
// debug for http request
func debugHTTPRequest(data []byte, err error) {
	if err == nil {
		//log.Debug(fmt.Sprintf("dump of http request\n%s", data))
		lg.Debugf("dump of http request : %s", data)
	} else {
		//log.Fatal(fmt.Sprintf("%v", err))
		lg.Fatal(err)
	}
}