Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added parser/__init__.py
Empty file.
57 changes: 57 additions & 0 deletions parser/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from urllib.parse import urlparse, unquote


def parse_after_question_mark(url):
url = unquote(url)
parsed_url = urlparse(url)
params = parsed_url.query.split("&")

dictionary = {}
for param in params:
if param != '':
dictionary.update({param.split("=")[0] : param.split("=")[1]})
return dictionary


def parse_between_quotes(pattern, url):
url1 = unquote(pattern)
parsed_url1 = urlparse(url1)
url2 = unquote(url)
parsed_url2 = urlparse(url2)

path1 = parsed_url1.path
path2 = parsed_url2.path

path1_parts = list(filter(None, path1.split("/")))
path2_parts = list(filter(None, path2.split("/")))

dictionary = {}
for i in range(len(path1_parts)):
if path1_parts[i].startswith("<") and path1_parts[i].endswith(">"):
sep = path1_parts[i][1:-1].split(":")
try:
if i < len(path2_parts):
if sep[0] == path1_parts[i][1:-1]:
dictionary.update({sep[0]:str(path2_parts[i])})
else:
if sep[1] == "str":
dictionary.update({sep[0]:str(path2_parts[i])})
elif sep[1] == "int":
dictionary.update({sep[0]:int(path2_parts[i])})
else:
raise ValueError("Invalid format")
else:
dictionary.update({sep[0]:None})
except:
raise ValueError("Invalid format")

return dictionary


if __name__ == "__main__":
pattern = "/main/accounts/<id:int>/<name:str>"
url = "/main/accounts/10/John?name=John&age=25"
print(parse_after_question_mark(url))
print(parse_between_quotes(pattern, url))
# {'name': 'John', 'age': '25'}
# {'id': 10, 'name': 'John'}