summaryrefslogtreecommitdiff
path: root/exitter.el
blob: 13c7091d5e641713a10da81510b5fe94699e037b (plain) (blame)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
;; -*- lexical-binding: t; -*-
;; Copyright (C) 2024  Free Software Foundation, Inc.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "29.4") (request "0.3.3"))

;; This file is part of exitter.

;; exitter is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; exitter is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with exitter.  If not, see <https://www.gnu.org/licenses/>.

(require 'request)

(defvar exitter-url-endpoint "https://api.twitter.com/1.1")
(defvar exitter-url-activate
  (format "%s/guest/activate.json" exitter-url-endpoint))
(defvar exitter-url-task
  (format "%s/onboarding/task.json" exitter-url-endpoint))
(defvar exitter-url-token
  (format "https://api.twitter.com/oauth2/token"))
(defvar exitter-url-tweet-detail
  "https://api.twitter.com/graphql/3XDB26fBve-MmjHaWTUZxA/TweetDetail")
(defvar exitter-tor-param "-x socks5://127.0.0.1:9050/")
(defvar exitter-init-headers
  `(
    ("Content-Type" . "application/json")
    ("User-Agent" . "TwitterAndroid/10.10.0 (29950000-r-0) ONEPLUS+A3010/9 (OnePlus;ONEPLUS+A3010;OnePlus;OnePlus3;0;;1;2016)")
    ;; ("User-Agent" . "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36")
    ("X-Twitter-API-Version" . "5")
    ("X-Twitter-Client" . "TwitterAndroid")
    ("X-Twitter-Client-Version" . "10.10.0")
    ("OS-Version" . "28")
    ("System-User-Agent" . "Dalvik/2.1.0 (Linux; U; Android 9; ONEPLUS A3010 Build/PKQ1.181203.001)")
    ("X-Twitter-Active-User" . "yes")
    ))
(defvar exitter-default-features
  (json-encode
   '(
     ("android_graphql_skip_api_media_color_palette" . :json-false)
     ("blue_business_profile_image_shape_enabled" . :json-false)
     ("creator_subscriptions_subscription_count_enabled" . :json-false)
     ("creator_subscriptions_tweet_preview_api_enabled" . t)
     ("freedom_of_speech_not_reach_fetch_enabled" . :json-false)
     ("graphql_is_translatable_rweb_tweet_is_translatable_enabled" . :json-false)
     ("hidden_profile_likes_enabled" . :json-false)
     ("highlights_tweets_tab_ui_enabled" . :json-false)
     ("interactive_text_enabled" . :json-false)
     ("longform_notetweets_consumption_enabled" . t)
     ("longform_notetweets_inline_media_enabled" . :json-false)
     ("longform_notetweets_richtext_consumption_enabled" . t)
     ("longform_notetweets_rich_text_read_enabled" . :json-false)
     ("responsive_web_edit_tweet_api_enabled" . :json-false)
     ("responsive_web_enhance_cards_enabled" . :json-false)
     ("responsive_web_graphql_exclude_directive_enabled" . t)
     ("responsive_web_graphql_skip_user_profile_image_extensions_enabled" . :json-false)
     ("responsive_web_graphql_timeline_navigation_enabled" . :json-false)
     ("responsive_web_media_download_video_enabled" . :json-false)
     ("responsive_web_text_conversations_enabled" . :json-false)
     ("responsive_web_twitter_article_tweet_consumption_enabled" . :json-false)
     ("responsive_web_twitter_blue_verified_badge_is_enabled" . t)
     ("rweb_lists_timeline_redesign_enabled" . t)
     ("spaces_2022_h2_clipping" . t)
     ("spaces_2022_h2_spaces_communities" . t)
     ("standardized_nudges_misinfo" . :json-false)
     ("subscriptions_verification_info_enabled" . t)
     ("subscriptions_verification_info_reason_enabled" . t)
     ("subscriptions_verification_info_verified_since_enabled" . t)
     ("super_follow_badge_privacy_enabled" . :json-false)
     ("super_follow_exclusive_tweet_notifications_enabled" . :json-false)
     ("super_follow_tweet_api_enabled" . :json-false)
     ("super_follow_user_api_enabled" . :json-false)
     ("tweet_awards_web_tipping_enabled" . :json-false)
     ("tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled" . :json-false)
     ("tweetypie_unmention_optimization_enabled" . :json-false)
     ("unified_cards_ad_metadata_container_dynamic_card_content_query_enabled" . :json-false)
     ("verified_phone_label_enabled" . :json-false)
     ("vibe_api_enabled" . :json-false)
     ("view_counts_everywhere_api_enabled" . :json-false)
     )))

(defvar exitter-oauth-consumer-key nil)
(defvar exitter-oauth-consumer-secret nil)
(defvar exitter-access-token nil)
(defvar exitter-username nil)
(defvar exitter-password nil)
(defvar exitter-email nil)
(defvar exitter-oauth-token nil)
(defvar exitter-oauth-token-secret nil)
(defvar exitter-oauth-token-ctime nil)

(defvar exitter-debug nil)

;;; for debugging
(if exitter-debug
    (setq request-message-level 'blather)
  (setq request-message-level -1))

;;; Get an oauth2 bearer token
;;; https://developer.x.com/en/docs/authentication/api-reference/token
(defun exitter-get-access-token ()
  (let ((oauth-consumer-key-secret
         (base64-encode-string
          (format "%s:%s" exitter-oauth-consumer-key
                  exitter-oauth-consumer-secret)
          t)))
    (request exitter-url-token
      :headers `(("Authorization" .
                  ,(format "Basic %s" oauth-consumer-key-secret))
                 ("Content-Type" . "application/x-www-form-urlencoded"))
      :data "grant_type=client_credentials"
      :parser 'json-read
      :type "POST"
      :success (cl-function
                (lambda (&key data &allow-other-keys)
                  ;; will generate exactly the same token as
                  ;; `exitter-access-token'
                  (print (alist-get 'access_token data))))
      :error
      (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
                     (message "Got error: %S" error-thrown)))
      )))

;;; Use the twitter frontend flow and the oauth2 bearer token to
;;; obtain a user oauth token aka user access token.

;;; The official way of doing so is by oauth 1.0a documented at
;;; https://developer.x.com/en/docs/authentication/oauth-1-0a, but
;;; here we use the twitter frontend flow, similar to how one logs in
;;; the twitter web client, see also
;;; https://github.com/fa0311/TwitterFrontendFlow
(defun exitter-get-guest-token ()
  (when exitter-debug (message "entering exitter-get-guest-token"))
  (request exitter-url-activate
    :headers `(("Authorization" . ,(format "Bearer %s" exitter-access-token)))
    :parser 'json-read
    :type "POST"
    :success (cl-function
              (lambda (&key data &allow-other-keys)
                (exitter-login-flow-token (alist-get 'guest_token data))))
    :error
    (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
                   (message "Got error: %S" error-thrown)))
    ))

(defun exitter-login-flow-token (guest-token)
  (when exitter-debug (message "entering exitter-login-flow-token"))
  (let ((headers `(,@exitter-init-headers
                   ("Authorization" . ,(format "Bearer %s" exitter-access-token))
                   ("X-Guest-Token" . ,guest-token)
                   )))
    (request exitter-url-task
      :params '(("flow_name" . "login")
                ("lang" . "en"))
      :headers headers
      :data (json-encode
             '(("flow_token" . nil)
               ("input_flow_data" .
                (("country_code" . nil)
                 ("flow_context" .
                  (("referral_context" .
                    (("referral_details" .
                      "utm_source=google-play&utm_medium=organic")
                     ("referrer_url" . "")))
                   ("start_location" . (("location" . "deeplink")))
                   ("request_variant" . nil)
                   ("target_user_id" . 0)))))))
      :parser 'json-read
      :type "POST"
      :complete (cl-function
                 (lambda (&key response &allow-other-keys)
                   (let* ((att
                           (request-response-header response "att"))
                          (data
                           (request-response-data response))
                          (flow-token (alist-get 'flow_token data)))
                     (unless (exitter-find-subtask data "LoginEnterUserIdentifier")
                       (error "Subtask LoginEnterUserIdentifier not found"))
                     ;; (pp data)
                     ;; (message "flow-token: %s\natt: %s"
                     ;;          (alist-get 'flow_token data)
                     ;;          att)
                     (when att
                       (setq headers
                             `(,@headers
                               ("att" . ,att)
                               ("cookie" . ,(format "att=%s" att)))))
                     (exitter-enter-username flow-token headers))))
      :error
      (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
                     (message "Got error: %S" error-thrown)))
      )))

(defun exitter-find-subtask (data subtask-id)
  (when exitter-debug
    (message "entering exitter-find-subtask")
    (message "subtask-id: %s" subtask-id))
  (seq-find
   (lambda (subtask)
     (equal (alist-get 'subtask_id subtask) subtask-id))
   (alist-get 'subtasks data)))

(defun exitter-report-error (&rest args &key error-thrown &allow-other-keys)
  (message "Got error: %S" error-thrown))

(defun exitter-enter-username (flow-token headers)
  (when exitter-debug (message "entering exitter-enter-username"))
  (request exitter-url-task
    :params '(("lang" . "en"))
    :headers headers
    :data (json-encode
           `(("flow_token" . ,flow-token)
             ("subtask_inputs" .
              [(("enter_text" .
                 (("suggestion_id" . nil)
                  ("text" . ,exitter-username)
                  ("link" . "next_link")))
                ("subtask_id" . "LoginEnterUserIdentifier"))])))
    :type "POST"
    :parser 'json-read
    :success (cl-function
              (lambda (&key data &allow-other-keys)
                (let ((new-flow-token
                       (alist-get 'flow_token data)))
                  (cond
                   ((exitter-find-subtask data "LoginEnterPassword")
                    (message "LoginEnterPassword")
                    (exitter-enter-password new-flow-token headers))
                   ((exitter-find-subtask data "LoginEnterAlternateIdentifierSubtask")
                    (message "LoginEnterAlternateIdentifierSubtask")
                    (exitter-enter-email new-flow-token headers))
                   (t (message "Cannot find any matching subtasks"))))
                ))
    :error
    (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
                   (message "Got error: %S" error-thrown)))))


(defun exitter-enter-password (flow-token headers)
  (when exitter-debug (message "entering exitter-enter-password"))
  (request exitter-url-task
    :params '(("lang" . "en"))
    :headers headers
    :data (json-encode
           `(("flow_token" . ,flow-token)
             ("subtask_inputs" .
              [(("enter_password" .
                 (("password" . ,exitter-password)
                  ("link" . "next_link")))
                ("subtask_id" . "LoginEnterPassword"))])))
    :type "POST"
    :parser 'json-read
    :success (cl-function
              (lambda (&key data &allow-other-keys)
                (cond
                 ((exitter-find-subtask data "LoginSuccessSubtask")
                  (message "LoginSuccessSubtask")
                  (let* ((subtask
                          (exitter-find-subtask data "LoginSuccessSubtask"))
                         (open-account (alist-get 'open_account subtask)))
                    (setq exitter-oauth-token
                          (alist-get 'oauth_token open-account)
                          exitter-oauth-token-secret
                          (alist-get 'oauth_token_secret open-account)
                          exitter-oauth-token-ctime
                          (format-time-string "%Y-%m-%d %a %H:%M:%S"
                                              (current-time)))))
                 (t (message "Cannot find any matching subtasks")))                ))
    :error
    (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
                   (message "Got error: %S" error-thrown)))
    ))

(defun exitter-enter-email (flow-token headers)
  (when exitter-debug (message "entering exitter-enter-email"))
  (request exitter-url-task
    :params '(("lang" . "en"))
    :headers headers
    :data (json-encode
           `(("flow_token" . ,flow-token)
             ("subtask_inputs" .
              [(("enter_text" .
                 (("text" . ,exitter-email)
                  ("link" . "next_link")))
                ("subtask_id" . "LoginEnterAlternateIdentifierSubtask"))])))
    :type "POST"
    :parser 'json-read
    :success (cl-function
              (lambda (&key data &allow-other-keys)
                (let ((new-flow-token
                       (alist-get 'flow_token data)))
                  (cond
                   ((exitter-find-subtask data "LoginEnterPassword")
                    (message "LoginEnterPassword")
                    (exitter-enter-password new-flow-token headers))
                   (t (message "Cannot find any matching subtasks"))))
                ))
    :error
    (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
                   (message "Got error: %S" error-thrown)))
    ))

;;; Example at
;;; https://developer.x.com/en/docs/authentication/oauth-1-0a/creating-a-signature
;;; should produce signature "Ls93hJiZbQ3akF3HF3x1Bz8%2FzU4%3D"
(defun exitter-example-sign-oauth ()
  (exitter-get-sign-oauth
   "https://api.x.com/1.1/statuses/update.json" ;link
   '(("status" . "Hello Ladies + Gentlemen, a signed OAuth request!")
     ("include_entities" .	"true"))             ;url-params
   "POST"                                        ;method
   "xvz1evFS4wEEPTGEFPHBog"                      ;oauth-consumer-key
   "kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw" ;oauth-consumer-secret
   "370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb" ;oauth-token
   "LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE"  ;oauth-token-secret
   "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg" ;oauth-nonce
   "1318622958"                                 ;oauth-timestamp
   ))

;;; see
;;; https://developer.x.com/en/docs/authentication/oauth-1-0a/creating-a-signature
;;; https://developer.x.com/en/docs/authentication/oauth-1-0a/authorizing-a-request
(defun exitter-get-sign-oauth (link url-params method &optional
                                    oauth-consumer-key oauth-consumer-secret
                                    oauth-token oauth-token-secret
                                    oauth-nonce oauth-timestamp)
  (let* ((oauth-params `(("oauth_consumer_key" .
                          ,(or oauth-consumer-key exitter-oauth-consumer-key))
                         ("oauth_nonce" . ,(or oauth-nonce (exitter-nonce)))
                         ("oauth_signature_method" . "HMAC-SHA1")
                         ("oauth_timestamp" . ,(or oauth-timestamp (format-time-string "%s" (current-time))))
                         ("oauth_token" . ,(or oauth-token exitter-oauth-token))
                         ("oauth_version" . "1.0")))
         (all-params (sort (seq-concatenate 'list url-params oauth-params)
                           (lambda (a b) (string< (car a) (car b)))))
         (method-up (upcase method))
         (hexed-link (url-hexify-string link))
         (param-to-sign-unencoded
          (mapconcat
           (lambda (pair)
             (format "%s=%s" (car pair) (url-hexify-string (cdr pair))))
           all-params
           "&"))
         (param-to-sign
          (replace-regexp-in-string
           "&" "%26"
           (replace-regexp-in-string
            "=" "%3D"
            (replace-regexp-in-string
             "%" "%25"
             (replace-regexp-in-string
              "\\+" "%20"
              param-to-sign-unencoded)))))
         (to-sign (format "%s&%s&%s" method-up hexed-link param-to-sign))
         (signing-key (format "%s&%s"
                              (or oauth-consumer-secret
                                  exitter-oauth-consumer-secret)
                              (or oauth-token-secret
                                  exitter-oauth-token-secret)))
         (signature (url-hexify-string
                     (base64-encode-string
                      (exitter-hmac-sha1 (encode-coding-string signing-key 'utf-8)
                                         (encode-coding-string to-sign 'utf-8)))))
         )
    ;; (message "PARAM-TO-SIGN-UNENC: %s" (prin1-to-string param-to-sign-unencoded))
    ;; (message "PARAM-TO-SIGN: %s" (prin1-to-string param-to-sign))
    ;; (message "TO-SIGN: %s" (prin1-to-string to-sign))
    (format "OAuth realm=\"http://api.twitter.com/\", oauth_signature=\"%s\", %s"
            signature
            (mapconcat
             (lambda (pair)
               (format "%s=\"%s\"" (car pair) (cdr pair)))
             oauth-params
             ", "))))

(defun exitter-do-fetch (link params &optional headers cb)
  (when exitter-debug (message "entering exitter-do-fetch"))
  (let ((authorization (exitter-get-sign-oauth link params "GET")))
    (request link
      :headers `(,@headers
                 ("Connection" . "Keep-Alive")
                 ("Authorization" . ,authorization)
                 ("Content-Type" . "application/json")
                 ("X-Twitter-Active-User" . "yes")
                 ("Authority" . "api.twitter.com")
                 ("Accept-Encoding" . "gzip")
                 ("Accept-Language" . "en-US,en;q=0.9")
                 ("Accept" . "*/*")
                 ("DNT" . "1")
                 ("User-Agent" .
                  "TwitterAndroid/10.10.0 (29950000-r-0) ONEPLUS+A3010/9 \
(OnePlus;ONEPLUS+A3010;OnePlus;OnePlus3;0;;1;2016)")
                 ("X-Twitter-API-Version" . "5")
                 ("X-Twitter-Client" . "TwitterAndroid")
                 ("X-Twitter-Client-Version" . "10.10.0")
                 ("OS-Version" . "28")
                 ("System-User-Agent" .
                  "Dalvik/2.1.0 (Linux; U; Android 9; \
ONEPLUS A3010 Build/PKQ1.181203.001)")
                 )
      :params params
      :type "GET"
      :parser 'json-read
      :success (if cb
                   (cl-function
                    (lambda (&key data &allow-other-keys)
                      (funcall cb data)))
                 (cl-function
                  (lambda (&key data &allow-other-keys)
                    (pp data))))
      :error
      (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
                     (message "Got error: %S" error-thrown)))
      )))

(defun exitter-get-tweet (id)
  (let ((variables (json-encode
                    `(
                      ("focalTweetId" . ,id)
                      ;; ("referrer" . "tweet")
                      ;; ("with_rux_injections" . :json-false)
                      ("includePromotedContent" . :json-false)
                      ;; ("withCommunity" . t)
                      ("withQuickPromoteEligibilityTweetFields" . :json-false)
                      ("includeHasBirdwatchNotes" . :json-false)
                      ("withBirdwatchNotes" . :json-false)
                      ("withVoice" . :json-false)
                      ("withV2Timeline" . t)
                      ))))
    (exitter-do-fetch
     exitter-url-tweet-detail
     `(("variables" . ,variables)
       ("features" . ,exitter-default-features))
     nil
     (lambda (data)
       ;; (pp data)
       (exitter-save-posts (exitter-filter-tweet-details data) id)))))

;;; transform alist
(defun exitter-filter-tweet-details (resp)
  (vconcat
   (seq-map
    'exitter-filter-tweet-entry
    (alist-get
     'entries
     (let-alist resp
       (elt .data.threaded_conversation_with_injections_v2.instructions 0))))))

;;; .content.entryType == "TimelineTimelineItem":

;;; If further its .content.itemContent.itemType=="TimelineTweet",
;;; then this is a simple case and we could filter its
;;; .content.itemContent.tweet_results.result. Otherwise if its
;;; .content.itemContent.itemType=="TimelineTimelineCursor" then it is
;;; paging which we need to figure out later.

;;; .content.entryType == "TimelineTimelineModule":
;;; has ~items~ containing entries. But these entries do not have
;;; content nor entryType anywhere. Instead they each have an
;;; item.itemContent.tweet_results.result

(defun exitter-filter-tweet-entry (entry)
  (let-alist entry
    (cond
     ((equal .content.entryType "TimelineTimelineItem")
      (let-alist .content.itemContent
        (pcase .itemType
          ("TimelineTweet"
           (exitter-filter-tweet-result .tweet_results.result))
          ("TimelineTimelineCursor"
           (message "TimelineTimelineCursor encountered. More tweets available"))
          (_
           (error "TimelineTimelineItem entry with unknown itemType: %s"
                  (pp entry))))))
     ((equal .content.entryType "TimelineTimelineModule")
      (vconcat (seq-map 'exitter-filter-tweet-entry .content.items)))
     ((equal .item.itemContent.itemType "TimelineTweet")
      (exitter-filter-tweet-result .item.itemContent.tweet_results.result))
     ((equal .item.itemContent.itemType "TimelineTimelineCursor")
      (message "TimelineTimelineCursor encountered. More tweets available"))
     (t (error "Entry with unknown entryType or itemType: %s" (pp entry))))))

(defun exitter-filter-tweet-result (result)
  (pcase (alist-get '__typename result)
    ("Tweet"
     (let (ret author quoted post)
       (let-alist result
         (let-alist .core.user_results.result.legacy
           (setq author
                 `((screen_name . ,.screen_name)
                   (name . ,.name))))
         (when .quoted_status_result
           (setq quoted
                 (exitter-filter-tweet-result .quoted_status_result.result)))
         (let-alist .legacy
           (setq post
                 `((id_str . ,.id_str)
                   (created_at . ,.created_at)
                   (full_text . ,.full_text)
                   (urls . ,(vconcat .entities.urls .entities.media))
                   (reply_count . ,.reply_count)
                   (retweet_count . ,.retweet_count)
                   (quote_count . ,.quote_count)
                   (favorite_count . ,.favorite_count)
                   (in_reply_to_status_id_str . ,.in_reply_to_status_id_str)
                   (is_quote_status . ,.is_quote_status)
                   (author . ,author)
                   (quoted . ,quoted))))
         (when .note_tweet.is_expandable
           (let-alist .note_tweet.note_tweet_results.result
             (push `(expanded_text . ,.text) post)
             (push `(extra_urls . ,.entity_set.urls) post)))
         (push `(post . ,post) ret)
         )
       ret))
    ("TweetWithVisibilityResults"
     (message "Aaaaaaads"))
    (_
     (error "result with unknown __typename: %s" result))))

;;; renderer, similar to mastorg
(require 'hierarchy)
(defun exitter-post-make-parent-fn (posts)
  "Given a collection of POSTS, return a function that find the parent post."
  (lambda (post)
    (let ((id (alist-get 'in_reply_to_status_id_str post)))
      (seq-find
       (lambda (candidate)
         (equal (alist-get 'id_str candidate) id))
       posts))))

;;; Formatting functions
(defun exitter-format-posts (filtered-details)
  "Format a post tree of post located at URL.

Including ancestors and descendants, if any."
  (let ((posts-hier (hierarchy-new))
        (posts (exitter-vector-flatten filtered-details)))
    (hierarchy-add-trees
     posts-hier
     posts
     (exitter-post-make-parent-fn posts))
    (string-join
     (hierarchy-map 'exitter-format-post posts-hier 1)
     "\n")))

(defvar exitter-dir (locate-user-emacs-file "exitter")
  "Path to local directory of saved threads.")

(defun exitter-save-posts (filtered-details id)
  ;; (pp filtered-details)
  (exitter-save-text-and-switch-to-buffer
   (exitter-format-posts filtered-details)
   (format (file-name-concat exitter-dir (format "%s.org" id)))))

(defun exitter-post-url (user post-id)
  (format "https://x.com/%s/status/%s" user post-id))

(defun exitter-make-org-link (link desc)
  (format "[[%s][%s]]" link desc))

(defun exitter-format-post (post level)
  "Format a POST with indent LEVEL."
  (let-alist post
    (format "%s %s (@%s) %s\n\n%s%s\n\nā¤·%s ā€œ%s ā‡†%s ā˜…%s\n"
            (make-string level ?*)
            .author.name
            .author.screen_name
            (exitter-make-org-link
             (exitter-post-url .author.screen_name .id_str)
             (exitter--relative-time-description .created_at))
            (exitter-replace-t-co-links (or .expanded_text .full_text)
                                        (or .extra_urls .urls))
            (if .quoted
                (format "\n\n----\n%s----"
                        (replace-regexp-in-string
                         "^." " \\&"
                         (if (stringp .quoted)
                             (message "Quoted post is a plain string: %s\n"
                                      .quoted)
                           ;; We can't use .quoted.post here because
                           ;; for some reason it is evaluated even if
                           ;; the "if" branch is entered.
                           (exitter-format-post (alist-get 'post .quoted) 1))))
              "")
            .reply_count
            .quote_count
            .retweet_count
            .favorite_count
            )))

(defun exitter-post-url-p (url)
  (string-match-p "^/[^/]+/status/\\([0-9]+\\)"
                  (url-filename (url-generic-parse-url url))))

(defun exitter-open-post (url)
  (interactive "sTwitter link: ")
  (let ((path-etc (url-filename (url-generic-parse-url url))))
    (unless (string-match "^/[^/]+/status/\\([0-9]+\\)" path-etc)
      (error "Not a valid x/twitter (or a frontend) url!"))
    (exitter-get-tweet (match-string 1 path-etc))))

;;; utilities

;;; code adapted from mastodon.el
(defun exitter--human-duration (seconds &optional resolution)
  "Return a string describing SECONDS in a more human-friendly way.
The return format is (STRING . RES) where RES is the resolution of
this string, in seconds.
RESOLUTION is the finest resolution, in seconds, to use for the
second part of the output (defaults to 60, so that seconds are only
displayed when the duration is smaller than a minute)."
  (cl-assert (>= seconds 0))
  (unless resolution (setq resolution 60))
  (let* ((units exitter--time-units)
         (n1 seconds) (unit1 (pop units)) (res1 1)
         n2 unit2 res2
         next)
    (while (and units (> (truncate (setq next (/ n1 (car units)))) 0))
      (setq unit2 unit1)
      (setq res2 res1)
      (setq n2 (- n1 (* (car units) (truncate n1 (car units)))))
      (setq n1 next)
      (setq res1 (truncate (* res1 (car units))))
      (pop units)
      (setq unit1 (pop units)))
    (setq n1 (truncate n1))
    (if n2 (setq n2 (truncate n2)))
    (cond
     ((null n2)
      ;; revert to old just now style for < 1 min:
      (cons "just now" 60))
     ;; (cons (format "%d %s%s" n1 unit1 (if (> n1 1) "s" ""))
     ;;       (max resolution res1)))
     ((< (* res2 n2) resolution)
      (cons (format "%d %s%s" n1 unit1 (if (> n1 1) "s" ""))
            (max resolution res2)))
     ((< res2 resolution)
      (let ((n2 (/ (* resolution (/ (* n2 res2) resolution)) res2)))
        (cons (format "%d %s%s, %d %s%s"
                      n1 unit1 (if (> n1 1) "s" "")
                      n2 unit2 (if (> n2 1) "s" ""))
              resolution)))
     (t
      (cons (format "%d %s%s, %d %s%s"
                    n1 unit1 (if (> n1 1) "s" "")
                    n2 unit2 (if (> n2 1) "s" ""))
            (max res2 resolution))))))

(defconst exitter--time-units
  '("sec"   60.0 ;; Use a float to convert `n' to float.
    "min"   60
    "hour"  24
    "day"   7
    "week"  4.345
    "month" 12
    "year"))

(defun exitter--relative-time-details (timestamp &optional current-time)
  "Return cons of (DESCRIPTIVE STRING . NEXT-CHANGE) for the TIMESTAMP.
Use the optional CURRENT-TIME as the current time (only used for
reliable testing).
The descriptive string is a human readable version relative to
the current time while the next change timestamp give the first
time that this description will change in the future.
TIMESTAMP is assumed to be in the past."
  (let* ((time-difference (time-subtract current-time timestamp))
         (seconds-difference (float-time time-difference))
         (tmp (exitter--human-duration (max 0 seconds-difference))))
    ;; revert to old just now style for < 1 min
    (cons (concat (car tmp) (if (string= "just now" (car tmp)) "" " ago"))
          (time-add current-time (cdr tmp)))))

(defun exitter--relative-time-description (time-string &optional current-time)
  "Return a string with a human readable TIME-STRING relative to the current time.
Use the optional CURRENT-TIME as the current time (only used for
reliable testing).
E.g. this could return something like \"1 min ago\", \"yesterday\", etc.
TIME-STAMP is assumed to be in the past."
  (car (exitter--relative-time-details
        (encode-time (parse-time-string time-string)) current-time)))

(defun exitter-save-text-and-switch-to-buffer (text file-name)
  "Save TEXT to FILE-NAME and switch to buffer."
  (let ((buffer (find-file-noselect file-name))
        (coding-system-for-write 'utf-8))
    (with-current-buffer buffer
      (let ((inhibit-read-only t))
        (erase-buffer)
        (insert text))
      (goto-char (point-min))
      (save-buffer)
      (revert-buffer t t))
    (switch-to-buffer buffer)))

(defun exitter-vector-flatten (v)
  (cond
   ((and (vectorp v) (length= v 0)) v)
   ((vectorp v)
    (vconcat (exitter-vector-flatten (elt v 0))
             (exitter-vector-flatten (subseq v 1 (length v)))))
   ((and (listp v) (alist-get 'post v) (vector (alist-get 'post v))))
   (t [])))

(require 'bindat)

(defun exitter-nonce ()
  (let ((xs))
    (dotimes (_ 32 xs)
      (setq xs (cons (random 256) xs)))
    (replace-regexp-in-string
     "[=/+]" ""
     (base64-encode-string
      (alist-get 'bs (bindat-unpack '((bs str 32)) (vconcat xs)))))))

(defun exitter-replace-t-co-links (text urls)
  (with-temp-buffer
    (insert text)
    (goto-char (point-min))
    (while (re-search-forward "https://t.co" nil t)
      (pcase-let* ((`(,beg . ,end) (bounds-of-thing-at-point 'url))
                   (new-url (exitter-get-redirect-url
                             (buffer-substring-no-properties beg end)
                             urls)))
        (delete-region beg end)
        (insert new-url)))
    (buffer-string)))

(defun exitter-get-redirect-url (url urls)
  (let-alist (seq-find
              (lambda (info) (equal url (alist-get 'url info)))
              urls)
    (with-temp-buffer
      (let ((expanded-url
             (or .media_url_https .expanded_url url)))
        (insert
         (if .video_info
             (format "%s (%s sec)"
                     (exitter-make-org-link
                      (alist-get 'url
                                 (elt .video_info.variants
                                      (1- (length .video_info.variants))))
                      .type)
                     (if .video_info.duration_millis
                         (number-to-string (/ .video_info.duration_millis 1000))
                       "??"))
           (exitter-make-org-link expanded-url (or .type "link"))))
        (when .media_url_https
          (let ((image-file-name
                 (file-name-concat exitter-dir
                                   (file-name-nondirectory .media_url_https))))
            (ignore-error 'file-already-exists
              (url-copy-file .media_url_https image-file-name))
            (insert "\n")
            (insert-image (create-image image-file-name)))))
      (buffer-string))))

;;; Probably not needed...
;; (defun exitter-get-redirect-url (url)
;;   "Get redirect link of URL.

;; Sends a HEAD request."
;;   (let* ((url-request-method "HEAD")
;;          (url-max-redirections 0)
;;          (buffer (url-retrieve-synchronously url))
;;          (inhibit-message t))
;;     (with-current-buffer buffer
;;       (goto-char (point-min))
;;       (when (re-search-forward "^Location: \\(.*\\)$" nil t)
;;         (match-string 1)))))

(require 'sha1)

;;; Source: https://www.emacswiki.org/emacs/HmacShaOne
(defun exitter-hmac-sha1 (key message)
  "Return an HMAC-SHA1 authentication code for KEY and MESSAGE.

KEY and MESSAGE must be unibyte strings.  The result is a unibyte
string.  Use the function `encode-hex-string' or the function
`base64-encode-string' to produce human-readable output.

See URL:<http://en.wikipedia.org/wiki/HMAC> for more information
on the HMAC-SHA1 algorithm.

The Emacs multibyte representation actually uses a series of
8-bit values under the hood, so we could have allowed multibyte
strings as arguments.  However, internal 8-bit values don't
correspond to any external representation \(at least for major
version 22).  This makes multibyte strings useless for generating
hashes.

Instead, callers must explicitly pick and use an encoding for
their multibyte data.  Most callers will want to use UTF-8
encoding, which we can generate as follows:

  (let ((unibyte-key   (encode-coding-string key   'utf-8 t))
        (unibyte-value (encode-coding-string value 'utf-8 t)))
    (hmac-sha1 unibyte-key unibyte-value))

For keys and values that are already unibyte, the
`encode-coding-string' calls just return the same string."
  (when (multibyte-string-p key)
    (error "key %s must be unibyte" key))
  (when (multibyte-string-p message)
    (error "message %s must be unibyte" message))

  ;; The key block is always exactly the block size of the hash
  ;; algorithm.  If the key is too small, we pad it with zeroes (or
  ;; instead, we initialize the key block with zeroes and copy the
  ;; key onto the nulls).  If the key is too large, we run it
  ;; through the hash algorithm and use the hashed value (strange
  ;; but true).

  (let ((+hmac-sha1-block-size-bytes+ 64)) ; SHA-1 uses 512-bit blocks
    (when (< +hmac-sha1-block-size-bytes+ (length key))
      (setq key (sha1 key nil nil t)))

    (let ((key-block (make-vector +hmac-sha1-block-size-bytes+ 0)))
      (dotimes (i (length key))
        (aset key-block i (aref key i)))

      (let ((opad (make-vector +hmac-sha1-block-size-bytes+ #x5c))
            (ipad (make-vector +hmac-sha1-block-size-bytes+ #x36)))

        (dotimes (i +hmac-sha1-block-size-bytes+)
          (aset ipad i (logxor (aref ipad i) (aref key-block i)))
          (aset opad i (logxor (aref opad i) (aref key-block i))))

        (when (fboundp 'unibyte-string)
          ;; `concat' of Emacs23 (and later?) generates a multi-byte
          ;; string from a vector of characters with eight bit.
          ;; Since `opad' and `ipad' must be unibyte, we have to
          ;; convert them by using `unibyte-string'.
          ;; We cannot use `string-as-unibyte' here because it encodes
          ;; bytes with the manner of UTF-8.
          (setq opad (apply 'unibyte-string (mapcar 'identity opad)))
          (setq ipad (apply 'unibyte-string (mapcar 'identity ipad))))

        (sha1 (concat opad
                      (sha1 (concat ipad message)
                            nil nil t))
              nil nil t)))))

(provide 'exitter)