repltalk 2.0.5 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 021bec9878ab43bc85c7c1ef8fb628c34cece2c8643559b2969b955e13694f1a
4
- data.tar.gz: 519b81ab0f69d8b8f0289ecb85de62bfb5fb4a674f5574e70b5868d98882ef07
3
+ metadata.gz: dd7a674596285cb7f8a4d0c2f6a5a9a03629a128f103def8f25b8404c4a6dba5
4
+ data.tar.gz: 53337d06dffe548294ffad1019dcb2c89e96f9d307d57d711fee8f29a6f75298
5
5
  SHA512:
6
- metadata.gz: 0ff92dcc8ce300ab5c84c0753f51bb8788b695c4da858ec58f9abbf6d11f92f287589a49a36bf2bb346845ffe3d8f954b4113181718b57c86056ff9cb85eca40
7
- data.tar.gz: fe44468b7671355bdc1b143e9b032472a6715c2b40ebce9a02fbfb0bc1f4d8fe381cc80c5523f59483683465e5b68081352416b738b75741dbfea834a2c9608d
6
+ metadata.gz: 9147b972df6cf7c7c204b748340ad45ddcca8f3d06f8438852625aba108707a6293d2afdcc7728ce3780475237783adb3d846c5802b3333266d9e09f47bb0c6c
7
+ data.tar.gz: 9b11286a5337596e79c928a68af57519c65a9e700396be6869b777238f5b31a5bfa8db7cbf670d13ba5a131faf36897e65c943e15a17e8cc320bac626bec5740
data/lib/repltalk.rb CHANGED
@@ -1,642 +1,8 @@
1
- require "http"
2
- require "json"
3
- require_relative "queries"
1
+ require_relative "repltalk/client"
2
+ require_relative "repltalk/graphql"
3
+ require_relative "repltalk/structures"
4
4
 
5
- $BASE_URL = "https://repl.it"
6
-
7
- class Role
8
- attr_reader :name, :key, :tagline
9
-
10
- def initialize(role)
11
- @name = role["name"]
12
- @key = role["key"]
13
- @tagline = role["tagline"]
14
- end
15
-
16
- def to_s
17
- @name
18
- end
19
- end
20
-
21
-
22
-
23
- class Organization
24
- attr_reader :id, :name, :country, :postal_code, :state, :city, :timestamp
25
-
26
- def initialize(organization)
27
- @id = organization["id"]
28
- @name = organization["name"]
29
- @country = organization["country"]
30
- @postal_code = organization["postalCode"]
31
- @state = organization["state"]
32
- @city = organization["city"]
33
- @timestamp = organization["timeCreated"]
34
- end
35
-
36
- def to_s
37
- @name
38
- end
39
- end
40
-
41
-
42
-
43
- class Subscription
44
- attr_reader :id, :plan_id, :quantity, :timestamp
45
-
46
- def initialize(subscription)
47
- @id = subscription["id"]
48
- @plan_id = subscription["planId"]
49
- @quantity = subscription["quantity"]
50
- @timestamp = subscription["timeCreated"]
51
- end
52
-
53
- def to_s
54
- @plan_id
55
- end
56
- end
57
-
58
-
59
-
60
- class Language
61
- attr_reader :id, :key, :name, :tagline, :icon, :category
62
-
63
- def initialize(lang)
64
- @id = lang["id"]
65
- @key = lang["key"]
66
- @name = lang["displayName"]
67
- @tagline = lang["tagline"]
68
- @icon = lang["icon"]
69
- @category = lang["category"]
70
- end
71
-
72
- def to_s
73
- @id
74
- end
5
+ module ReplTalk
6
+ $BASE_URL = "https://replit.com"
7
+ VERSION = "4.0.0"
75
8
  end
76
-
77
-
78
-
79
- class Board
80
- attr_reader :id, :name, :color, :description
81
-
82
- def initialize(board)
83
- @id = board["id"]
84
- @name = board["name"]
85
- @color = board["color"]
86
- @description = board["description"]
87
- end
88
-
89
- def to_s
90
- @name
91
- end
92
- end
93
-
94
-
95
-
96
- class ReplComment
97
- attr_reader :id, :content, :author, :repl, :replies
98
-
99
- def initialize(client, comment)
100
- @client = client
101
-
102
- @id = comment["id"]
103
- @content = comment["body"]
104
- @author = comment["user"] == nil ? nil : User.new(@client, comment["user"])
105
- @repl = comment["repl"] == nil ? nil : Repl.new(@client, comment["repl"])
106
- @replies = comment["replies"] == nil ? nil : comment["replies"].map { |c| ReplComment.new(@client, c) }
107
- end
108
-
109
- def create_comment(content)
110
- c = @client.graphql(
111
- "ReplViewCreateReplCommentReply",
112
- Mutations.reply_repl_comment,
113
- input: {
114
- replCommentId: @id,
115
- body: content
116
- }
117
- )
118
- ReplComment.new(@client, c["createReplCommentReply"])
119
- end
120
-
121
- def edit(content)
122
- c = @client.graphql(
123
- "ReplViewCommentsUpdateReplComment",
124
- Mutations.edit_repl_comment,
125
- input: {
126
- id: @id,
127
- body: content
128
- }
129
- )
130
- ReplComment.new(@client, c["updateReplComment"])
131
- end
132
-
133
- def delete
134
- @client.graphql(
135
- "ReplViewCommentsDeleteReplComment",
136
- Mutations.delete_repl_comment,
137
- id: @id
138
- )
139
- nil
140
- end
141
-
142
- def to_s
143
- @content
144
- end
145
- end
146
-
147
-
148
-
149
- class Repl
150
- attr_reader :id, :url, :title, :author, :description, :timestamp, :size, :language, :img_url, :origin_url, :is_private, :is_always_on
151
-
152
- def initialize(client, repl)
153
- @client = client
154
-
155
- @id = repl["id"]
156
- @url = $BASE_URL + repl["url"]
157
- @title = repl["title"]
158
- @author = User.new(@client, repl["user"])
159
- @description = repl["description"]
160
- @timestamp = repl["timeCreated"]
161
- @size = repl["size"]
162
- @language = Language.new(repl["lang"])
163
- @image_url = repl["imageUrl"]
164
- @origin_url = repl["origin"] == nil ? nil : $BASE_URL + repl["origin"]["url"]
165
-
166
- @is_private = repl["isPrivate"]
167
- @is_always_on = repl["isAlwaysOn"]
168
- end
169
-
170
- def get_forks(count: 100, after: nil)
171
- f = @client.graphql(
172
- "ReplViewForks",
173
- Queries.get_repl_forks,
174
- url: @url,
175
- count: count,
176
- after: after
177
- )
178
- f["repl"]["publicForks"]["items"].map { |repl| Repl.new(@client, repl) }
179
- end
180
-
181
- def get_comments(count: nil, after: nil)
182
- c = @client.graphql(
183
- "ReplViewComments",
184
- Queries.get_repl_comments,
185
- url: @url,
186
- count: count,
187
- after: after
188
- )
189
- c["repl"]["comments"]["items"].map { |comment| ReplComment.new(@client, comment) }
190
- end
191
-
192
- def create_comment(content)
193
- c = @client.graphql(
194
- "ReplViewCreateReplComment",
195
- Mutations.create_repl_comment,
196
- input: {
197
- replId: @id,
198
- body: content
199
- }
200
- )
201
- ReplComment.new(@client, c["createReplComment"])
202
- end
203
-
204
- def to_s
205
- @title
206
- end
207
- end
208
-
209
-
210
-
211
- class Comment
212
- attr_reader :id, :url, :author, :content, :post_id, :is_answer, :vote_count, :timestamp, :can_vote, :has_voted
213
-
214
- def initialize(client, comment)
215
- @client = client
216
-
217
- @id = comment["id"]
218
- @url = $BASE_URL + comment["url"]
219
- @author = comment["user"] == nil ? "[deleted user]" : User.new(@client, comment["user"])
220
- @content = comment["body"]
221
- @post_id = comment["post"]["id"]
222
- @is_answer = comment["isAnswer"]
223
- @vote_count = comment["voteCount"]
224
- @timestamp = comment["timeCreated"]
225
-
226
- @can_vote = comment["canVote"]
227
- @has_voted = comment["hasVoted"]
228
- end
229
-
230
- def get_post
231
- p = @client.graphql(
232
- "post",
233
- Queries.get_post,
234
- id: @post_id
235
- )
236
- Post.new(@client, p["post"])
237
- end
238
-
239
- def get_comments
240
- c = @client.graphql(
241
- "comment",
242
- Queries.get_comments_comments,
243
- id: @id
244
- )
245
- c["comment"]["comments"].map { |comment| Comment.new(@client, comment) }
246
- end
247
-
248
- def get_parent
249
- c = @client.graphql(
250
- "comment",
251
- Queries.get_parent_comment,
252
- id: @id
253
- )
254
- c["comment"]["parentComment"] == nil ? nil : Comment.new(@client, c["comment"]["parentComment"])
255
- end
256
-
257
- def create_comment(content)
258
- c = @client.graphql(
259
- "createComment",
260
- Mutations.create_comment,
261
- input: {
262
- postId: @post_id,
263
- commentId: @id,
264
- body: content
265
- }
266
- )
267
- Comment.new(@client, c["createComment"]["comment"])
268
- end
269
-
270
- def edit(content)
271
- c = @client.graphql(
272
- "updateComment",
273
- Mutations.edit_comment,
274
- input: {
275
- id: @id,
276
- body: content
277
- }
278
- )
279
- Comment.new(@client, c["updateComment"]["comment"])
280
- end
281
-
282
- def delete
283
- @client.graphql(
284
- "deleteComment",
285
- Mutations.delete_comment,
286
- id: @id
287
- )
288
- nil
289
- end
290
-
291
- def report(reason)
292
- @client.graphql(
293
- "createBoardReport",
294
- Mutations.report_comment,
295
- id: @id,
296
- reason: reason
297
- )
298
- nil
299
- end
300
-
301
- def to_s
302
- @content
303
- end
304
- end
305
-
306
-
307
-
308
- class Post
309
- attr_reader :id, :url, :repl, :board, :title, :author, :answer, :content, :preview, :timestamp, :vote_count, :comment_count, :can_vote, :has_voted, :is_answered, :is_answerable, :is_hidden, :is_pinned, :is_locked, :is_announcement
310
-
311
- def initialize(client, post)
312
- @client = client
313
-
314
- @id = post["id"]
315
- @url = $BASE_URL + post["url"]
316
- @title = post["title"]
317
- @content = post["body"]
318
- @preview = post["preview"]
319
- @timestamp = post["timeCreated"]
320
-
321
- @board = Board.new(post["board"])
322
- @repl = post["repl"] == nil ? nil : Repl.new(@client, post["repl"])
323
- @author = post["user"] == nil ? nil : User.new(@client, post["user"])
324
- @answer = post["answer"] == nil ? nil : Comment.new(@client, post["answer"])
325
-
326
- @vote_count = post["voteCount"]
327
- @comment_count = post["commentCount"]
328
-
329
- @can_vote = post["canVote"]
330
- @has_voted = post["hasVoted"]
331
-
332
- @is_answered = post["isAnswered"]
333
- @is_answerable = post["isAnswerable"]
334
-
335
- @is_hidden = post["isHidden"]
336
- @is_pinned = post["isPinned"]
337
- @is_locked = post["isLocked"]
338
- @is_announcement = post["isAnnouncement"]
339
- end
340
-
341
- def get_comments(order: "new", count: nil, after: nil)
342
- c = @client.graphql(
343
- "post",
344
- Queries.get_posts_comments,
345
- postId: @id,
346
- order: order,
347
- count: count,
348
- after: after
349
- )
350
- c["post"]["comments"]["items"].map { |comment| Comment.new(@client, comment) }
351
- end
352
-
353
- def get_upvotes(count: nil)
354
- u = @client.graphql(
355
- "post",
356
- Queries.get_posts_upvoters,
357
- id: @id,
358
- count: count
359
- )
360
- u["post"]["votes"]["items"].map { |vote| User.new(@client, vote["user"]) }
361
- end
362
-
363
- def create_comment(content)
364
- c = @client.graphql(
365
- "createComment",
366
- Mutations.create_comment,
367
- input: {
368
- postId: @id,
369
- body: content
370
- }
371
- )
372
- Comment.new(@client, c["createComment"]["comment"])
373
- end
374
-
375
- def edit(title: @title, content: @content, repl_id: @repl.id, show_hosted: false)
376
- p = @client.graphql(
377
- "updatePost",
378
- Mutations.edit_post,
379
- input: {
380
- id: @id,
381
- title: title,
382
- body: content,
383
- replId: repl_id,
384
- showHosted: show_hosted
385
- }
386
- )
387
- Post.new(@client, p["updatePost"]["post"])
388
- end
389
-
390
- def delete
391
- @client.graphql(
392
- "deletePost",
393
- Mutations.delete_post,
394
- id: @id
395
- )
396
- nil
397
- end
398
-
399
- def report(reason)
400
- @client.graphql(
401
- "createBoardReport",
402
- Mutations.report_post,
403
- id: @id,
404
- reason: reason
405
- )
406
- nil
407
- end
408
-
409
- def to_s
410
- @title
411
- end
412
- end
413
-
414
-
415
-
416
- class User
417
- attr_reader :id, :username, :name, :pfp, :bio, :cycles, :is_hacker, :timestamp, :subscription, :roles, :organization, :languages
418
-
419
- def initialize(client, user)
420
- return nil if user == nil
421
- @client = client
422
-
423
- @id = user["id"]
424
- @username = user["username"]
425
- @name = user["fullName"]
426
- @pfp = user["image"]
427
- @bio = user["bio"]
428
- @cycles = user["karma"]
429
- @is_hacker = user["isHacker"]
430
- @timestamp = user["timeCreated"]
431
- @subscription = user["subscription"] == nil ? nil : Subscription.new(user["subscription"])
432
- @roles = user["roles"].map { |role| Role.new(role) }
433
- @organization = user["organization"] == nil ? nil : Organization.new(user["organization"])
434
- @languages = user["languages"].map { |lang| Language.new(lang) }
435
- end
436
-
437
- def get_posts(order: "new", count: nil, after: nil)
438
- p = @client.graphql(
439
- "user",
440
- Queries.get_user_posts,
441
- username: @username,
442
- order: order,
443
- count: count,
444
- after: after
445
- )
446
- p["user"]["posts"]["items"].map { |post| Post.new(@client, post) }
447
- end
448
-
449
- def get_comments(order: "new", count: nil, after: nil)
450
- c = @client.graphql(
451
- "user",
452
- Queries.get_user_comments,
453
- username: @username,
454
- order: order,
455
- count: count,
456
- after: after
457
- )
458
- c["user"]["comments"]["items"].map { |comment| Comment.new(@client, comment) }
459
- end
460
-
461
- def get_repls(count: nil, order: nil, direction: nil, before: nil, after: nil, pinnedReplsFirst: nil, showUnnamed: nil)
462
- r = @client.graphql(
463
- "user",
464
- Queries.get_user_repls,
465
- username: @username,
466
- order: order,
467
- count: count,
468
- direction: direction,
469
- before: before,
470
- after: after,
471
- pinnedReplsFirst: pinnedReplsFirst,
472
- showUnnamed: showUnnamed
473
- )
474
- r["user"]["publicRepls"]["items"].map { |repl| Repl.new(@client, repl) }
475
- end
476
-
477
- def to_s
478
- @username
479
- end
480
- end
481
-
482
-
483
-
484
- class LeaderboardUser < User
485
- attr_reader :cycles_since
486
-
487
- def initialize(client, user)
488
- super(client, user)
489
- @cycles_since = user["karmaSince"]
490
- end
491
- end
492
-
493
-
494
-
495
- class Client
496
- attr_writer :sid
497
-
498
- def initialize(sid=nil)
499
- @sid = sid
500
- end
501
-
502
- def graphql(name, query, **variables)
503
- payload = {
504
- operationName: name,
505
- query: query,
506
- variables: variables.to_json
507
- }
508
- r = HTTP
509
- .cookies(
510
- "connect.sid": @sid
511
- )
512
- .headers(
513
- referer: "#{$BASE_URL}/@CodingCactus/repltalk",
514
- "X-Requested-With": "ReplTalk"
515
- )
516
- .post(
517
- "#{$BASE_URL}/graphql",
518
- form: payload
519
- )
520
- begin data = JSON.parse(r)
521
- rescue
522
- puts "\e[31mERROR\n#{r}\e[0m"
523
- return nil
524
- end
525
- if data.include?("errors")
526
- puts "\e[31mERROR\n#{r}\e[0m"
527
- return nil
528
- end
529
- data = data["data"] if data.include?("data")
530
- data
531
- end
532
-
533
- def get_user(name)
534
- u = graphql(
535
- "userByUsername",
536
- Queries.get_user,
537
- username: name
538
- )
539
- return nil if u == nil || u["user"] == nil
540
- User.new(self, u["user"])
541
- end
542
-
543
- def get_user_by_id(id)
544
- u = graphql(
545
- "user",
546
- Queries.get_user_by_id,
547
- user_id: id
548
- )
549
- return nil if u == nil || u["user"] == nil
550
- User.new(self, u["user"])
551
- end
552
-
553
- def get_post(id)
554
- p = graphql(
555
- "post",
556
- Queries.get_post,
557
- id: id
558
- )
559
- return nil if p == nil || p["post"] == nil
560
- Post.new(self, p["post"])
561
- end
562
-
563
- def get_comment(id)
564
- c = graphql(
565
- "comment",
566
- Queries.get_comment,
567
- id: id
568
- )
569
- return nil if c == nil || c["comment"] == nil
570
- Comment.new(self, c["comment"])
571
- end
572
-
573
- def get_repl(url)
574
- r = graphql(
575
- "ReplView",
576
- Queries.get_repl,
577
- url: url
578
- )
579
- return nil if r == nil || r["repl"] == nil
580
- Repl.new(self, r["repl"])
581
- end
582
-
583
- def get_repl_comment(id)
584
- c = graphql(
585
- "ReplViewComment",
586
- Queries.get_repl_comment,
587
- id: id
588
- )
589
- return nil if c == nil || c["replComment"] == nil
590
- ReplComment.new(self, c["replComment"])
591
- end
592
-
593
- def get_board(name)
594
- b = graphql(
595
- "boardBySlug",
596
- Queries.get_board,
597
- slug: name
598
- )
599
- return nil if b == nil || b["board"] == nil
600
- Board.new(b["board"])
601
- end
602
-
603
- def get_leaderboard(count: nil, since: nil, after: nil)
604
- u = graphql(
605
- "LeaderboardQuery",
606
- Queries.get_leaderboard,
607
- count: count,
608
- since: since,
609
- after: after
610
- )
611
- u["leaderboard"]["items"].map { |user| LeaderboardUser.new(self, user) }
612
- end
613
-
614
- def get_posts(board: "all", order: "new", count: nil, after: nil, search: nil, languages: nil)
615
- p = graphql(
616
- "PostsFeed",
617
- Queries.get_posts,
618
- boardSlugs: [board],
619
- order: order,
620
- count: count,
621
- after: after,
622
- searchQuery: search,
623
- languages: languages
624
- )
625
- p["posts"]["items"].map { |post| Post.new(self, post) }
626
- end
627
-
628
- def create_post(board_name, title, content, repl_id: nil, show_hosted: false)
629
- p = graphql(
630
- "createPost",
631
- Mutations.create_post,
632
- input: {
633
- boardId: get_board(board_name).id,
634
- title: title,
635
- body: content,
636
- replId: repl_id,
637
- showHosted: show_hosted
638
- }
639
- )
640
- Post.new(self, p["createPost"]["post"])
641
- end
642
- end