-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (144 loc) · 4.02 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
"golang.org/x/net/html/charset"
)
type Response struct {
Code int `json:"code"`
Data struct {
Items []Item `json:"items"`
} `json:"data"`
}
type Item struct {
ID int `json:"id"`
//ColumnID int `json:"column_id"`
Title string `json:"title"`
Description string `json:"description"`
Published_at string `json:"published_At"`
}
var uniitems []Item
var apiURL = "https://xizhi.qqoq.net/XZ0683393781ffb434c949f89b1acf4acf.channel"
func clearScreen() {
switch goos := runtime.GOOS; goos {
case "windows":
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
case "linux", "darwin":
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
}
}
func isItemIDExist(itemID int) bool {
file, err := os.Open("itemid.txt")
if err != nil {
return false
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if line == strconv.Itoa(itemID) {
return true
}
}
return false
}
func main() {
for {
resp, err := http.Get("https://www.odaily.news/api/pp/api/info-flow/newsflash_columns/newsflashes?b_id=&per_page=3")
if err != nil {
fmt.Println("请求出错:", err)
} else {
defer resp.Body.Close()
// 将响应体的数据转换为UTF-8编码
utf8Body, err := charset.NewReader(resp.Body, resp.Header.Get("Content-Type"))
if err != nil {
fmt.Println("转换编码出错:", err)
} else {
result, err := ioutil.ReadAll(utf8Body)
if err != nil {
fmt.Println("读取转换后的数据出错:", err)
} else {
// 解析JSON数据
var response Response
err := json.Unmarshal(result, &response)
if err != nil {
fmt.Println("解析JSON数据出错:", err)
} else {
fmt.Println("解析到的数据:")
for _, item := range response.Data.Items {
if isItemIDExist(item.ID) {
clearScreen()
fmt.Println("存在相同文章")
continue
}
// 将item信息添加到结构体数组中
uniitems = append(uniitems, Item{
ID: item.ID,
Published_at: item.Published_at,
Title: item.Title,
Description: item.Description,
})
}
// 遍历输出结构体数组中的item信息
for _, item := range uniitems {
fmt.Println("ID:", item.ID)
fmt.Println("Published_at:", item.Published_at)
fmt.Println("Title:", item.Title)
fmt.Println("Description:", item.Description)
fmt.Println("==============================")
// 准备要发送的数据
title := item.Title
content := "发布时间: " + "\n" + item.Published_at + "\n" + "\n" + "概述: " + "\n" + "\n" + item.Description
// 使用url.Values来保存POST请求的参数
payload := url.Values{}
payload.Set("title", title)
payload.Set("content", content)
// 发送POST请求
resp, err := http.Post(apiURL, "application/x-www-form-urlencoded", strings.NewReader(payload.Encode()))
if err != nil {
fmt.Println("发送POST请求失败:", err)
continue
}
defer resp.Body.Close()
// 检查响应状态码
if resp.StatusCode != http.StatusOK {
fmt.Println("POST请求返回非200状态码:", resp.StatusCode)
continue
}
// 输出响应结果
fmt.Println("POST请求成功")
// 将item.ID写入itemid.txt文件
file, err := os.OpenFile("itemid.txt", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
fmt.Println("写入itemid.txt文件出错:", err)
continue
}
defer file.Close()
_, err = file.WriteString(strconv.Itoa(item.ID) + "\n")
if err != nil {
fmt.Println("写入itemid.txt文件出错:", err)
}
}
// 清空items切片
uniitems = nil
}
}
}
}
time.Sleep(20 * time.Second)
}
}