mirror of
https://github.com/zedeus/nitter.git
synced 2025-03-04 01:51:12 +00:00
Support both web and Android sessions
This commit is contained in:
parent
4f9ba9c7d6
commit
661be438ec
4 changed files with 47 additions and 18 deletions
|
@ -1,5 +1,5 @@
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
import httpclient, asyncdispatch, options, strutils, uri, times, math, tables
|
import httpclient, asyncdispatch, options, strformat, strutils, uri, times, math, tables
|
||||||
import jsony, packedjson, zippy, oauth1
|
import jsony, packedjson, zippy, oauth1
|
||||||
import types, auth, consts, parserutils, http_pool
|
import types, auth, consts, parserutils, http_pool
|
||||||
import experimental/types/common
|
import experimental/types/common
|
||||||
|
@ -28,21 +28,27 @@ proc getOauthHeader(url, oauthToken, oauthTokenSecret: string): string =
|
||||||
|
|
||||||
return getOauth1RequestHeader(params)["authorization"]
|
return getOauth1RequestHeader(params)["authorization"]
|
||||||
|
|
||||||
proc genHeaders*(url, oauthToken, oauthTokenSecret: string): HttpHeaders =
|
proc genHeaders*(url: string; session: Session): HttpHeaders =
|
||||||
let header = getOauthHeader(url, oauthToken, oauthTokenSecret)
|
|
||||||
|
|
||||||
result = newHttpHeaders({
|
result = newHttpHeaders({
|
||||||
"connection": "keep-alive",
|
"connection": "keep-alive",
|
||||||
"authorization": header,
|
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
"x-twitter-active-user": "yes",
|
"x-twitter-active-user": "yes",
|
||||||
"authority": "api.x.com",
|
"authority": "api.x.com",
|
||||||
"accept-encoding": "gzip",
|
"accept-encoding": "gzip",
|
||||||
"accept-language": "en-US,en;q=0.9",
|
"accept-language": "en-US,en;q=0.9",
|
||||||
"accept": "*/*",
|
"accept": "*/*",
|
||||||
"DNT": "1"
|
"DNT": "1",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
case session.kind
|
||||||
|
of oauth:
|
||||||
|
result["authorization"] = getOauthHeader(url, session.oauthToken, session.oauthSecret)
|
||||||
|
of cookie:
|
||||||
|
result["authorization"] = "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"
|
||||||
|
result["x-twitter-auth-type"] = "OAuth2Session"
|
||||||
|
result["x-csrf-token"] = session.ct0
|
||||||
|
result["cookie"] = &"ct0={session.ct0}; auth_token={session.authToken}"
|
||||||
|
|
||||||
template fetchImpl(result, fetchBody) {.dirty.} =
|
template fetchImpl(result, fetchBody) {.dirty.} =
|
||||||
once:
|
once:
|
||||||
pool = HttpPool()
|
pool = HttpPool()
|
||||||
|
@ -54,7 +60,7 @@ template fetchImpl(result, fetchBody) {.dirty.} =
|
||||||
|
|
||||||
try:
|
try:
|
||||||
var resp: AsyncResponse
|
var resp: AsyncResponse
|
||||||
pool.use(genHeaders($url, session.oauthToken, session.oauthSecret)):
|
pool.use(genHeaders($url, session)):
|
||||||
template getContent =
|
template getContent =
|
||||||
resp = await c.get($url)
|
resp = await c.get($url)
|
||||||
result = await resp.body
|
result = await resp.body
|
||||||
|
|
|
@ -1,15 +1,24 @@
|
||||||
import std/strutils
|
import std/strutils
|
||||||
import jsony
|
import jsony
|
||||||
import ../types/session
|
import ../types/session
|
||||||
from ../../types import Session
|
from ../../types import Session, SessionKind
|
||||||
|
|
||||||
proc parseSession*(raw: string): Session =
|
proc parseSession*(raw: string): Session =
|
||||||
let
|
let session = raw.fromJson(RawSession)
|
||||||
session = raw.fromJson(RawSession)
|
|
||||||
id = session.oauthToken[0 ..< session.oauthToken.find('-')]
|
|
||||||
|
|
||||||
|
case session.kind
|
||||||
|
of "oauth":
|
||||||
|
let id = session.oauthToken[0 ..< session.oauthToken.find('-')]
|
||||||
result = Session(
|
result = Session(
|
||||||
|
kind: oauth,
|
||||||
id: parseBiggestInt(id),
|
id: parseBiggestInt(id),
|
||||||
oauthToken: session.oauthToken,
|
oauthToken: session.oauthToken,
|
||||||
oauthSecret: session.oauthTokenSecret
|
oauthSecret: session.oauthTokenSecret
|
||||||
)
|
)
|
||||||
|
of "cookie":
|
||||||
|
result = Session(
|
||||||
|
kind: cookie,
|
||||||
|
id: 999,
|
||||||
|
ct0: session.ct0,
|
||||||
|
authToken: session.authToken
|
||||||
|
)
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
type
|
type
|
||||||
RawSession* = object
|
RawSession* = object
|
||||||
|
kind*: string
|
||||||
oauthToken*: string
|
oauthToken*: string
|
||||||
oauthTokenSecret*: string
|
oauthTokenSecret*: string
|
||||||
|
ct0*: string
|
||||||
|
authToken*: string
|
||||||
|
|
||||||
|
|
|
@ -31,10 +31,20 @@ type
|
||||||
remaining*: int
|
remaining*: int
|
||||||
reset*: int
|
reset*: int
|
||||||
|
|
||||||
|
SessionKind* = enum
|
||||||
|
oauth
|
||||||
|
cookie
|
||||||
|
|
||||||
Session* = ref object
|
Session* = ref object
|
||||||
id*: int64
|
case kind*: SessionKind
|
||||||
|
of oauth:
|
||||||
oauthToken*: string
|
oauthToken*: string
|
||||||
oauthSecret*: string
|
oauthSecret*: string
|
||||||
|
of cookie:
|
||||||
|
ct0*: string
|
||||||
|
authToken*: string
|
||||||
|
|
||||||
|
id*: int64
|
||||||
pending*: int
|
pending*: int
|
||||||
limited*: bool
|
limited*: bool
|
||||||
limitedAt*: int
|
limitedAt*: int
|
||||||
|
|
Loading…
Reference in a new issue