Skip to content

Commit

Permalink
[fix] Shift-JISからUTF-8処理の修正
Browse files Browse the repository at this point in the history
  • Loading branch information
yoneyan committed Aug 28, 2021
1 parent 8380aff commit 9cd9776
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 40 deletions.
59 changes: 19 additions & 40 deletions jpnic.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
Expand Down Expand Up @@ -228,12 +227,13 @@ func (c *Config) GetAllIPv4(searchStr string) ([]InfoIPv4, error) {
}
defer resp.Body.Close()

bodyStr, err := ioutil.ReadAll(resp.Body)
reader := transform.NewReader(resp.Body, japanese.ShiftJIS.NewDecoder())
bodyByte, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatal(err)
return nil, err
}

doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyStr)))
doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyByte)))
if err != nil {
return nil, err
}
Expand All @@ -245,13 +245,7 @@ func (c *Config) GetAllIPv4(searchStr string) ([]InfoIPv4, error) {
tableHtml.Find("tr").Each(func(_ int, rowHtml *goquery.Selection) {
var info InfoIPv4
rowHtml.Find("td").Each(func(index int, tableCell *goquery.Selection) {
ioStr := strings.NewReader(tableCell.Text())
reader := transform.NewReader(ioStr, japanese.ShiftJIS.NewDecoder())
strByte, err = ioutil.ReadAll(reader)
if err != nil {
//return nil, err
}
dataStr := strings.TrimSpace(string(strByte))
dataStr := strings.TrimSpace(tableCell.Text())

switch index {
case 0:
Expand Down Expand Up @@ -369,12 +363,13 @@ func (c *Config) GetAllIPv6(searchStr string) ([]InfoIPv6, error) {
}
defer resp.Body.Close()

bodyStr, err := ioutil.ReadAll(resp.Body)
reader := transform.NewReader(resp.Body, japanese.ShiftJIS.NewDecoder())
bodyByte, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatal(err)
return nil, err
}

doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyStr)))
doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyByte)))
if err != nil {
return nil, err
}
Expand All @@ -386,13 +381,7 @@ func (c *Config) GetAllIPv6(searchStr string) ([]InfoIPv6, error) {
tableHtml.Find("tr").Each(func(_ int, rowHtml *goquery.Selection) {
var info InfoIPv6
rowHtml.Find("td").Each(func(index int, tableCell *goquery.Selection) {
ioStr := strings.NewReader(tableCell.Text())
reader := transform.NewReader(ioStr, japanese.ShiftJIS.NewDecoder())
strByte, err = ioutil.ReadAll(reader)
if err != nil {
//return nil, err
}
dataStr := strings.TrimSpace(string(strByte))
dataStr := strings.TrimSpace(tableCell.Text())

switch index {
case 0:
Expand Down Expand Up @@ -497,12 +486,13 @@ func (c *Config) GetIPv4User(userURL string) (InfoDetailIPv4, error) {
}
defer resp.Body.Close()

bodyStr, err := ioutil.ReadAll(resp.Body)
reader := transform.NewReader(resp.Body, japanese.ShiftJIS.NewDecoder())
bodyByte, err := ioutil.ReadAll(reader)
if err != nil {
return info, err
}

doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyStr)))
doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyByte)))
if err != nil {
return info, err
}
Expand All @@ -521,13 +511,7 @@ func (c *Config) GetIPv4User(userURL string) (InfoDetailIPv4, error) {
tableCell3.Find("table").Each(func(_ int, tableHtml4 *goquery.Selection) {
tableHtml4.Find("tr").Each(func(_ int, rowHtml4 *goquery.Selection) {
rowHtml4.Find("td").Each(func(index int, tableCell4 *goquery.Selection) {
ioStr := strings.NewReader(tableCell4.Text())
reader := transform.NewReader(ioStr, japanese.ShiftJIS.NewDecoder())
strByte, err := ioutil.ReadAll(reader)
if err != nil {
//return info, err
}
dataStr := strings.TrimSpace(string(strByte))
dataStr := strings.TrimSpace(string(tableCell4.Text()))

if index == 1 {
switch count {
Expand Down Expand Up @@ -660,12 +644,13 @@ func (c *Config) GetIPv6User(userURL string) (InfoDetailIPv6, error) {
}
defer resp.Body.Close()

bodyStr, err := ioutil.ReadAll(resp.Body)
reader := transform.NewReader(resp.Body, japanese.ShiftJIS.NewDecoder())
bodyByte, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatal(err)
return info, err
}

doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyStr)))
doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bodyByte)))
if err != nil {
return info, err
}
Expand All @@ -684,13 +669,7 @@ func (c *Config) GetIPv6User(userURL string) (InfoDetailIPv6, error) {
tableCell3.Find("table").Each(func(_ int, tableHtml4 *goquery.Selection) {
tableHtml4.Find("tr").Each(func(_ int, rowHtml4 *goquery.Selection) {
rowHtml4.Find("td").Each(func(index int, tableCell4 *goquery.Selection) {
ioStr := strings.NewReader(tableCell4.Text())
reader := transform.NewReader(ioStr, japanese.ShiftJIS.NewDecoder())
strByte, err := ioutil.ReadAll(reader)
if err != nil {
//return info, err
}
dataStr := strings.TrimSpace(string(strByte))
dataStr := strings.TrimSpace(tableCell4.Text())

if index == 1 {
switch count {
Expand Down
30 changes: 30 additions & 0 deletions jpnic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ var searchStr = "doornoc"
// }
//}

func Test1GetIPv4(t *testing.T) {
con := Config{
URL: "https://iphostmaster.nic.ad.jp/jpnic/certmemberlogin.do",
CertFilePath: certFilePath,
KeyFilePath: keyFilePath,
CAFilePath: caFilePath,
}

data, err := con.GetAllIPv4(searchStr)
if err != nil {
t.Fatal(err)
}
t.Log(data)
}

func TestGetIPv4(t *testing.T) {
sessionID, err := randomStr()
if err != nil {
Expand Down Expand Up @@ -282,6 +297,21 @@ func TestGetIPv4(t *testing.T) {
}
}

func Test1GetIPv6(t *testing.T) {
con := Config{
URL: "https://iphostmaster.nic.ad.jp/jpnic/certmemberlogin.do",
CertFilePath: certFilePath,
KeyFilePath: keyFilePath,
CAFilePath: caFilePath,
}

data, err := con.GetAllIPv6(searchStr)
if err != nil {
t.Fatal(err)
}
t.Log(data)
}

func TestGetIPv6(t *testing.T) {
sessionID, err := randomStr()
if err != nil {
Expand Down

0 comments on commit 9cd9776

Please sign in to comment.