From 2a2b598a72d124ab73f11959a5d28999d2b1718d Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 13 Jul 2023 09:47:49 +0100 Subject: [PATCH] This should get most embeds working again. TODO: fix polls & more testing needed --- twExtract/__init__.py | 20 ++++++++++++++++++-- twitfix.py | 17 +++++++++-------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/twExtract/__init__.py b/twExtract/__init__.py index 308ce23..4a43c2d 100644 --- a/twExtract/__init__.py +++ b/twExtract/__init__.py @@ -70,7 +70,7 @@ def extractStatus_syndication(url): # https://github.com/mikf/gallery-dl/blob/46cae04aa3a113c7b6bbee1bb468669564b14ae8/gallery_dl/extractor/twitter.py#L1784 m = re.search(pathregex, url) if m is None: - return extractStatus_token(url) + raise twExtractError.TwExtractError(400, "Extract error") twid = m.group(2) tweet = requests.get("https://cdn.syndication.twimg.com/tweet-result?id=" + twid) @@ -122,13 +122,29 @@ def extractStatusV2(url): # try another token continue tweet=output['data']['threaded_conversation_with_injections_v2']['instructions'][0]['entries'][0]["content"]["itemContent"]["tweet_results"]["result"] + if '__typename' in tweet and tweet['__typename'] == 'TweetWithVisibilityResults': + tweet=tweet['tweet'] except Exception as e: continue return tweet raise twExtractError.TwExtractError(400, "Extract error") +def extractStatusV2Legacy(url): + tweet = extractStatusV2(url) + if 'errors' in tweet or 'legacy' not in tweet: + raise twExtractError.TwExtractError(400, "Extract error") + tweet['legacy']['user'] = tweet["core"]["user_results"]["result"]["legacy"] + tweet['legacy']['user']['profile_image_url'] = tweet['legacy']['user']['profile_image_url_https'] + if 'card' in tweet: + tweet['legacy']['card'] = tweet['card']['legacy'] + if 'extended_entities' in tweet['legacy']: + tweet['legacy']['extended_entities'] = {'media':tweet['legacy']['extended_entities']['media']} + for media in tweet['legacy']['extended_entities']['media']: + media['media_url'] = media['media_url_https'] + return tweet['legacy'] + def extractStatus(url): - methods=[extractStatus_guestToken,extractStatus_syndication,extractStatus_token] + methods=[extractStatus_guestToken,extractStatus_syndication,extractStatus_token,extractStatusV2Legacy] for method in methods: try: return method(url) diff --git a/twitfix.py b/twitfix.py index 37da8fe..e99cc32 100644 --- a/twitfix.py +++ b/twitfix.py @@ -108,8 +108,6 @@ def twitfix(sub_path): twitter_url = "https://twitter.com/" + sub_path try: tweet = twExtract.extractStatusV2(twitter_url) - if '__typename' in tweet and tweet['__typename'] == 'TweetWithVisibilityResults': - tweet=tweet['tweet'] tweetL = tweet["legacy"] userL = tweet["core"]["user_results"]["result"]["legacy"] media=[] @@ -399,7 +397,7 @@ def link_to_vnf_from_tweet_data(tweet,video_link): isGif=True qrtURL = None - if 'quoted_status' in tweet and 'quoted_status_permalink' in tweet: + if 'quoted_status_permalink' in tweet: qrtURL = tweet['quoted_status_permalink']['expanded'] text = tweet['full_text'] @@ -417,11 +415,14 @@ def link_to_vnf_from_tweet_data(tweet,video_link): text = text.replace(eurl["url"],eurl["expanded_url"]) ttl = None #default - if 'card' in tweet and tweet['card']['name'].startswith('poll'): - poll=getPollObject(tweet['card']) - if tweet['card']['binding_values']['counts_are_final']['boolean_value'] == False: - ttl = datetime.today().replace(microsecond=0) + timedelta(minutes=1) - else: + try: + if 'card' in tweet and tweet['card']['name'].startswith('poll'): + poll=getPollObject(tweet['card']) + if tweet['card']['binding_values']['counts_are_final']['boolean_value'] == False: + ttl = datetime.today().replace(microsecond=0) + timedelta(minutes=1) + else: + poll=None + except: poll=None vnf = tweetInfo(