-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContestScraperAPI.go
More file actions
270 lines (240 loc) · 6.64 KB
/
ContestScraperAPI.go
File metadata and controls
270 lines (240 loc) · 6.64 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/gorilla/mux"
)
type Contest struct {
Code string `json:"Code"`
Name string `json:"Name"`
Start string `json:"Start"`
End string `json:"End"`
Url string `json:"Url"`
}
type Profile struct {
UserName string `json:"UserName"`
Country string `json:"Country"`
City string `json:"City"`
Link string `json:"Link"`
Role string `json:"Role"`
Institution string `json:"Institution"`
CodechefRating string `json:"CodechefRating"`
GlobalRank string `json:"GlobalRank"`
CountryRank string `json:"CountryRank"`
}
type Contests []Contest
type JsonContestObject struct {
Data []Contest
}
// func fetchPagedContest(whichContest int, page int) Contests {
// fmt.Println("Page Number ", page)
// res, err := http.Get("https://www.codechef.com/contests")
// if err != nil {
// fmt.Println("Error")
// }
// doc, err := goquery.NewDocumentFromReader(res.Body)
// if err != nil {
// fmt.Println("Error")
// }
// allContest := Contests{}
// doc.Find(".dataTable").Each(func(i int, s *goquery.Selection) {
// if whichContest == i {
// code := "Code"
// name := "Name"
// start := "Start"
// end := "End"
// s.Find("tr").Each(func(i int, r *goquery.Selection) {
// if i > ((page*10)-10) && i <= (page*10) {
// r.Find("td").Each(func(i int, t *goquery.Selection) {
// switch i {
// case 0:
// code = t.Text()
// case 1:
// name = t.Text()
// name = strings.TrimSpace(name)
// case 2:
// start = t.Text()
// case 3:
// end = t.Text()
// }
// })
// randomContest := Contest{code, name, start, end}
// allContest = append(allContest, randomContest)
// //fmt.Println(code,name,start,end)
// }
// })
// }
// })
// return allContest
// }
func fetchContest(whichContest int) Contests {
res, err := http.Get("https://www.codechef.com/contests")
if err != nil {
fmt.Println("Error")
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
fmt.Println("Error")
}
allContest := Contests{}
doc.Find(".dataTable").Each(func(i int, s *goquery.Selection) {
if whichContest == i {
switch i {
case 0:
fmt.Println("***Ongoing Contest***")
case 1:
fmt.Println("***Future Contest***")
case 2:
fmt.Println("Past Contest")
}
code := "Code"
name := "Name"
start := "Start"
end := "End"
url := "Url"
s.Find("tr").Each(func(i int, r *goquery.Selection) {
if i > 0 {
r.Find("td").Each(func(i int, t *goquery.Selection) {
switch i {
case 0:
code = t.Text()
case 1:
name = t.Text()
name = strings.TrimSpace(name)
case 2:
start = t.Text()
case 3:
end = t.Text()
}
})
r.Find("a").Each(func(q int, qu *goquery.Selection) {
fmt.Println("**dot**dot**")
urlDup, ok := qu.Attr("href")
if ok {
url = urlDup
}
// fmt.Println(qu.Attr("href"))
})
randomContest := Contest{code, name, start, end, url}
allContest = append(allContest, randomContest)
fmt.Println(code, name, start, end)
}
})
}
})
fmt.Println(allContest)
//randomArray := jsonContestObject{allContest}
//fmt.Println(randomArray)
return allContest
}
func fetchProfile() {
res, err := http.Get("https://www.codechef.com/users/venky_2801")
if err != nil {
fmt.Println("Something Went Wrong!")
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
fmt.Println("Something Went Wrong!")
}
//user-details-container plr10
userDetatilsContainer := doc.Find(".user-details-container").First()
userDetails := userDetatilsContainer.Find(".user-details").First()
FullName := userDetails.Find("h2").Text()
var stars, username, countryname, state, city, link, profession, institution string
userDetails.Find("li").Each(func(j int, k *goquery.Selection) {
switch j {
case 0:
k.Find("span").Each(func(l int, m *goquery.Selection) {
if l == 0 {
stars = m.Find("span").Text()
} else {
username = m.Find("span").Text()
}
})
case 1:
countryname = k.Find("user-country-name").Text()
case 2:
state = k.Find("span").Text()
case 3:
city = k.Find("span").Text()
case 4:
link = k.Find("a").Text()
case 5:
profession = k.Find("span").Text()
case 6:
institution = k.Find("span").Text()
}
})
fmt.Println(FullName, stars, username, countryname, state, city, link, profession, institution)
}
func allOngoingContest(w http.ResponseWriter, r *http.Request) {
onGoingContestArrayList := fetchContest(0)
ran := JsonContestObject{onGoingContestArrayList}
b, err := json.Marshal(ran)
if err != nil {
fmt.Println("Random ", b)
} else {
fmt.Println("Error", string(b))
}
json.NewEncoder(w).Encode(ran)
}
func allFutureContest(w http.ResponseWriter, r *http.Request) {
futureContestArrayList := fetchContest(1)
ran := JsonContestObject{futureContestArrayList}
b, err := json.Marshal(ran)
if err != nil {
fmt.Println("Random ", b)
} else {
fmt.Println("Error", string(b))
}
json.NewEncoder(w).Encode(ran)
}
func allPastContest(w http.ResponseWriter, r *http.Request) {
// vars := mux.Vars(r)
// key := vars["page"]
// keyPage, err := strconv.Atoi(key)
// pastContestArrayList := fetchPagedContest(2, keyPage)
pastContestArrayList := fetchContest(2)
ran := JsonContestObject{pastContestArrayList}
b, err := json.Marshal(ran)
if err != nil {
fmt.Println("Random ", b)
} else {
fmt.Println("Error", string(b))
}
json.NewEncoder(w).Encode(ran)
}
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to Codechef API HomePage \n")
fmt.Fprintf(w, "To get list of Ongoing Contest go to /ongoing \n")
fmt.Fprintf(w, "To get list of Future Contest go to /future \n")
fmt.Fprintf(w, "TO get list of Past Contest go to /past \n")
}
func HandleRequest() {
fmt.Println("Listening")
// http.HandleFunc("/", homePage)
// http.HandleFunc("/ongoing", allOngoingContest)
// http.HandleFunc("/future", allFutureContest)
// //http.HandleFunc("/past/{page}", allPastContest)
// http.HandleFunc("/past", allPastContest)
// log.Fatal(http.ListenAndServe(":80", nil))
port := os.Getenv("PORT")
fmt.Println(port)
if port == "" {
log.Fatal("$PORT must be set")
}
myRouter := mux.NewRouter().StrictSlash(true)
myRouter.HandleFunc("/", homePage)
myRouter.HandleFunc("/ongoing", allOngoingContest)
myRouter.HandleFunc("/future", allFutureContest)
myRouter.HandleFunc("/past", allPastContest)
log.Fatal(http.ListenAndServe(":"+port, myRouter))
}
func main() {
HandleRequest()
}