import requests, time, os, sys from bs4 import BeautifulSoup def main(): endpoint = endpointBuilder(2017, "STATE", "YOSELL2", "CATAG18", False, "2", "DASWT_1", False, "json") print(endpoint) def endpointBuilder(year, row, col, control=None, control_filter_not=False, control_filter=None, weight=None, chisq=False, fmt=None): lcontrol = "" if control_filter == None else "&control={}".format(control) lcfnot = "" if control_filter_not == False else "!" lcontrol_filter = "" if (control_filter == None or lcontrol == "") else "&filter={}{}%3D{}".format(control, lcfnot, control_filter) lweight = "" if weight == None else "&weight={}".format(weight) lchisq = "&run_chisq=false" if chisq == False else "&run_chisq=true" lfmt = "json" if (fmt == None or fmt not in ["json", "msgpack", "api"]) else fmt year_rng = "{}-{}".format(int(year), int(year)+1) return "https://rdas.samhsa.gov/api/surveys/NSDUH-{}-RD02YR/crosstab/?row={}&column={}{}{}{}{}&format={}".format( year_rng, row, col, lcontrol, lcontrol_filter, lweight, lchisq, lfmt ) if __name__ == "__main__": main()