from base64 import b64encode def encode_search_string(publication="The Astrophysical Journal", fromYear=2022, toYear=2025, page=1 ): """ Returns the search string for the page we are interest in. Base search is: b'{"contentTypes":["Images"],"fromYear":2022,"toYear":2025,"page":1,"show":"200"}' """ # original code by Edwin Henneken # Encode a JSON formatted search string for Astronomy Image Explorer # into base 64 to use as a search URL: # The JSON format is of the form: # b'{"provider":"AAS Journals","contentTypes":["Images"],"page":1,"show":"200"}' # for contentSearch, otherwise: # {"provider":"AAS Journals","fromYear":2022,"page":1,"show":"200"} # and the encoded 64 bit string is of the form: # eyJwcm92aWRlciI6IkFBUyBKb3VybmFscyIsImZyb21ZZWFyIjoyMDIyLCJwYWdlIjoxLCJzaG93IjoiMjAwIn0=, # where the "=" is padding and needs to be removed before creating the url search_string = f"""{{"publications":["{publication}"],"contentTypes":["Images"],"fromYear":{fromYear},"toYear":{toYear},"page":{page},"show":"200"}}""" # Convert search string to byte search_string = bytes(search_string, 'utf-8') # Encode search string to base 64 search_string = b64encode(search_string) # Convert search string from byte format to a string # and remove any trailing "=" search_string = search_string.decode("utf-8").strip('=') # Append the search string to the URL search_string = "https://www.astroexplorer.org/search/" + search_string return search_string