replace old %-formatted and .format(…) strings with f-strings (#7671)

mostly using flynt
https://github.com/ikamensh/flynt
This commit is contained in:
Mike Fährmann
2025-06-28 19:36:16 +02:00
parent f77e98b57d
commit 9dbe33b6de
167 changed files with 756 additions and 891 deletions

View File

@@ -181,11 +181,8 @@ class TwitterExtractor(Extractor):
if "video_info" in media:
if self.videos == "ytdl":
file = {
"url": "ytdl:{}/i/web/status/{}".format(
self.root, tweet["id_str"]),
"extension": "mp4",
}
url = f"ytdl:{self.root}/i/web/status/{tweet['id_str']}"
file = {"url": url, "extension": "mp4"}
elif self.videos:
video_info = media["video_info"]
variant = max(
@@ -291,7 +288,7 @@ class TwitterExtractor(Extractor):
if self.cards == "ytdl":
tweet_id = tweet.get("rest_id") or tweet["id_str"]
url = "ytdl:{}/i/web/status/{}".format(self.root, tweet_id)
url = f"ytdl:{self.root}/i/web/status/{tweet_id}"
files.append({"url": url})
def _extract_twitpic(self, tweet, files):
@@ -425,8 +422,7 @@ class TwitterExtractor(Extractor):
self._extract_media_source(
tdata, legacy["extended_entities"]["media"][0])
if tdata["retweet_id"]:
tdata["content"] = "RT @{}: {}".format(
author["name"], tdata["content"])
tdata["content"] = f"RT @{author['name']}: {tdata['content']}"
tdata["date_original"] = text.parse_timestamp(
((tdata["retweet_id"] >> 22) + 1288834974657) // 1000)
@@ -595,7 +591,7 @@ class TwitterUserExtractor(Dispatch, TwitterExtractor):
if user_id is not None:
user = "id:" + user_id
base = "{}/{}/".format(self.root, user)
base = f"{self.root}/{user}/"
return self._dispatch_extractors((
(TwitterInfoExtractor , base + "info"),
(TwitterAvatarExtractor , base + "photo"),
@@ -661,12 +657,12 @@ class TwitterTimelineExtractor(TwitterExtractor):
self.api._user_id_by_screen_name(self.user)
# build search query
query = "from:{} max_id:{}".format(self._user["name"], tweet_id)
query = f"from:{self._user['name']} max_id:{tweet_id}"
if self.retweets:
query += " include:retweets include:nativeretweets"
if state <= 2:
self._cursor_prefix = "2_{}/".format(tweet_id)
self._cursor_prefix = f"2_{tweet_id}/"
if reset:
self._cursor = self._cursor_prefix
@@ -682,7 +678,7 @@ class TwitterTimelineExtractor(TwitterExtractor):
if state <= 3:
# yield unfiltered search results
self._cursor_prefix = "3_{}/".format(tweet_id)
self._cursor_prefix = f"3_{tweet_id}/"
if reset:
self._cursor = self._cursor_prefix
@@ -845,7 +841,7 @@ class TwitterHashtagExtractor(TwitterExtractor):
example = "https://x.com/hashtag/NAME"
def items(self):
url = "{}/search?q=%23{}".format(self.root, self.user)
url = f"{self.root}/search?q=%23{self.user}"
data = {"_extractor": TwitterSearchExtractor}
yield Message.Queue, url, data
@@ -975,7 +971,7 @@ class TwitterQuotesExtractor(TwitterExtractor):
example = "https://x.com/USER/status/12345/quotes"
def items(self):
url = "{}/search?q=quoted_tweet_id:{}".format(self.root, self.user)
url = f"{self.root}/search?q=quoted_tweet_id:{self.user}"
data = {"_extractor": TwitterSearchExtractor}
yield Message.Queue, url, data
@@ -1053,8 +1049,7 @@ class TwitterImageExtractor(Extractor):
TwitterExtractor._init_sizes(self)
def items(self):
base = "https://pbs.twimg.com/media/{}?format={}&name=".format(
self.id, self.fmt)
base = f"https://pbs.twimg.com/media/{self.id}?format={self.fmt}&name="
data = {
"filename": self.id,
@@ -1389,7 +1384,7 @@ class TwitterAPI():
("viewer", "communities_timeline", "timeline"))
def live_event_timeline(self, event_id):
endpoint = "/2/live_event/timeline/{}.json".format(event_id)
endpoint = f"/2/live_event/timeline/{event_id}.json"
params = self.params.copy()
params["timeline_id"] = "recap"
params["urt"] = "true"
@@ -1397,7 +1392,7 @@ class TwitterAPI():
return self._pagination_legacy(endpoint, params)
def live_event(self, event_id):
endpoint = "/1.1/live_event/1/{}/timeline.json".format(event_id)
endpoint = f"/1.1/live_event/1/{event_id}/timeline.json"
params = self.params.copy()
params["count"] = "0"
params["urt"] = "true"
@@ -1482,9 +1477,9 @@ class TwitterAPI():
return user["rest_id"]
except KeyError:
if "unavailable_message" in user:
raise exception.NotFoundError("{} ({})".format(
user["unavailable_message"].get("text"),
user.get("reason")), False)
raise exception.NotFoundError(
f"{user['unavailable_message'].get('text')} "
f"({user.get('reason')})", False)
else:
raise exception.NotFoundError("user")
@@ -1782,12 +1777,10 @@ class TwitterAPI():
extr.log.info("Retrying API request as guest")
continue
raise exception.AuthorizationError(
"{} blocked your account".format(
user["screen_name"]))
f"{user['screen_name']} blocked your account")
elif user.get("protected"):
raise exception.AuthorizationError(
"{}'s Tweets are protected".format(
user["screen_name"]))
f"{user['screen_name']}'s Tweets are protected")
raise exception.StopExtraction(
"Unable to retrieve Tweets from this timeline")
@@ -2017,7 +2010,7 @@ def _login_impl(extr, username, password):
errors = []
for error in data.get("errors") or ():
msg = error.get("message")
errors.append('"{}"'.format(msg) if msg else "Unknown error")
errors.append(f'"{msg}"' if msg else "Unknown error")
extr.log.debug(response.text)
raise exception.AuthenticationError(", ".join(errors))
@@ -2161,7 +2154,7 @@ def _login_impl(extr, username, password):
"subtask_inputs": [inputs],
}
extr.sleep(random.uniform(1.0, 3.0), "login ({})".format(subtask))
extr.sleep(random.uniform(1.0, 3.0), f"login ({subtask})")
flow_token, subtask = process(data)
return {