forked from ludoux/ngapost2md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (73 loc) · 3.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/imroc/req/v3"
"github.com/ludoux/ngapost2md/nga"
"github.com/spf13/cast"
"gopkg.in/ini.v1"
)
var (
//修改了 config.ini 文件后,需要同步修改此处和 assets/config.ini 文件里的 version
CONFIG_VERSION = "1.2.0"
)
func main() {
fmt.Printf("ngapost2md (c) ludoux [ GitHub: https://github.com/ludoux/ngapost2md/tree/neo ]\nVersion: %s\n", nga.VERSION)
if nga.DEBUG_MODE == "1" {
fmt.Println("***DEBUG MODE ON***")
}
if len(os.Args) != 2 {
log.Fatalln("传参数目错误!请使用 ngapost2md -h 命令查看 ngapost2md 的使用参数说明。")
}
if len(os.Args) == 2 && (cast.ToString(os.Args[1]) == "help" || cast.ToString(os.Args[1]) == "-help" || cast.ToString(os.Args[1]) == "--help" || cast.ToString(os.Args[1]) == "-h") {
fmt.Println("使用: ngapost2md tid")
fmt.Println("选项与参数说明: ")
fmt.Println("tid: 待下载的帖子 tid 号")
os.Exit(0)
}
//DEBUG_MODE不为1时(不是DEBUG_MODE)时检测更新
if nga.DEBUG_MODE != "1" {
resp, _ := req.C().R().Get("https://gitee.com/ludoux/check-update/raw/master/ngapost2md/version_neo.txt")
//版本更新配置文件改为 DO_NOT_CHECK ,软件则不会强制使用最新版本
if resp.String() != nga.VERSION && resp.String() != "DO_NOT_CHECK" {
log.Printf("目前版本: %s 最新版本: %s", nga.VERSION, resp.String())
log.Fatalln("请去 GitHub Releases 页面下载最新版本。软件即将退出……")
}
}
cfg, err := ini.Load("config.ini")
if err != nil {
log.Fatalln("无法读取 config.ini 文件,请检查文件是否存在。错误信息:", err.Error())
}
if cfg.Section("config").Key("version").String() != CONFIG_VERSION {
log.Fatalf("config.ini 版本号(%s)与本软件所需版本(%s)不符!\n软件升级后,请使用最新的 config.ini 配置文件,并修改其内的 UA、uid、cid 和其它个性化设置\n", cfg.Section("config").Key("version").String(), CONFIG_VERSION)
}
//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
nga.THREAD_COUNT = cfg.Section("network").Key("thread").InInt(2, []int{1, 2, 3})
nga.PAGE_DOWNLOAD_LIMIT = cfg.Section("network").Key("page_download_limit").RangeInt(100, -1, 100)
nga.GET_IP_LOCATION = cfg.Section("post").Key("get_ip_location").MustBool()
nga.ENHANCE_ORI_REPLY = cfg.Section("post").Key("enhance_ori_reply").MustBool()
nga.ENABLE_POST_TITLE = cfg.Section("post").Key("enable_post_title").MustBool()
nga.Client = nga.NewNgaClient()
tie := nga.Tiezi{}
tid, err := cast.ToIntE(os.Args[1])
if err != nil {
log.Fatalln("tid 无法转为数字:", err.Error())
}
if _, err := os.Stat(cast.ToString(tid)); os.IsNotExist(err) {
tie.InitFromWeb(tid)
} else {
log.Println("本地存在此 tid 文件夹,追加最新更改。")
tie.InitFromLocal(tid)
}
tie.Download()
}