Skip to content

Commit

Permalink
feat:合并配置
Browse files Browse the repository at this point in the history
  • Loading branch information
oarinv committed May 17, 2023
1 parent 6b03207 commit 2c658bd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
2 changes: 2 additions & 0 deletions assets/config.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[network]
base_url="https://bbs.nga.cn"
ua="""MODIFY_ME"""
ngaPassportUid=""
ngaPassportCid=""
thread=2

[post]
Expand Down
1 change: 0 additions & 1 deletion assets/cookies.json

This file was deleted.

9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"strings"

"github.com/ludoux/ngapost2md/nga"
"github.com/spf13/cast"
Expand All @@ -18,6 +19,14 @@ func main() {
fmt.Printf("Fail to find or read config.ini file: %v", err)
os.Exit(1)
}

//Cookie
var ngaPassportUid = cfg.Section("network").Key("ngaPassportUid").String()
var ngaPassportCid = cfg.Section("network").Key("ngaPassportCid").String()
var cookie strings.Builder
cookie.WriteString("ngaPassportUid="+ngaPassportUid+";"+"ngaPassportCid="+ngaPassportCid)
nga.COOKIE = cookie.String()

nga.BASE_URL = cfg.Section("network").Key("base_url").String()
nga.UA = cfg.Section("network").Key("ua").String()
//默认线程数为2,仅支持1~3
Expand Down
12 changes: 2 additions & 10 deletions nga/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package nga

import (
"fmt"
"log"

"github.com/imroc/req/v3"
cookiejar "github.com/orirawlings/persistent-cookiejar"
)

type NgaClient struct {
Expand All @@ -16,18 +14,12 @@ type NgaClient struct {
var Client *NgaClient
var BASE_URL string
var UA string
var COOKIE string

func NewNgaClient() *NgaClient {
jar, err := cookiejar.New(&cookiejar.Options{
Filename: "cookies.json",
})
if err != nil {
log.Fatalf("failed to create persistent cookiejar: %s\n", err.Error())
}
c := req.C().
SetCookieJar(jar).
SetBaseURL(BASE_URL).
SetCommonHeader("Referer", BASE_URL).
SetCommonHeader("Cookie",COOKIE).
SetUserAgent(UA).
// EnableDump at the request level in request middleware which dump content into
// memory (not print to stdout), we can record dump content only when unexpected
Expand Down

0 comments on commit 2c658bd

Please sign in to comment.