Python scripting to automate fetching data from RDAS.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

main.py 1.2KB

1234567891011121314151617181920212223242526272829
  1. import requests, time, os, sys
  2. from bs4 import BeautifulSoup
  3. def main():
  4. endpoint = endpointBuilder(2017, "STATE", "YOSELL2", "CATAG18", False, "2", "DASWT_1", False, "json")
  5. print(endpoint)
  6. def endpointBuilder(year, row, col, control=None, control_filter_not=False, control_filter=None, weight=None, chisq=False, fmt=None):
  7. lcontrol = "" if control_filter == None else "&control={}".format(control)
  8. lcfnot = "" if control_filter_not == False else "!"
  9. lcontrol_filter = "" if (control_filter == None or lcontrol == "") else "&filter={}{}%3D{}".format(control, lcfnot, control_filter)
  10. lweight = "" if weight == None else "&weight={}".format(weight)
  11. lchisq = "&run_chisq=false" if chisq == False else "&run_chisq=true"
  12. lfmt = "json" if (fmt == None or fmt not in ["json", "msgpack", "api"]) else fmt
  13. year_rng = "{}-{}".format(int(year), int(year)+1)
  14. return "https://rdas.samhsa.gov/api/surveys/NSDUH-{}-RD02YR/crosstab/?row={}&column={}{}{}{}{}&format={}".format(
  15. year_rng,
  16. row,
  17. col,
  18. lcontrol,
  19. lcontrol_filter,
  20. lweight,
  21. lchisq,
  22. lfmt
  23. )
  24. if __name__ == "__main__":
  25. main()