Skip to content

Instantly share code, notes, and snippets.

@dohq
Created June 5, 2016 00:26
Show Gist options
  • Save dohq/f6ed736f8d13d917d8486a17358e17d7 to your computer and use it in GitHub Desktop.
Save dohq/f6ed736f8d13d917d8486a17358e17d7 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
// HMKey is common
var HMKey = "hO2QHdapzbqbTFOaJgZTKXgT2gWqYS"
// AppKey is get 2chApiWiki(http://prokusi.wiki.fc2.com/wiki)
var AppKey = "JYW2J6wh9z8p8xjGFxO3M2JppGCyjQ"
// CT is unique 10 length number
var CT = "1234567890"
// serverName is URL SubDomein (ex.http://***.2ch.net/...)
var serverName = "echo"
// boardName is (http://foo.2ch.net/***/...)
var boardName = "unix"
// threadId is (http://foo.2ch.net/bar/***/...)
var threadID = "1455436351"
// セッションID取得
func authenticate() string {
message := AppKey + CT
//Get HB String
key := []byte(HMKey)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
HB := hex.EncodeToString(h.Sum(nil))
//POST Start
client := &http.Client{}
data := url.Values{"ID": {""}, "PW": {""}, "KY": {AppKey}, "CT": {CT}, "HB": {HB}}
req, _ := http.NewRequest(
"POST",
"https://api.2ch.net/v1/auth/",
strings.NewReader(data.Encode()),
)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
//add header
req.Header.Set("User-Agent", "Mozilla/3.0 (compatible; JaneStyle/3.83)")
req.Header.Set("X-2ch-UA", "JaneStyle/3.83")
resp, _ := client.Do(req)
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
arr := strings.Split(string(body), ":")
sid := (arr[1])
//return Session-ID
return (sid)
}
// 取得したsidを使用し、DATを取得
func getDAT(serverName string, boardName string, threadID string, sid string) string {
message := "/v1/" + serverName + "/" + boardName + "/" + threadID + sid + AppKey
URL := "https://api.2ch.net/v1/" + serverName + "/" + boardName + "/" + threadID
key := []byte(HMKey)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
hobo := hex.EncodeToString(h.Sum(nil))
client := &http.Client{}
data := url.Values{"sid": {sid}, "hobo": {hobo}, "appkey": {AppKey}}
req, _ := http.NewRequest(
"POST",
URL,
strings.NewReader(data.Encode()),
)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
//add header
req.Header.Set("User-Agent", "Mozilla/3.0 (compatible; JaneStyle/3.83)")
resp, _ := client.Do(req)
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
//return body
return (string(body))
}
// DATの読み込み
func main() {
sid := authenticate()
dat := getDAT(serverName, boardName, threadID, sid)
fmt.Println(dat)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment