This repository has been archived by the owner on Jun 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
godel.go
423 lines (344 loc) · 10.4 KB
/
godel.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package godel
import (
"encoding/json"
"github.com/levigross/grequests"
"fmt"
"math/rand"
"time"
"crypto/hmac"
"crypto/sha1"
"github.com/google/go-querystring/query"
"strings"
"path"
"os"
)
const clientID = "81e8a76e-1e02-4d17-9ba0-8a7020261b26";
const apiBaseURL = "htts://go-tellm.de/api/v2/"
//RED color
const RED = "DD5F5F"
//BLUE color
const BLUE = "DD5F5F"
//ORANGE color
const ORANGE = "FF9908"
//GREEN color
const GREEN = "9EC41C"
//LIMEGREEN color
const LIMEGREEN = "8ABDB0"
//YELLOW color
const YELLOW = "FFBA00"
//Pick a random color
const RANDOM = "FFFFFF"
const runes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
//Godel struct
type Godel struct {
Key string
DeviceUID string
AccessToken string
RefreshToken string
DistinctID string
Location Location
Debug bool
DownloadDir string
}
//NewClient create a new Godel Client, if no deviceuid is given a random one will be created
func NewClient(deviceuid string) (_ *Godel) {
uid := NewDeviceUID()
if deviceuid != "" {
uid = deviceuid
}
return &Godel{
Key: "aPLFAjyUusVPHgcgvlAxihthmRaiuqCjBsRCPLan",
DeviceUID: uid,
}
}
//NewDeviceUID creates a new DeviceUID use this, or create your own 63 char rand string
func NewDeviceUID() (uid string) {
b := make([]byte, 63) // create new 63 letter string
for i := range b {
b[i] = runes[rand.Int63() % int64(len(runes))]
}
return string(b)
}
//this handles request signing...
func signRequest(body, url, auth, method string) (hmacresult, ts string) {
timestamp := time.Now().UTC().Format(time.RFC3339)
parameters := ""
message := method + "%" + "api.go-tellm.com" + "%" + "443" + "%/api/v2/" + url + "%" + auth + "%" + timestamp + "%" + parameters + "%" + body
mac := hmac.New(sha1.New, []byte("aPLFAjyUusVPHgcgvlAxihthmRaiuqCjBsRCPLan"))
mac.Write([]byte(message))
expectedMAC := mac.Sum(nil)
hash := fmt.Sprintf("%x", expectedMAC)
return hash, timestamp
}
//sendRequest handles all sorts of request to the jodel api
func (g *Godel) sendRequest(endpoint string, parameters interface{}, payload interface{}, method string) *grequests.Response {
url := fmt.Sprintf("https://api.go-tellm.com/api/v2/%s", endpoint)
if(parameters != nil) {
params, _ := query.Values(parameters)
url = url + "?" + strings.ToLower(params.Encode())
}
payloadString, err := json.Marshal(payload)
if err != nil {
fmt.Println("error ", err)
}
if g.Debug {
fmt.Printf("Requst URL: %s\n", url)
fmt.Printf("Requst MODE: %s\n", method)
fmt.Printf("Requst Data: %s\n", payloadString)
}
//sign the request using stringified request and hmac
hmac, timestamp := signRequest(string(payloadString), endpoint, g.AccessToken, method)
ro := &grequests.RequestOptions{
UserAgent: "Jodel/4.28.1 Dalvik/2.1.0 (Linux; U; Android 5.1.1; D6503 Build/23.4.A.1.232)",
DisableCompression: false,
Headers: map[string]string{"Connection": "keep-alive","Content-Type": "application/json; charset=UTF-8","X-Timestamp": timestamp, "X-Client-Type": "android_4.28.1", "X-Api-Version": "0.2", "X-Authorization": "HMAC " + hmac,"Authorization": "Bearer " + g.AccessToken},
JSON: payload,
}
var resp *grequests.Response
switch method {
case "POST":
resp, err = grequests.Post(url, ro)
case "GET":
resp, err = grequests.Get(url, ro)
case "PUT":
resp, err = grequests.Put(url, ro)
case "DELETE":
resp, err = grequests.Put(url, ro)
default:
resp, err = grequests.Get(url, ro)
}
if err != nil {
fmt.Println("Unable to make request: ", err)
}
if g.Debug {
fmt.Printf("Status Code: %d\n", resp.StatusCode)
fmt.Printf("Response Headers: %v\n", resp.Header)
fmt.Printf("Response Content: %s\n", resp.String())
}
if g.DownloadDir != "" {
filePath := path.Join(g.DownloadDir, endpoint, "data.json")
if g.Debug {
fmt.Printf("Download option active, downloading response to: %s\n", filePath)
}
os.MkdirAll(path.Join(g.DownloadDir, endpoint), os.ModePerm)
f, err := os.Create(filePath)
defer f.Close()
if err != nil {
fmt.Println("error creating file ", err)
}
f.WriteString(resp.String())
}
return resp
}
func (g *Godel) get(endpoint string, parameters interface{}) *grequests.Response {
return g.sendRequest(endpoint, parameters, nil, "GET")
}
func (g *Godel) put(endpoint string, payload interface{}) *grequests.Response {
return g.sendRequest(endpoint, nil, payload, "PUT")
}
func (g *Godel) delete(endpoint string, payload interface{}) *grequests.Response {
return g.sendRequest(endpoint, nil, payload, "DELETE")
}
// Internal post function
func (g *Godel) post(endpoint string, payload interface{}) *grequests.Response {
return g.sendRequest(endpoint, nil, payload, "POST")
}
//GetRequestToken sends user location and deviceUID to get a request token
func (g *Godel) GetRequestToken(city string, country string, lat float64, lng float64) (err error) {
location := Location{
19,
city,
country,
city,
Coordinates{
lat,
lng,
},
}
login := LoginPayload{
clientID,
g.DeviceUID,
location,
}
response := g.post("users/", login)
x := LoginResponse{}
response.JSON(&x)
if(x.AccessToken != "" && response.Ok) {
g.Location = location
g.AccessToken = x.AccessToken
g.DistinctID = x.DistinctID
g.RefreshToken = x.RefreshToken
return nil
}
return nil
}
//GetNewAccessToken refresh the access token
func (g *Godel) GetNewAccessToken() {
data := NewAccessTokenRequest{
clientID,
g.DistinctID,
g.RefreshToken,
}
response := g.post("users/refreshToken", data)
token := NewAccessTokenResonse{}
response.JSON(&token)
if response.Ok {
g.AccessToken = token.AccessToken
}
}
//GetMostRecentPosts get the most recent posts for the current location
func (g *Godel) GetMostRecentPosts() []SinglePostResponse {
response := g.get("posts/location/", nil)
posts := LatestPostsResponse{}
response.JSON(&posts)
return posts.Posts
}
//GetMostPopularPosts get most popular posts for the current location
func (g *Godel) GetMostPopularPosts() []SinglePostResponse {
response := g.get("posts/location/popular", nil)
posts := LatestPostsResponse{}
response.JSON(&posts)
return posts.Posts
}
//GetMostDiscussedPosts get most discussed posts for the current location
func (g *Godel) GetMostDiscussedPosts(pagination interface{}) []SinglePostResponse {
response := g.get("posts/location/discussed", pagination)
posts := LatestPostsResponse{}
response.JSON(&posts)
return posts.Posts
}
//GetMyPosts returns a list of your own posts
func (g *Godel) GetMyPosts() []SinglePostResponse {
response := g.get("posts/mine", nil)
posts := LatestPostsResponse{}
response.JSON(&posts)
return posts.Posts
}
//GetMyPinnedPosts returns a list of your pinned posts
func (g *Godel) GetMyPinnedPosts() []SinglePostResponse {
response := g.get("posts/mine/pinned", nil)
posts := LatestPostsResponse{}
response.JSON(&posts)
return posts.Posts
}
//GetMyPopularPosts returns a list of your own posts
func (g *Godel) GetMyPopularPosts() []SinglePostResponse {
response := g.get("posts/mine/popular", nil)
posts := LatestPostsResponse{}
response.JSON(&posts)
return posts.Posts
}
//GetMyMostDiscussedPosts returns a list of your own posts
func (g *Godel) GetMyMostDiscussedPosts() []SinglePostResponse {
response := g.get("posts/mine/discussed", nil)
posts := LatestPostsResponse{}
response.JSON(&posts)
return posts.Posts
}
//GetMyRepliedPosts returns a list of posts you've replied to
func (g *Godel) GetMyRepliedPosts() []SinglePostResponse {
response := g.get("posts/mine/replies", nil)
post := LatestPostsResponse{}
response.JSON(&post)
return post.Posts
}
//GetMyVotedPosts returns a list of posts you've replied to
func (g *Godel) GetMyVotedPosts() []SinglePostResponse {
response := g.get("posts/mine/votes", nil)
post := LatestPostsResponse{}
response.JSON(&post)
return post.Posts
}
//GetMyPostsCombo returns a list of your last posts, your votes and comments
func (g *Godel) GetMyPostsCombo() ComboPostsResponse {
response := g.get("posts/mine/combo", nil)
post := ComboPostsResponse{}
response.JSON(&post)
return post
}
//GetKarma returns a list of your own posts
func (g *Godel) GetKarma() KarmaResponse {
response := g.get("users/karma", nil)
karma := KarmaResponse{}
response.JSON(&karma)
return karma
}
//GetPost returns a single post
func (g *Godel) GetPost(postid string) SinglePostResponse {
response := g.get("posts/" + postid, nil)
posts := SinglePostResponse{}
response.JSON(&posts)
return posts
}
//DeletePost is used to delete a post
func (g *Godel) DeletePost(postid string) bool {
response := g.delete("posts/"+ postid, nil)
return response.Ok
}
//UpvotePost upvote a selected Post
func (g *Godel) UpvotePost(postid string) SinglePostResponse {
response := g.put("posts/"+postid+"/upvote", nil)
post := voteResponse{}
response.JSON(&post)
return post.Post
}
//DownvotePost upvote a selected Post
func (g *Godel) DownvotePost(postid string) SinglePostResponse {
response := g.put("posts/"+postid+"/downvote", nil)
post := voteResponse{}
response.JSON(&post)
return post.Post
}
//PinPost upvote a selected Post
func (g *Godel) PinPost(postid string) bool {
response := g.put("posts/"+postid+"/pin", nil)
return response.Ok
}
//UnpinPost upvote a selected Post
func (g *Godel) UnpinPost(postid string) bool {
response := g.put("posts/"+postid+"/unpin", nil)
return response.Ok
}
//SendPost creates a new Jodel Post
func (g *Godel) SendPost(message, color string) bool {
payload := NewPost{
color,
g.Location,
message,
}
response := g.post("posts/", payload)
return response.Ok
}
//SendReply allows you to reply to a post
func (g *Godel) SendReply(postid, message, color string) SinglePostResponse {
reply := ReplyToPost{
postid,
color,
g.Location,
message,
}
response := g.post("posts", reply)
posts := SinglePostResponse{}
response.JSON(&posts)
return posts
}
//SendUserLocation can be used to change the location withouth the need to relogin
func (g *Godel) SendUserLocation(city string, country string, lat, lng float64) Location {
loc := Location{
19,
city,
country,
city,
Coordinates{
lat,
lng,
},
}
newLoc := &SendLocationRequest{
loc,
}
response := g.put("users/location", newLoc)
g.Location = loc
fmt.Println(response.String())
return loc
}