Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Lin-jun-xiang committed Sep 21, 2023
1 parent 4b8d20e commit 629f12a
Showing 1 changed file with 0 additions and 37 deletions.
37 changes: 0 additions & 37 deletions pyga4/analytic/data_transform.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,10 @@
import base64
import json
from datetime import datetime, timedelta, timezone
from typing import Union, Iterable

from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad


class Transformer:
"""Transform the data of GA4"""

@staticmethod
def decryptData(
encoded_data: Union[str, Iterable],
key = b'secret_key'
) -> Union[str, list]:
""" Decrypt Data (string or json)
"""
if isinstance(encoded_data, str):
# 將 base64 編碼的數據解碼並進行解密
encrypted_data = base64.b64decode(encoded_data)

# 進行 Zero Padding
key = key.ljust(16, b'\x00')

# 創建 AES 解密器
cipher = AES.new(key, AES.MODE_CBC, key)

decrypted_data = unpad(cipher.decrypt(encrypted_data), AES.block_size)

try:
json_data = decrypted_data.decode()
data = json.loads(json_data)
except (json.JSONDecodeError, UnicodeDecodeError):
data = decrypted_data.decode()

return data

decrypted_list = []
for item in encoded_data:
decrypted_list.append(Transformer.decryptData(item, key))
return decrypted_list

@staticmethod
def timestamp_to_datetime(
ga4_timestamp: Union[str, Iterable]
Expand Down

0 comments on commit 629f12a

Please sign in to comment.