[text] add second argument to 'parse_query_list()' (#7138)
return only values whose name is in 'as_list' as a list
This commit is contained in:
@@ -372,7 +372,8 @@ class CivitaiUserImagesExtractor(CivitaiExtractor):
|
||||
example = "https://civitai.com/user/USER/images"
|
||||
|
||||
def __init__(self, match):
|
||||
self.params = text.parse_query_list(match.group(2))
|
||||
self.params = text.parse_query_list(
|
||||
match.group(2), {"reactions"})
|
||||
if self.params.get("section") == "reactions":
|
||||
self.subcategory = "reactions"
|
||||
self.images = self.images_reactions
|
||||
@@ -392,12 +393,8 @@ class CivitaiUserImagesExtractor(CivitaiExtractor):
|
||||
params = self.params
|
||||
params["authed"] = True
|
||||
params["useIndex"] = False
|
||||
if "reactions" in params:
|
||||
if isinstance(params["reactions"], str):
|
||||
params["reactions"] = (params["reactions"],)
|
||||
else:
|
||||
params["reactions"] = (
|
||||
"Like", "Dislike", "Heart", "Laugh", "Cry")
|
||||
if "reactions" not in params:
|
||||
params["reactions"] = ("Like", "Dislike", "Heart", "Laugh", "Cry")
|
||||
return self.api.images(params)
|
||||
|
||||
|
||||
|
||||
@@ -80,7 +80,8 @@ class ItakuSearchExtractor(ItakuExtractor):
|
||||
example = "https://itaku.ee/home/images?tags=SEARCH"
|
||||
|
||||
def posts(self):
|
||||
params = text.parse_query_list(self.groups[0])
|
||||
params = text.parse_query_list(
|
||||
self.groups[0], {"tags", "maturity_rating"})
|
||||
return self.api.search_images(params)
|
||||
|
||||
|
||||
@@ -99,13 +100,7 @@ class ItakuAPI():
|
||||
negative_tags = []
|
||||
optional_tags = []
|
||||
|
||||
tags = params.pop("tags", None)
|
||||
if not tags:
|
||||
tags = ()
|
||||
elif isinstance(tags, str):
|
||||
tags = (tags,)
|
||||
|
||||
for tag in tags:
|
||||
for tag in params.pop("tags", None) or ():
|
||||
if not tag:
|
||||
pass
|
||||
elif tag[0] == "-":
|
||||
|
||||
@@ -258,10 +258,10 @@ def parse_query(qs):
|
||||
return result
|
||||
|
||||
|
||||
def parse_query_list(qs):
|
||||
def parse_query_list(qs, as_list=()):
|
||||
"""Parse a query string into name-value pairs
|
||||
|
||||
Combine values of duplicate names into lists
|
||||
Combine values of names in 'as_list' into lists
|
||||
"""
|
||||
if not qs:
|
||||
return {}
|
||||
@@ -273,14 +273,13 @@ def parse_query_list(qs):
|
||||
if eq:
|
||||
name = unquote(name.replace("+", " "))
|
||||
value = unquote(value.replace("+", " "))
|
||||
if name in result:
|
||||
rvalue = result[name]
|
||||
if isinstance(rvalue, list):
|
||||
rvalue.append(value)
|
||||
if name in as_list:
|
||||
if name in result:
|
||||
result[name].append(value)
|
||||
else:
|
||||
result[name] = [rvalue, value]
|
||||
else:
|
||||
result[name] = value
|
||||
result[name] = [value]
|
||||
elif name not in result:
|
||||
result[name] = unquote(value.replace("+", " "))
|
||||
except Exception:
|
||||
pass
|
||||
return result
|
||||
|
||||
@@ -109,7 +109,7 @@ __tests__ = (
|
||||
},
|
||||
|
||||
{
|
||||
"#url" : "https://itaku.ee/home/images?tags=%2Bcute&tags=-cute&tags=~cute&maturity_rating=SFW&date_range=&ordering=-date_added",
|
||||
"#url" : "https://itaku.ee/home/images?tags=cute&tags=-cute&tags=~cute&maturity_rating=SFW&date_range=&ordering=-date_added",
|
||||
"#comment" : "search with postive, negative, and optional tags",
|
||||
"#category": ("", "itaku", "search"),
|
||||
"#class" : itaku.ItakuSearchExtractor,
|
||||
|
||||
@@ -431,10 +431,10 @@ class TestText(unittest.TestCase):
|
||||
self.assertEqual(f("foo=1&bar&baz=3"), {"foo": "1", "baz": "3"})
|
||||
|
||||
# keys with identical names
|
||||
self.assertEqual(f("foo=1&foo=2"), {"foo": ["1", "2"]})
|
||||
self.assertEqual(f("foo=1&foo=2", ("foo",)), {"foo": ["1", "2"]})
|
||||
self.assertEqual(
|
||||
f("foo=1&bar=2&foo=3&bar=4&foo=5"),
|
||||
{"foo": ["1", "3", "5"], "bar": ["2", "4"]},
|
||||
f("foo=1&bar=2&foo=3&bar=4&foo=5", {"foo", "baz"}),
|
||||
{"foo": ["1", "3", "5"], "bar": "2"},
|
||||
)
|
||||
|
||||
# invalid arguments
|
||||
|
||||
Reference in New Issue
Block a user