-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
283 lines (201 loc) · 5.57 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/SanjaySinghRajpoot/remote-crawler/config"
"github.com/SanjaySinghRajpoot/remote-crawler/models"
"github.com/SanjaySinghRajpoot/remote-crawler/routes"
"github.com/dghubble/oauth1"
"github.com/gin-gonic/gin"
"github.com/gocolly/colly"
"github.com/joho/godotenv"
"gopkg.in/robfig/cron.v2"
)
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type Choice struct {
Index int `json:"index"`
Message Message `json:"message"`
FinishReason string `json:"finish_reason"`
}
type Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
type ChatCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []Choice `json:"choices"`
Usage Usage `json:"usage"`
}
var cnt = 10
func goGetEnv(key string) string {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error, unable to load the .env file")
}
return os.Getenv(key)
}
func getShortUrl(url string) string {
baseUrl := "http://tinyurl.com/api-create.php?url="
urlToShorten := url
getReqUrl := baseUrl + urlToShorten
response, err := http.Get(getReqUrl)
if err != nil {
log.Fatal(err)
}
// read response body
body, error := io.ReadAll(response.Body)
if error != nil {
fmt.Println(error)
}
defer response.Body.Close()
return string(body)
}
// need a CRON Job for 24 hours set
func runCronJobs() {
// https://himalayas.app/jobs/developer
baseURL := "www.himalayas.app"
startingURL := "https://" + baseURL
// url := []string{baseURL}
c := colly.NewCollector(colly.AllowedDomains(), colly.Async(true))
// Set the concurrency limit
c.Limit(&colly.LimitRule{DomainGlob: "*", Parallelism: 2})
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.OnError(func(_ *colly.Response, err error) {
log.Println("Something went wrong:", err)
})
c.OnResponse(func(r *colly.Response) {
fmt.Println("Visited", r.Request.URL)
})
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
e.Request.Visit(e.Attr("href"))
// Print link
url := fmt.Sprintf("https://himalayas.app%s", link)
// get the shorted link
shortedUrl := getShortUrl(url)
name := e.ChildText("h2.text-xl.font-medium.text-gray-900")
saveJob := models.Job{
Name: name,
Description: "",
URL: shortedUrl,
}
result := config.DB.Where(models.Job{URL: shortedUrl}).FirstOrCreate(&saveJob)
if result.Error != nil {
fmt.Println("error")
return
}
})
fmt.Println("Starting crawl at: ", startingURL)
if err := c.Visit(startingURL); err != nil {
fmt.Println("Error on start of crawl: ", err)
}
c.Wait()
}
func sendTweeet(tweet string) {
consumerKey := goGetEnv("ConsumerKey")
consumerSecret := goGetEnv("ConsumerSecret")
accessToken := goGetEnv("AccessToken")
accessSecret := goGetEnv("AccessSecret")
// prompt := os.Getenv("PROMPT")
if consumerKey == "" || consumerSecret == "" || accessToken == "" || accessSecret == "" {
panic("Missing required environment variable")
}
fetched := tweet
// From here on, Twitter POST API
config := oauth1.NewConfig(consumerKey, consumerSecret)
token := oauth1.NewToken(accessToken, accessSecret)
httpClient := config.Client(oauth1.NoContext, token)
// Necessary - Lambdas timing out.
httpClient.Timeout = time.Second * 10
path := "https://api.twitter.com/2/tweets"
body := fmt.Sprintf(`{"text": "%s"}`, fetched)
bodyReader := strings.NewReader(body)
response, err := httpClient.Post(path, "application/json", bodyReader)
if err != nil {
log.Fatalf("Error when posting to twitter: %v", err)
}
defer response.Body.Close()
log.Printf("Response was OK: %v", response)
fmt.Println("tweet was successfully")
}
func makeTweetUsingGPT(tweet models.Job) string {
client := &http.Client{}
var data = strings.NewReader(fmt.Sprintf(`{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You will be provided with a job title, and your task is to create a attractive tweet using the job title with 270 characters limit"
},
{
"role": "user",
"content": "%s"
}
],
"temperature": 0,
"max_tokens": 256,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
}`, tweet.Name))
req, err := http.NewRequest("POST", "https://api.openai.com/v1/chat/completions", data)
if err != nil {
log.Fatal(err)
}
openAIKey := goGetEnv("OPEN_AI_KEY")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+openAIKey)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var responseObj ChatCompletionResponse
unmarshalError := json.Unmarshal([]byte(bodyText), &responseObj)
if unmarshalError != nil {
fmt.Println("Error:", err)
return ""
}
return responseObj.Choices[0].Message.Content
}
func getTweetFromDB() {
jobs := []models.Job{}
config.DB.Limit(3).Find(&jobs)
for _, job := range jobs {
tweet := makeTweetUsingGPT(job)
sendTweeet(tweet)
}
}
func main() {
config.Connect()
cronJob := cron.New()
// Cron Job set up to run on Weekly basis
cronJob.AddFunc("@1minute", func() {
runCronJobs()
})
cronJob.Start()
// getTweetFromDB()
// starting the golang server
router := gin.New()
routes.UserRoute(router)
router.Run(":8080")
}