-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
246 lines (198 loc) · 5.67 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"bufio"
"encoding/xml"
"encoding/json"
"fmt"
_"time"
"io/ioutil"
"log"
"net/http"
"bytes"
_"github.com/lib/pq"
"database/sql"
"os"
"github.com/ilyakaznacheev/cleanenv"
)
// Config Settings
type Config struct {
Telegram struct {
Token string `yaml:"Token"`
ChatID int `yaml:"ChatID"`
} `yaml:"Telegram"`
Discord struct {
WebHook string `yaml:"WebHook"`
} `yaml:"Discord"`
Database struct {
Host string `yaml:"Host"`
Port int `yaml:"Port"`
User string `yaml:"User"`
Password string `yaml:"Password"`
DatabaseName string `yaml:"DatabaseName"`
} `yaml:"Database"`
}
var cfg Config
var err = cleanenv.ReadConfig("./config.yml", &cfg)
// Config Settings
var (
WarningLogger *log.Logger
InfoLogger *log.Logger
ErrorLogger *log.Logger
)
type Rss struct {
XMLName xml.Name `xml:"rss"`
Channel []Channel `xml:"channel"`
}
type Channel struct {
XMLName xml.Name `xml:"channel"`
Items []Items `xml:"item"`
}
type Items struct {
XMLName xml.Name `xml:"item"`
Title string `xml:"title"`
Link string `xml:"guid"`
PubDate string `xml:"pubDate"`
}
func CheckError(err error) {
if err != nil {
ErrorLogger.Println(err)
}
}
func ReadSource() []string {
file, err := os.Open("./sources.txt")
CheckError(err)
defer file.Close()
scanner := bufio.NewScanner(file)
mySlice := []string{}
for scanner.Scan() {
mySlice = append(mySlice, scanner.Text())
}
return mySlice
}
func GetWriteUps(WeblogURL string) [][]string {
//InfoLogger.Println("Start Load Writeups.")
resp, err := http.Get(WeblogURL)
CheckError(err)
defer resp.Body.Close()
byteValue, err := ioutil.ReadAll(resp.Body)
CheckError(err)
// Read XML Response
var rss Rss
xml.Unmarshal(byteValue, &rss)
Writeup := [][]string{}
for i := 0; i < len(rss.Channel[0].Items); i++ {
title := rss.Channel[0].Items[i].Title
link := rss.Channel[0].Items[i].Link
pub_date := rss.Channel[0].Items[i].PubDate
Writeup = append(Writeup, []string{title, link, pub_date})
// fmt.Println(Writeup)
}
// Writeup = append(Writeup, rss.Channel[0].Items[i].Title)
// Writeup = append(Writeup, rss.Channel[0].Items[i].Link)
// Writeup = append(Writeup, rss.Channel[0].Items[i].PubDate)
return Writeup
}
func CheckPost(Writeups [][]string ) int {
// Database
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",cfg.Database.Host, cfg.Database.Port, cfg.Database.User, cfg.Database.Password, cfg.Database.DatabaseName)
db, err := sql.Open("postgres", psqlconn)
CheckError(err)
defer db.Close()
err = db.Ping()
CheckError(err)
// fmt.Println("Successfully connected!")
// Check Posts Table Exist Or Not.
CreateQuery := `
CREATE TABLE IF NOT EXISTS "Posts"
(
id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ),
"Tittle" text NOT NULL,
"Link" text NOT NULL,
"PubDate" text ,
CONSTRAINT posts_pkey PRIMARY KEY (id)
)`
_, err= db.Exec(CreateQuery)
CheckError(err)
var result int
for i:=0; i<len(Writeups); i++ {
title := Writeups[i][0]
link := Writeups[i][1]
pub_date := Writeups[i][2]
SelectQuery := `SELECT * FROM "Posts" WHERE "Tittle"=$1 OR "Link"=$2`
rows, err := db.Query(SelectQuery, title, link)
CheckError(err)
if !rows.Next() {
InsertQuery := `insert into "Posts"("Tittle", "Link", "PubDate") values($1, $2, $3)`
_, err = db.Query(InsertQuery, title, link, pub_date)
CheckError(err)
InfoLogger.Println("Added To Database: ",title)
DiscordServer(title, link, pub_date)
TelegramChannel(title, link, pub_date)
result = result + 1
// return result
}
}
return result
}
func DiscordServer(title string, link string, pub_date string) {
// rawJSON := []byte(`{
// "content": "[%s](%s)",
// "embeds": null
// }`)
Text := fmt.Sprintf("%s\n%s\n%s", title, pub_date, link )
body, _ := json.Marshal(map[string]interface{}{
"content": Text,
})
req, err := http.Post(
cfg.Discord.WebHook,
"application/json",
bytes.NewBuffer(body),
)
CheckError(err)
InfoLogger.Println("Added To Discord: ",title)
defer req.Body.Close()
}
func TelegramChannel(title string, link string, pub_date string) {
URL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", cfg.Telegram.Token)
Text := fmt.Sprintf("%s\n%s\n%s", title, pub_date, link )
body, _ := json.Marshal(map[string]interface{}{
"chat_id": cfg.Telegram.ChatID,
"text" : Text,
"parse_mode" : "Markdown",
})
req, err := http.Post(
URL,
"application/json",
bytes.NewBuffer(body),
)
CheckError(err)
InfoLogger.Println("Added To Telegram Channel: ",title)
defer req.Body.Close()
}
func logger() {
file, err := os.OpenFile("info.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
CheckError(err)
InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
WarningLogger = log.New(file, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile)
ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
}
func main() {
// Start Logger Function
logger()
// InfoLogger.Println("Starting the application...")
// InfoLogger.Println("Something noteworthy happened")
// WarningLogger.Println("There is something you should know about")
// ErrorLogger.Println("Something went wrong")
// Read Resources File
SourcesList := ReadSource()
for i := 0; i < len(SourcesList); i++ {
WeblogURL := SourcesList[i]
InfoLogger.Println("Check :", SourcesList[i])
Writeups := GetWriteUps(WeblogURL)
result := CheckPost(Writeups)
if result!=0 {
InfoLogger.Println(result,"New Writeup.")
}
}
InfoLogger.Println("Not New Writeups Available.")
}