From 5469b5a0084a0f9e9261d3ba8e541c8ae57017b0 Mon Sep 17 00:00:00 2001 From: girst Date: Sat, 29 Aug 2020 10:30:58 +0200 Subject: [PATCH] make |G() take multiple keys --- app/common/innertube.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/common/innertube.py b/app/common/innertube.py index 825fa0b..df7236e 100644 --- a/app/common/innertube.py +++ b/app/common/innertube.py @@ -21,12 +21,15 @@ class G: null-coalescing version of dict.get() that also works on lists. the | operator is overloaded to achieve similar looking code to jq(1) filters. + the first found key is used: dict(foo=1)|G('bar','foo') returns 1. """ - def __init__(self, key): - self.key = key + def __init__(self, *keys): + self.keys = keys def __ror__(self, other): - try: return other[self.key] - except: return None + for key in self.keys: + try: return other[key] + except: continue + return None class _Text: """ parses youtube's .runs[].text and .simpleText variants """ def __ror__(self, other): # Note: only returning runs[0], not concat'ing all! @@ -166,8 +169,7 @@ def parse_result_items(items): results.append({'type': 'VIDEO', 'content': { 'video_id': content['videoId'], 'title': content['title']|G.text, - 'author': content|G('longBylineText')|G.text or \ - content|G('shortBylineText')|G.text, + 'author': content|G('longBylineText','shortBylineText')|G.text, 'channel_id': content|G('ownerText')|G('runs')|G(0) \ |G('navigationEndpoint')|G('browseEndpoint')|G('browseId'), 'length': content|G('lengthText')|G.text, # "44:07", "1:41:50" -- 2.39.3