snowflake_reisacher.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import csv
  2. import json
  3. from datetime import datetime
  4. from pathlib import Path
  5. import requests
  6. import snowflake.connector
  7. url = "https://sq43047.eu-central-1.snowflakecomputing.com/oauth/token-request"
  8. # User: REISACHER_GLOBALCUBE
  9. # Password: ZXA9gmy4axa_wtd3vxu
  10. # Login: https://sq43047.eu-central-1.snowflakecomputing.com/oauth/authorize?response_type=code&client_id=AxayPnlaAHtkZ9FuBbERcf0Wa8E%3D&redirect_uri=https%3A%2F%2Flocalhost.com
  11. # clientid: AxayPnlaAHtkZ9FuBbERcf0Wa8E=
  12. # clientSecret: RC18m4LHUwcDlxSInZ5FOlUOQ8J4ud5LMcq7ATZNgps=
  13. # old code: A7FF59DAFCAE232C56413B78D0F5C48B33C6EE34
  14. # code: 9FDA41370466E0E108F1712FAEF08297CB05E81B
  15. login = {
  16. "user": "AxayPnlaAHtkZ9FuBbERcf0Wa8E=",
  17. "password": "RC18m4LHUwcDlxSInZ5FOlUOQ8J4ud5LMcq7ATZNgps=",
  18. }
  19. credentials = {
  20. "user": "1103",
  21. "account": "sq43047.eu-central-1",
  22. "authenticator": "oauth",
  23. "token": "256A1D8740C7AC1FE617BFF4B2C74E31789BE754",
  24. "database": "REISACHER",
  25. "schema": "EXTRAKTION",
  26. }
  27. sql = """
  28. select
  29. *
  30. from "01_EXTR_REISACHER_RSP_ACCOUNTCUSTOMERCARES" AS "T1"
  31. inner join "01_EXTR_REISACHER_RSP_ACCOUNTS_LEGALENTITYINFORMATION" AS "T2" ON "T1"."accountId" = "T2"."accountId"
  32. """
  33. # sql = "SHOW TABLES"
  34. def get_auth_code():
  35. payload = {
  36. "grant_type": "authorization_code",
  37. "code": credentials["token"],
  38. "redirect_uri": "https://localhost.com",
  39. }
  40. r = requests.post(
  41. url=url,
  42. auth=(*login.values(),),
  43. data=payload,
  44. )
  45. token = r.json()
  46. return token
  47. def get_access_token(token):
  48. payload = {
  49. "grant_type": "refresh_token",
  50. "refresh_token": token["refresh_token"],
  51. "redirect_uri": "https://localhost.com",
  52. }
  53. r = requests.post(
  54. url=url,
  55. auth=(*login.values(),),
  56. data=payload,
  57. )
  58. token = r.json()
  59. return token
  60. def snowflake_to_csv(csv_export_file):
  61. token_json = Path("token.json")
  62. if token_json.exists():
  63. token = json.load(token_json.open("r"))
  64. if datetime.now().timestamp() > token_json.stat().st_mtime + token["expires_in"]:
  65. token = token.update(get_access_token(token))
  66. json.dump(token, token_json.open("w"), indent=2)
  67. else:
  68. token = get_auth_code()
  69. json.dump(token, token_json.open("w"), indent=2)
  70. credentials["token"] = token["access_token"]
  71. # get_refresh_token()
  72. with snowflake.connector.connect(**credentials) as conn:
  73. cur = conn.cursor()
  74. cur.execute(sql)
  75. with Path(csv_export_file).open("w", encoding="latin-1", newline="") as fwh:
  76. csv_export = csv.writer(fwh, delimiter=";")
  77. csv_export.writerow([i[0] for i in cur.description])
  78. csv_export.writerows(cur.fetchall())
  79. if __name__ == "__main__":
  80. snowflake_to_csv(Path(__file__).parent / "01_EXTR_REISACHER_RSP_ACCOUNTCUSTOMERCARES.csv")
  81. # print(datetime.now().timestamp())