# -*- coding: utf-8 -*- # Copyright 2022 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 class NitterExtractor(BaseExtractor): """Base class for nitter extractors""" basecategory = "nitter" directory_fmt = ("{category}", "{user[name]}") filename_fmt = "{tweet_id}_{num}.{extension}" archive_fmt = "{tweet_id}_{num}" def __init__(self, match): BaseExtractor.__init__(self, match) self.user = match.group(match.lastindex) def items(self): videos = self.config("videos", True) ytdl = (videos == "ytdl") for tweet_html in self.tweets(): tweet = self._tweet_from_html(tweet_html) attachments = tweet.pop("_attach", "") if attachments: files = [] append = files.append for url in text.extract_iter( attachments, 'href="', '"'): if url[0] == "/": url = self.root + url append({"url": url}) if videos and not files: if ytdl: append({ "url": "ytdl:{}/i/status/{}".format( self.root, tweet["tweet_id"]), "extension": None, }) else: for url in text.extract_iter( attachments, 'data-url="', '"'): if url[0] == "/": url = self.root + url append({"url": "ytdl:" + url}) else: files = () tweet["count"] = len(files) yield Message.Directory, tweet for tweet["num"], file in enumerate(files, 1): url = file["url"] file.update(tweet) if "extension" not in file: text.nameext_from_url(url, file) yield Message.Url, url, file def _tweet_from_html(self, html): extr = text.extract_from(html) user = { "name": extr('class="fullname" href="/', '"'), "nick": extr('title="', '"'), } extr('")[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]), } def _pagination(self, path): base_url = url = self.root + path while True: page = self.request(url).text yield from page.split('