# -*- coding: utf-8 -*- # Copyright 2022-2026 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. """Extractors for Nitter instances""" from .common import BaseExtractor, Message from .. import text, util class NitterExtractor(BaseExtractor): """Base class for nitter extractors""" basecategory = "nitter" directory_fmt = ("nitter", "{user[name]}") filename_fmt = "{tweet_id}_{num}.{extension}" archive_fmt = "{tweet_id}_{num}" useragent = util.USERAGENT_GALLERYDL request_interval = (0.5, 1.5) def __init__(self, match): self.cookies_domain = self.root.partition("://")[2] BaseExtractor.__init__(self, match) self.user = self.groups[-2] self.user_id = self.groups[-1] self.user_obj = None def items(self): retweets = self.config("retweets", False) if videos := self.config("videos", True): ytdl = (videos == "ytdl") videos = True self.cookies.set("hlsPlayback", "on", domain=self.cookies_domain) for tweet in self.tweets(): if not retweets and tweet["retweet"]: self.log.debug("Skipping %s (retweet)", tweet["tweet_id"]) continue if attachments := tweet.pop("_attach", ""): files = [] for url in text.extract_iter( attachments, 'href="', '"'): if "/i/broadcasts/" in url: self.log.debug( "Skipping unsupported broadcast '%s'", url) continue if "/enc/" in url: name = util.b64decode( url.rpartition("/")[2]).rpartition("/")[2] else: name = text.unquote(url.rpartition("%2F")[2]) if url[0] == "/": url = self.root + url file = text.nameext_from_url( name, {"url": url, "_http_retry": _retry_on_404}) files.append(file) if videos and not files: if ytdl: url = f"ytdl:{self.root}/i/status/{tweet['tweet_id']}" files.append({"url": url, "extension": "mp4"}) else: for url in text.extract_iter( attachments, 'data-url="', '"'): if "/enc/" in url: name = util.b64decode( url.rpartition("/")[2]).rpartition("/")[2] else: name = text.unquote(url.rpartition("%2F")[2]) if url[0] == "/": url = self.root + url files.append({ "url" : "ytdl:" + url, "filename" : name.rpartition(".")[0], "extension": "mp4", }) for url in text.extract_iter( attachments, '")[2], "_attach" : extr('class="attachments', 'class="tweet-stats'), "comments": text.parse_int(extr( 'class="icon-comment', '').rpartition(">")[2]), "retweets": text.parse_int(extr( 'class="icon-retweet', '').rpartition(">")[2]), "quotes" : text.parse_int(extr( 'class="icon-quote', '').rpartition(">")[2]), "likes" : text.parse_int(extr( 'class="icon-heart', '').rpartition(">")[2]), "retweet" : 'class="retweet-header' in html, "quoted" : False, } def _tweet_from_quote(self, html): extr = text.extract_from(html) author = { "name": extr('class="fullname" href="/', '"'), "nick": extr('title="', '"'), } extr('")[2], "_attach" : extr('class="attachments', ''' '''), "retweet" : False, "quoted" : True, } def _user_from_html(self, html): extr = text.extract_from(html, html.index('class="profile-tabs')) banner = extr('class="profile-banner">', '<'), "date" : self.parse_datetime( extr('class="profile-joindate">', '<').replace(",", "")), "friends_count" : text.parse_int(extr( 'class="profile-stat-num">', '<').replace(",", "")), "followers_count" : text.parse_int(extr( 'class="profile-stat-num">', '<').replace(",", "")), "favourites_count": text.parse_int(extr( 'class="profile-stat-num">', '<').replace(",", "")), "verified" : 'title="Verified account"' in html, } def _extract_quote(self, html): html, _, quote = html.partition('class="quote') if quote: quote, _, tail = quote.partition('class="tweet-published') return (html + tail, quote) return (html, None) def _pagination(self, path): more = None tries = 0 retries = self.config("fallback-retries", 2) + 1 quoted = self.config("quoted", False) if self.user_id: self.user = self.request_location( f"{self.root}/i/user/{self.user_id}", method="GET", ).rpartition("/")[2] base_url = url = f"{self.root}/{self.user}{path}" while True: tweets_html = self.request(url).text.split( '
No more items" in tweets_html[-1]: tries += 1 self.log.warning("Empty Tweet results (%s/%s)", tries, retries) if tries >= retries: break continue if self.user_obj is None: self.user_obj = self._user_from_html(tweets_html[0]) del tweets_html[0] for html, quote in map(self._extract_quote, tweets_html): tweet = self._tweet_from_html(html) if not tweet["date"]: continue yield tweet if quoted and quote: yield self._tweet_from_quote(quote) more = text.extr( tweets_html[-1], '