summaryrefslogtreecommitdiff
path: root/exitter.el
blob: 8dde97de851baa9ece596f5f72dc11d65b2daaf3 (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
;; -*- 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)

(setq exitter-url-endpoint "https://api.twitter.com/1.1"
      exitter-url-activate
      (format "%s/guest/activate.json" exitter-url-endpoint)
      exitter-url-task
      (format "%s/onboarding/task.json" exitter-url-endpoint)
      exitter-url-token
      (format "https://api.twitter.com/oauth2/token"))
(setq exitter-tor-param "-x socks5://127.0.0.1:9050/")
(setq 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-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))

(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)))
      )))

(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
                          (print (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)))
    ))

(provide 'exitter)