repltalk 2.0.4 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7698843f4821cfa70c70212835ca464d12aef28d561f918f95d644f3c4656b7
4
- data.tar.gz: 6386104c0526471dc1501e90e2f9cbfd22efef4a8de53c0490b1942478283815
3
+ metadata.gz: 66f29deaf911567d3c4c959a6687c350f0764d758664654cb65b1aaa076c037f
4
+ data.tar.gz: b4f3293b1231a4ef20cf9923f877fb939008375b9410607a8e8a593527f97a6d
5
5
  SHA512:
6
- metadata.gz: d3803dcf7ec09ecbbb63fd4819f48f4464138264a462e384a923747374525c37764e3f50fcdc796e7d581602179d24e0b9d3e93b5507d4020ebbb13a3846145a
7
- data.tar.gz: ee2a048806996c587420ca9a6ec33a9ab45de7a357c8e0c61c177b628bfe9796d75396da0d6e3746b3ec9125ac0700757679fb35dc987c73ace13c8ba31ba8e5
6
+ metadata.gz: aaeb2dd357f62d1ce23a415ca34cd46e18e14e8087a11c440341be3ec406581d3c80f2eddaf16247ca9e7bcc6ddd59d22f110112372fde0c9c14600d6e195ea4
7
+ data.tar.gz: 060fbd5f0a744a756dd83341adf6f3a4a2965d0c73cae741b0b763d6104ae60faeb76d5ca04bb279a8856e12a423dad4ed27e7325db88f460b3e14d9770d8fa2
data/lib/repltalk.rb CHANGED
@@ -1,641 +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 = "3.2.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, :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
- @size = repl["size"]
161
- @language = Language.new(repl["lang"])
162
- @image_url = repl["imageUrl"]
163
- @origin_url = repl["origin"] == nil ? nil : $BASE_URL + repl["origin"]["url"]
164
-
165
- @is_private = repl["isPrivate"]
166
- @is_always_on = repl["isAlwaysOn"]
167
- end
168
-
169
- def get_forks(count: 100, after: nil)
170
- f = @client.graphql(
171
- "ReplViewForks",
172
- Queries.get_repl_forks,
173
- url: @url,
174
- count: count,
175
- after: after
176
- )
177
- f["repl"]["publicForks"]["items"].map { |repl| Repl.new(@client, repl) }
178
- end
179
-
180
- def get_comments(count: nil, after: nil)
181
- c = @client.graphql(
182
- "ReplViewComments",
183
- Queries.get_repl_comments,
184
- url: @url,
185
- count: count,
186
- after: after
187
- )
188
- c["repl"]["comments"]["items"].map { |comment| ReplComment.new(@client, comment) }
189
- end
190
-
191
- def create_comment(content)
192
- c = @client.graphql(
193
- "ReplViewCreateReplComment",
194
- Mutations.create_repl_comment,
195
- input: {
196
- replId: @id,
197
- body: content
198
- }
199
- )
200
- ReplComment.new(@client, c["createReplComment"])
201
- end
202
-
203
- def to_s
204
- @title
205
- end
206
- end
207
-
208
-
209
-
210
- class Comment
211
- attr_reader :id, :url, :author, :content, :post_id, :is_answer, :vote_count, :timestamp, :can_vote, :has_voted
212
-
213
- def initialize(client, comment)
214
- @client = client
215
-
216
- @id = comment["id"]
217
- @url = $BASE_URL + comment["url"]
218
- @author = comment["user"] == nil ? "[deleted user]" : User.new(@client, comment["user"])
219
- @content = comment["body"]
220
- @post_id = comment["post"]["id"]
221
- @is_answer = comment["isAnswer"]
222
- @vote_count = comment["voteCount"]
223
- @timestamp = comment["timeCreated"]
224
-
225
- @can_vote = comment["canVote"]
226
- @has_voted = comment["hasVoted"]
227
- end
228
-
229
- def get_post
230
- p = @client.graphql(
231
- "post",
232
- Queries.get_post,
233
- id: @post_id
234
- )
235
- Post.new(@client, p["post"])
236
- end
237
-
238
- def get_comments
239
- c = @client.graphql(
240
- "comment",
241
- Queries.get_comments_comments,
242
- id: @id
243
- )
244
- c["comment"]["comments"].map { |comment| Comment.new(@client, comment) }
245
- end
246
-
247
- def get_parent
248
- c = @client.graphql(
249
- "comment",
250
- Queries.get_parent_comment,
251
- id: @id
252
- )
253
- c["comment"]["parentComment"] == nil ? nil : Comment.new(@client, c["comment"]["parentComment"])
254
- end
255
-
256
- def create_comment(content)
257
- c = @client.graphql(
258
- "createComment",
259
- Mutations.create_comment,
260
- input: {
261
- postId: @post_id,
262
- commentId: @id,
263
- body: content
264
- }
265
- )
266
- Comment.new(@client, c["createComment"]["comment"])
267
- end
268
-
269
- def edit(content)
270
- c = @client.graphql(
271
- "updateComment",
272
- Mutations.edit_comment,
273
- input: {
274
- id: @id,
275
- body: content
276
- }
277
- )
278
- Comment.new(@client, c["updateComment"]["comment"])
279
- end
280
-
281
- def delete
282
- @client.graphql(
283
- "deleteComment",
284
- Mutations.delete_comment,
285
- id: @id
286
- )
287
- nil
288
- end
289
-
290
- def report(reason)
291
- @client.graphql(
292
- "createBoardReport",
293
- Mutations.report_comment,
294
- id: @id,
295
- reason: reason
296
- )
297
- nil
298
- end
299
-
300
- def to_s
301
- @content
302
- end
303
- end
304
-
305
-
306
-
307
- class Post
308
- 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
309
-
310
- def initialize(client, post)
311
- @client = client
312
-
313
- @id = post["id"]
314
- @url = $BASE_URL + post["url"]
315
- @title = post["title"]
316
- @content = post["body"]
317
- @preview = post["preview"]
318
- @timestamp = post["timeCreated"]
319
-
320
- @board = Board.new(post["board"])
321
- @repl = post["repl"] == nil ? nil : Repl.new(@client, post["repl"])
322
- @author = post["user"] == nil ? nil : User.new(@client, post["user"])
323
- @answer = post["answer"] == nil ? nil : Comment.new(@client, post["answer"])
324
-
325
- @vote_count = post["voteCount"]
326
- @comment_count = post["commentCount"]
327
-
328
- @can_vote = post["canVote"]
329
- @has_voted = post["hasVoted"]
330
-
331
- @is_answered = post["isAnswered"]
332
- @is_answerable = post["isAnswerable"]
333
-
334
- @is_hidden = post["isHidden"]
335
- @is_pinned = post["isPinned"]
336
- @is_locked = post["isLocked"]
337
- @is_announcement = post["isAnnouncement"]
338
- end
339
-
340
- def get_comments(order: "new", count: nil, after: nil)
341
- c = @client.graphql(
342
- "post",
343
- Queries.get_posts_comments,
344
- postId: @id,
345
- order: order,
346
- count: count,
347
- after: after
348
- )
349
- c["post"]["comments"]["items"].map { |comment| Comment.new(@client, comment) }
350
- end
351
-
352
- def get_upvotes(count: nil)
353
- u = @client.graphql(
354
- "post",
355
- Queries.get_posts_upvoters,
356
- id: @id,
357
- count: count
358
- )
359
- u["post"]["votes"]["items"].map { |vote| User.new(@client, vote["user"]) }
360
- end
361
-
362
- def create_comment(content)
363
- c = @client.graphql(
364
- "createComment",
365
- Mutations.create_comment,
366
- input: {
367
- postId: @id,
368
- body: content
369
- }
370
- )
371
- Comment.new(@client, c["createComment"]["comment"])
372
- end
373
-
374
- def edit(title: @title, content: @content, repl_id: @repl.id, show_hosted: false)
375
- p = @client.graphql(
376
- "updatePost",
377
- Mutations.edit_post,
378
- input: {
379
- id: @id,
380
- title: title,
381
- body: content,
382
- replId: repl_id,
383
- showHosted: show_hosted
384
- }
385
- )
386
- Post.new(@client, p["updatePost"]["post"])
387
- end
388
-
389
- def delete
390
- @client.graphql(
391
- "deletePost",
392
- Mutations.delete_post,
393
- id: @id
394
- )
395
- nil
396
- end
397
-
398
- def report(reason)
399
- @client.graphql(
400
- "createBoardReport",
401
- Mutations.report_post,
402
- id: @id,
403
- reason: reason
404
- )
405
- nil
406
- end
407
-
408
- def to_s
409
- @title
410
- end
411
- end
412
-
413
-
414
-
415
- class User
416
- attr_reader :id, :username, :name, :pfp, :bio, :cycles, :is_hacker, :timestamp, :subscription, :roles, :organization, :languages
417
-
418
- def initialize(client, user)
419
- return nil if user == nil
420
- @client = client
421
-
422
- @id = user["id"]
423
- @username = user["username"]
424
- @name = user["fullName"]
425
- @pfp = user["image"]
426
- @bio = user["bio"]
427
- @cycles = user["karma"]
428
- @is_hacker = user["isHacker"]
429
- @timestamp = user["timeCreated"]
430
- @subscription = user["subscription"] == nil ? nil : Subscription.new(user["subscription"])
431
- @roles = user["roles"].map { |role| Role.new(role) }
432
- @organization = user["organization"] == nil ? nil : Organization.new(user["organization"])
433
- @languages = user["languages"].map { |lang| Language.new(lang) }
434
- end
435
-
436
- def get_posts(order: "new", count: nil, after: nil)
437
- p = @client.graphql(
438
- "user",
439
- Queries.get_user_posts,
440
- username: @username,
441
- order: order,
442
- count: count,
443
- after: after
444
- )
445
- p["user"]["posts"]["items"].map { |post| Post.new(@client, post) }
446
- end
447
-
448
- def get_comments(order: "new", count: nil, after: nil)
449
- c = @client.graphql(
450
- "user",
451
- Queries.get_user_comments,
452
- username: @username,
453
- order: order,
454
- count: count,
455
- after: after
456
- )
457
- c["user"]["comments"]["items"].map { |comment| Comment.new(@client, comment) }
458
- end
459
-
460
- def get_repls(count: nil, order: nil, direction: nil, before: nil, after: nil, pinnedReplsFirst: nil, showUnnamed: nil)
461
- r = @client.graphql(
462
- "user",
463
- Queries.get_user_repls,
464
- username: @username,
465
- order: order,
466
- count: count,
467
- direction: direction,
468
- before: before,
469
- after: after,
470
- pinnedReplsFirst: pinnedReplsFirst,
471
- showUnnamed: showUnnamed
472
- )
473
- r["user"]["publicRepls"]["items"].map { |repl| Repl.new(@client, repl) }
474
- end
475
-
476
- def to_s
477
- @username
478
- end
479
- end
480
-
481
-
482
-
483
- class LeaderboardUser < User
484
- attr_reader :cycles_since
485
-
486
- def initialize(client, user)
487
- super(client, user)
488
- @cycles_since = user["karmaSince"]
489
- end
490
- end
491
-
492
-
493
-
494
- class Client
495
- attr_writer :sid
496
-
497
- def initialize(sid=nil)
498
- @sid = sid
499
- end
500
-
501
- def graphql(name, query, **variables)
502
- payload = {
503
- operationName: name,
504
- query: query,
505
- variables: variables.to_json
506
- }
507
- r = HTTP
508
- .cookies(
509
- "connect.sid": @sid
510
- )
511
- .headers(
512
- referer: "#{$BASE_URL}/@CodingCactus/repltalk",
513
- "X-Requested-With": "ReplTalk"
514
- )
515
- .post(
516
- "#{$BASE_URL}/graphql",
517
- form: payload
518
- )
519
- begin data = JSON.parse(r)
520
- rescue
521
- puts "\e[31mERROR\n#{r}\e[0m"
522
- return nil
523
- end
524
- if data.include?("errors")
525
- puts "\e[31mERROR\n#{r}\e[0m"
526
- return nil
527
- end
528
- data = data["data"] if data.include?("data")
529
- data
530
- end
531
-
532
- def get_user(name)
533
- u = graphql(
534
- "userByUsername",
535
- Queries.get_user,
536
- username: name
537
- )
538
- return nil if u == nil || u["user"] == nil
539
- User.new(self, u["user"])
540
- end
541
-
542
- def get_user_by_id(id)
543
- u = graphql(
544
- "user",
545
- Queries.get_user_by_id,
546
- user_id: id
547
- )
548
- return nil if u == nil || u["user"] == nil
549
- User.new(self, u["user"])
550
- end
551
-
552
- def get_post(id)
553
- p = graphql(
554
- "post",
555
- Queries.get_post,
556
- id: id
557
- )
558
- return nil if p == nil || p["post"] == nil
559
- Post.new(self, p["post"])
560
- end
561
-
562
- def get_comment(id)
563
- c = graphql(
564
- "comment",
565
- Queries.get_comment,
566
- id: id
567
- )
568
- return nil if c == nil || c["comment"] == nil
569
- Comment.new(self, c["comment"])
570
- end
571
-
572
- def get_repl(url)
573
- r = graphql(
574
- "ReplView",
575
- Queries.get_repl,
576
- url: url
577
- )
578
- return nil if r == nil || r["repl"] == nil
579
- Repl.new(self, r["repl"])
580
- end
581
-
582
- def get_repl_comment(id)
583
- c = graphql(
584
- "ReplViewComment",
585
- Queries.get_repl_comment,
586
- id: id
587
- )
588
- return nil if c == nil || c["replComment"] == nil
589
- ReplComment.new(self, c["replComment"])
590
- end
591
-
592
- def get_board(name)
593
- b = graphql(
594
- "boardBySlug",
595
- Queries.get_board,
596
- slug: name
597
- )
598
- return nil if b == nil || b["board"] == nil
599
- Board.new(b["board"])
600
- end
601
-
602
- def get_leaderboard(count: nil, since: nil, after: nil)
603
- u = graphql(
604
- "LeaderboardQuery",
605
- Queries.get_leaderboard,
606
- count: count,
607
- since: since,
608
- after: after
609
- )
610
- u["leaderboard"]["items"].map { |user| LeaderboardUser.new(self, user) }
611
- end
612
-
613
- def get_posts(board: "all", order: "new", count: nil, after: nil, search: nil, languages: nil)
614
- p = graphql(
615
- "PostsFeed",
616
- Queries.get_posts,
617
- boardSlugs: [board],
618
- order: order,
619
- count: count,
620
- after: after,
621
- searchQuery: search,
622
- languages: languages
623
- )
624
- p["posts"]["items"].map { |post| Post.new(self, post) }
625
- end
626
-
627
- def create_post(board_name, title, content, repl_id: nil, show_hosted: false)
628
- p = graphql(
629
- "createPost",
630
- Mutations.create_post,
631
- input: {
632
- boardId: get_board(board_name).id,
633
- title: title,
634
- body: content,
635
- replId: repl_id,
636
- showHosted: show_hosted
637
- }
638
- )
639
- Post.new(self, p["createPost"]["post"])
640
- end
641
- end