[nitter] add 'videos' option (#3279)

with the same semantics as for twitter
This commit is contained in:
Mike Fährmann
2022-11-24 22:56:01 +01:00
parent 8c4e21b110
commit e31d12139c
3 changed files with 52 additions and 19 deletions

View File

@@ -1860,6 +1860,20 @@ Description
You can use ``"all"`` instead of listing all values separately. You can use ``"all"`` instead of listing all values separately.
extractor.nitter.videos
-----------------------
Type
``bool`` or ``string``
Default
``true``
Description
Control video download behavior.
* ``true``: Download videos
* ``"ytdl"``: Download videos using `youtube-dl`_
* ``false``: Skip video Tweets
extractor.oauth.browser extractor.oauth.browser
----------------------- -----------------------
Type Type

View File

@@ -204,6 +204,9 @@
"password": null, "password": null,
"include": "illustration,doujin" "include": "illustration,doujin"
}, },
"nitter": {
"videos": true
},
"oauth": "oauth":
{ {
"browser": true, "browser": true,

View File

@@ -24,31 +24,47 @@ class NitterExtractor(BaseExtractor):
self.user = match.group(match.lastindex) self.user = match.group(match.lastindex)
def items(self): def items(self):
videos = self.config("videos", True)
ytdl = (videos == "ytdl")
for tweet_html in self.tweets(): for tweet_html in self.tweets():
tweet = self._tweet_from_html(tweet_html) tweet = self._tweet_from_html(tweet_html)
attachments_html = tweet.pop("_attach", "") attachments = tweet.pop("_attach", "")
if attachments_html: if attachments:
attachments = list(text.extract_iter( files = []
attachments_html, 'href="', '"')) append = files.append
attachments.extend(text.extract_iter(
attachments_html, 'data-url="', '"')) 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: else:
attachments = () files = ()
tweet["count"] = len(attachments) tweet["count"] = len(files)
yield Message.Directory, tweet yield Message.Directory, tweet
for tweet["num"], url in enumerate(attachments, 1): for tweet["num"], file in enumerate(files, 1):
if url[0] == "/": url = file["url"]
url = self.root + url file.update(tweet)
if "/video/" in url: if "extension" not in file:
url = "ytdl:" + url text.nameext_from_url(url, file)
tweet["filename"] = url.rpartition( yield Message.Url, url, file
"%2F")[2].partition(".")[0]
tweet["extension"] = "mp4"
else:
text.nameext_from_url(url, tweet)
yield Message.Url, url, tweet
def _tweet_from_html(self, html): def _tweet_from_html(self, html):
extr = text.extract_from(html) extr = text.extract_from(html)