From 34678ea2568e46c13abe3d659a9c5e96422d4966 Mon Sep 17 00:00:00 2001 From: Ryu18 Date: Thu, 6 Jan 2022 12:00:00 +0100 Subject: [PATCH] use api for guest_token fix #28 --- twspace_dl/__init__.py | 4 ++-- twspace_dl/twspace_dl.py | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/twspace_dl/__init__.py b/twspace_dl/__init__.py index 4317ecf..60c8321 100644 --- a/twspace_dl/__init__.py +++ b/twspace_dl/__init__.py @@ -1,4 +1,4 @@ -from .FormatInfo import FormatInfo -from .TwspaceDL import TwspaceDL +from .format_info import FormatInfo +from .twspace_dl import TwspaceDL __all__ = ["FormatInfo", "TwspaceDL"] diff --git a/twspace_dl/twspace_dl.py b/twspace_dl/twspace_dl.py index 36c2d61..b050343 100644 --- a/twspace_dl/twspace_dl.py +++ b/twspace_dl/twspace_dl.py @@ -133,15 +133,17 @@ def user_id(user_url: str) -> str: @staticmethod def guest_token() -> str: - guest_token = "" - for i in range(5): - response = requests.get("https://twitter.com/").text - guest_token_list = re.findall(r"(?<=gt\=)\d{19}", response) - if len(guest_token_list) != 0: - guest_token = guest_token_list[0] - else: - print(f"trying to get guest token try number:{i}", end="\r") - time.sleep(1) + headers = { + "authorization": ( + "Bearer " + "AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs" + "=1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA" + ) + } + response = requests.post( + "https://api.twitter.com/1.1/guest/activate.json", headers=headers + ).json() + guest_token = response["guest_token"] if not guest_token: raise RuntimeError("No guest token found after five retry") print()