[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"
|
example = "https://civitai.com/user/USER/images"
|
||||||
|
|
||||||
def __init__(self, match):
|
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":
|
if self.params.get("section") == "reactions":
|
||||||
self.subcategory = "reactions"
|
self.subcategory = "reactions"
|
||||||
self.images = self.images_reactions
|
self.images = self.images_reactions
|
||||||
@@ -392,12 +393,8 @@ class CivitaiUserImagesExtractor(CivitaiExtractor):
|
|||||||
params = self.params
|
params = self.params
|
||||||
params["authed"] = True
|
params["authed"] = True
|
||||||
params["useIndex"] = False
|
params["useIndex"] = False
|
||||||
if "reactions" in params:
|
if "reactions" not in params:
|
||||||
if isinstance(params["reactions"], str):
|
params["reactions"] = ("Like", "Dislike", "Heart", "Laugh", "Cry")
|
||||||
params["reactions"] = (params["reactions"],)
|
|
||||||
else:
|
|
||||||
params["reactions"] = (
|
|
||||||
"Like", "Dislike", "Heart", "Laugh", "Cry")
|
|
||||||
return self.api.images(params)
|
return self.api.images(params)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -80,7 +80,8 @@ class ItakuSearchExtractor(ItakuExtractor):
|
|||||||
example = "https://itaku.ee/home/images?tags=SEARCH"
|
example = "https://itaku.ee/home/images?tags=SEARCH"
|
||||||
|
|
||||||
def posts(self):
|
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)
|
return self.api.search_images(params)
|
||||||
|
|
||||||
|
|
||||||
@@ -99,13 +100,7 @@ class ItakuAPI():
|
|||||||
negative_tags = []
|
negative_tags = []
|
||||||
optional_tags = []
|
optional_tags = []
|
||||||
|
|
||||||
tags = params.pop("tags", None)
|
for tag in params.pop("tags", None) or ():
|
||||||
if not tags:
|
|
||||||
tags = ()
|
|
||||||
elif isinstance(tags, str):
|
|
||||||
tags = (tags,)
|
|
||||||
|
|
||||||
for tag in tags:
|
|
||||||
if not tag:
|
if not tag:
|
||||||
pass
|
pass
|
||||||
elif tag[0] == "-":
|
elif tag[0] == "-":
|
||||||
|
|||||||
@@ -258,10 +258,10 @@ def parse_query(qs):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def parse_query_list(qs):
|
def parse_query_list(qs, as_list=()):
|
||||||
"""Parse a query string into name-value pairs
|
"""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:
|
if not qs:
|
||||||
return {}
|
return {}
|
||||||
@@ -273,14 +273,13 @@ def parse_query_list(qs):
|
|||||||
if eq:
|
if eq:
|
||||||
name = unquote(name.replace("+", " "))
|
name = unquote(name.replace("+", " "))
|
||||||
value = unquote(value.replace("+", " "))
|
value = unquote(value.replace("+", " "))
|
||||||
if name in result:
|
if name in as_list:
|
||||||
rvalue = result[name]
|
if name in result:
|
||||||
if isinstance(rvalue, list):
|
result[name].append(value)
|
||||||
rvalue.append(value)
|
|
||||||
else:
|
else:
|
||||||
result[name] = [rvalue, value]
|
result[name] = [value]
|
||||||
else:
|
elif name not in result:
|
||||||
result[name] = value
|
result[name] = unquote(value.replace("+", " "))
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
return result
|
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",
|
"#comment" : "search with postive, negative, and optional tags",
|
||||||
"#category": ("", "itaku", "search"),
|
"#category": ("", "itaku", "search"),
|
||||||
"#class" : itaku.ItakuSearchExtractor,
|
"#class" : itaku.ItakuSearchExtractor,
|
||||||
|
|||||||
@@ -431,10 +431,10 @@ class TestText(unittest.TestCase):
|
|||||||
self.assertEqual(f("foo=1&bar&baz=3"), {"foo": "1", "baz": "3"})
|
self.assertEqual(f("foo=1&bar&baz=3"), {"foo": "1", "baz": "3"})
|
||||||
|
|
||||||
# keys with identical names
|
# 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(
|
self.assertEqual(
|
||||||
f("foo=1&bar=2&foo=3&bar=4&foo=5"),
|
f("foo=1&bar=2&foo=3&bar=4&foo=5", {"foo", "baz"}),
|
||||||
{"foo": ["1", "3", "5"], "bar": ["2", "4"]},
|
{"foo": ["1", "3", "5"], "bar": "2"},
|
||||||
)
|
)
|
||||||
|
|
||||||
# invalid arguments
|
# invalid arguments
|
||||||
|
|||||||
Reference in New Issue
Block a user