Created
August 30, 2021 18:51
-
-
Save birkin/a8cdbdfab58433cc9a817f45b7e5dd9e to your computer and use it in GitHub Desktop.
non-django querystring handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Reminder for handling querystrings outside of django. | |
| Notes... | |
| - It's legal to have duplicate keys | |
| - This is a messy, real openURL, with three foo test keys | |
| - foo1 shows that a string with a comma is still parsed as a single string | |
| - foo2 shows that a key without a val is ignored | |
| - foo3 shows that a key with an `=` but without a val is also ignored | |
| ''' | |
| import pprint | |
| from urllib import parse | |
| url = '''https://bruknow.library.brown.edu/discovery/openurl?institution=01BU_INST&vid=01BU_INST:BROWN&date=2020&rft_val_fmt=info:ofi%2Ffmt:kev:mtx:book&aulast=Reed&rft_id=info:oclcnum%2F1122724548&rft_id=urn:ISBN:9781566895842&isbn=9781566895842&rfr_id=info:sid%2Ffirstsearch.oclc.org:WorldCat&linktype=openurl&rft.isbn=9781566895842&pid=1122724548%3Cfssessid%3E0%3C%2Ffssessid%3E&rft.genre=unknown&rft.genre=unknown&rft.aufirst=Justin&title=The%20malevolent%20volume&title=Brown%20University&aufirst=Justin&sid=FirstSearch:WorldCat&rft.aulast=Reed&req_dat=%3Csessionid%3E0%3C%2Fsessionid%3E&url_ver=Z39.88-2004&rft.date=2020&checksum=23ee504fe7c47fc1a54186fc2e68bd86&rfe_dat=%3Caccessionnumber%3E1122724548%3C%2Faccessionnumber%3E&id=doi:&detail=RBN&rft.title=The%20malevolent%20volume&foo1=barA,barB&foo2&foo3=''' | |
| print( f'\nurl, ``{url}``' ) | |
| parse_obj = parse.urlsplit( url ) | |
| querystring = parse_obj.query | |
| print( f'\nquerystring, ``{querystring}``' ) | |
| initial_dict = parse.parse_qs( querystring ) | |
| assert type( initial_dict ) == dict | |
| print( f'\ninitial_dict, ``{pprint.pformat(initial_dict)}``' ) | |
| final_dct = {} | |
| for ( key, val ) in initial_dict.items(): | |
| if len(val) == 1: | |
| final_dct[key] = val[0] | |
| else: | |
| final_dct[key] = val | |
| print( f'\nfinal_dict, ``{pprint.pformat(final_dct)}``' ) | |
| # ---------- | |
| # output... | |
| # ---------- | |
| # url, ``https://bruknow.library.brown.edu/discovery/openurl?institution=01BU_INST&vid=01BU_INST:BROWN&date=2020&rft_val_fmt=info:ofi%2Ffmt:kev:mtx:book&aulast=Reed&rft_id=info:oclcnum%2F1122724548&rft_id=urn:ISBN:9781566895842&isbn=9781566895842&rfr_id=info:sid%2Ffirstsearch.oclc.org:WorldCat&linktype=openurl&rft.isbn=9781566895842&pid=1122724548%3Cfssessid%3E0%3C%2Ffssessid%3E&rft.genre=unknown&rft.genre=unknown&rft.aufirst=Justin&title=The%20malevolent%20volume&title=Brown%20University&aufirst=Justin&sid=FirstSearch:WorldCat&rft.aulast=Reed&req_dat=%3Csessionid%3E0%3C%2Fsessionid%3E&url_ver=Z39.88-2004&rft.date=2020&checksum=23ee504fe7c47fc1a54186fc2e68bd86&rfe_dat=%3Caccessionnumber%3E1122724548%3C%2Faccessionnumber%3E&id=doi:&detail=RBN&rft.title=The%20malevolent%20volume&foo1=barA,barB&foo2&foo3=`` | |
| # querystring, ``institution=01BU_INST&vid=01BU_INST:BROWN&date=2020&rft_val_fmt=info:ofi%2Ffmt:kev:mtx:book&aulast=Reed&rft_id=info:oclcnum%2F1122724548&rft_id=urn:ISBN:9781566895842&isbn=9781566895842&rfr_id=info:sid%2Ffirstsearch.oclc.org:WorldCat&linktype=openurl&rft.isbn=9781566895842&pid=1122724548%3Cfssessid%3E0%3C%2Ffssessid%3E&rft.genre=unknown&rft.genre=unknown&rft.aufirst=Justin&title=The%20malevolent%20volume&title=Brown%20University&aufirst=Justin&sid=FirstSearch:WorldCat&rft.aulast=Reed&req_dat=%3Csessionid%3E0%3C%2Fsessionid%3E&url_ver=Z39.88-2004&rft.date=2020&checksum=23ee504fe7c47fc1a54186fc2e68bd86&rfe_dat=%3Caccessionnumber%3E1122724548%3C%2Faccessionnumber%3E&id=doi:&detail=RBN&rft.title=The%20malevolent%20volume&foo1=barA,barB&foo2&foo3=`` | |
| # initial_dict, ``{'aufirst': ['Justin'], | |
| # 'aulast': ['Reed'], | |
| # 'checksum': ['23ee504fe7c47fc1a54186fc2e68bd86'], | |
| # 'date': ['2020'], | |
| # 'detail': ['RBN'], | |
| # 'foo1': ['barA,barB'], | |
| # 'id': ['doi:'], | |
| # 'institution': ['01BU_INST'], | |
| # 'isbn': ['9781566895842'], | |
| # 'linktype': ['openurl'], | |
| # 'pid': ['1122724548<fssessid>0</fssessid>'], | |
| # 'req_dat': ['<sessionid>0</sessionid>'], | |
| # 'rfe_dat': ['<accessionnumber>1122724548</accessionnumber>'], | |
| # 'rfr_id': ['info:sid/firstsearch.oclc.org:WorldCat'], | |
| # 'rft.aufirst': ['Justin'], | |
| # 'rft.aulast': ['Reed'], | |
| # 'rft.date': ['2020'], | |
| # 'rft.genre': ['unknown', 'unknown'], | |
| # 'rft.isbn': ['9781566895842'], | |
| # 'rft.title': ['The malevolent volume'], | |
| # 'rft_id': ['info:oclcnum/1122724548', 'urn:ISBN:9781566895842'], | |
| # 'rft_val_fmt': ['info:ofi/fmt:kev:mtx:book'], | |
| # 'sid': ['FirstSearch:WorldCat'], | |
| # 'title': ['The malevolent volume', 'Brown University'], | |
| # 'url_ver': ['Z39.88-2004'], | |
| # 'vid': ['01BU_INST:BROWN']}`` | |
| # final_dict, ``{'aufirst': 'Justin', | |
| # 'aulast': 'Reed', | |
| # 'checksum': '23ee504fe7c47fc1a54186fc2e68bd86', | |
| # 'date': '2020', | |
| # 'detail': 'RBN', | |
| # 'foo1': 'barA,barB', | |
| # 'id': 'doi:', | |
| # 'institution': '01BU_INST', | |
| # 'isbn': '9781566895842', | |
| # 'linktype': 'openurl', | |
| # 'pid': '1122724548<fssessid>0</fssessid>', | |
| # 'req_dat': '<sessionid>0</sessionid>', | |
| # 'rfe_dat': '<accessionnumber>1122724548</accessionnumber>', | |
| # 'rfr_id': 'info:sid/firstsearch.oclc.org:WorldCat', | |
| # 'rft.aufirst': 'Justin', | |
| # 'rft.aulast': 'Reed', | |
| # 'rft.date': '2020', | |
| # 'rft.genre': ['unknown', 'unknown'], | |
| # 'rft.isbn': '9781566895842', | |
| # 'rft.title': 'The malevolent volume', | |
| # 'rft_id': ['info:oclcnum/1122724548', 'urn:ISBN:9781566895842'], | |
| # 'rft_val_fmt': 'info:ofi/fmt:kev:mtx:book', | |
| # 'sid': 'FirstSearch:WorldCat', | |
| # 'title': ['The malevolent volume', 'Brown University'], | |
| # 'url_ver': 'Z39.88-2004', | |
| # 'vid': '01BU_INST:BROWN'}`` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment