kensvin commited on
Commit
2ee750c
1 Parent(s): 8099d9b

test with complete headers

Browse files
Files changed (1) hide show
  1. app.py +92 -51
app.py CHANGED
@@ -48,63 +48,104 @@ db = None
48
  qa = None
49
  cache = {}
50
 
 
51
 
52
  # Function to request product ID from Tokopedia
53
  def request_product_id(shop_domain, product_key):
54
  ENDPOINT = "https://gql.tokopedia.com/graphql/PDPGetLayoutQuery"
55
- payload = {
56
- "operationName": "PDPGetLayoutQuery",
57
- "variables": {
58
- "shopDomain": f"{shop_domain}",
59
- "productKey": f"{product_key}",
60
- "apiVersion": 1,
61
- },
62
- "query": """fragment ProductVariant on pdpDataProductVariant {
63
- errorCode
64
- parentID
65
- defaultChild
66
- children {
67
- productID
68
- }
69
- __typename
70
- }
71
-
72
- query PDPGetLayoutQuery($shopDomain: String, $productKey: String, $layoutID: String, $apiVersion: Float, $userLocation: pdpUserLocation, $extParam: String, $tokonow: pdpTokoNow, $deviceID: String) {
73
- pdpGetLayout(shopDomain: $shopDomain, productKey: $productKey, layoutID: $layoutID, apiVersion: $apiVersion, userLocation: $userLocation, extParam: $extParam, tokonow: $tokonow, deviceID: $deviceID) {
74
- requestID
75
- name
76
- pdpSession
77
- basicInfo {
78
- id: productID
79
- }
80
- components {
81
- name
82
- type
83
- position
84
- data {
85
- ...ProductVariant
86
- __typename
87
- }
88
- __typename
89
- }
90
- __typename
91
- }
92
- }
93
- """,
94
- }
95
  headers = {
96
- "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
97
- "Referer": "https://www.tokopedia.com",
98
- "X-TKPD-AKAMAI": "pdpGetLayout",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  }
100
- try:
101
- response = requests.post(ENDPOINT, json=payload, headers=headers, timeout=60)
102
- response.raise_for_status()
103
- logger.info(f"Request successful. Status code: {response.status_code}")
104
- return response
105
- except requests.exceptions.RequestException as e:
106
- logger.error(f"Request failed: {e}")
107
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
 
110
  # Function to request product reviews from Tokopedia
 
48
  qa = None
49
  cache = {}
50
 
51
+ import json
52
 
53
  # Function to request product ID from Tokopedia
54
  def request_product_id(shop_domain, product_key):
55
  ENDPOINT = "https://gql.tokopedia.com/graphql/PDPGetLayoutQuery"
56
+ payload = json.dumps([
57
+ {
58
+ "operationName": "PDPGetLayoutQuery",
59
+ "variables": {
60
+ "shopDomain": shop_domain,
61
+ "productKey": product_key,
62
+ "layoutID": "",
63
+ "apiVersion": 1
64
+ }
65
+ }
66
+ ])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  headers = {
68
+ 'authority': 'gql.tokopedia.com',
69
+ 'accept': '*/*',
70
+ 'accept-language': 'en-US,en;q=0.9',
71
+ 'content-type': 'application/json',
72
+ 'cookie': '_UUID_NONLOGIN_=e9727c37c5f733a77479185a66e63e4d; _UUID_NONLOGIN_.sig=tkAjvTdngH8Tn2TawWMZs8yir7g; DID=a717cbd11e2c1799009d1f87dd469aee95e922f0f927d3df40966a41e4eec18f634c74b0f2242b80393e711af4bf7119; DID_JS=YTcxN2NiZDExZTJjMTc5OTAwOWQxZjg3ZGQ0NjlhZWU5NWU5MjJmMGY5MjdkM2RmNDA5NjZhNDFlNGVlYzE4ZjYzNGM3NGIwZjIyNDJiODAzOTNlNzExYWY0YmY3MTE547DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=; __auc=f6d4b66f17cefc7db9583cc0ea3; _hjid=52f6214b-1f92-4aac-a3be-adc11e04aafc; cto_bundle=T0m1vF83VlNReTd2VXh6JTJGdGNtNXhZUDZMbkQ3WjZveUxUM1ZVUHdkd3FKcSUyQlNTMUclMkJtZHpDdyUyRllUU0x3ZWlHWHJGT2dIWWQ4WTdqejBxSTNJWFMwMGMlMkZHVXJuUWUyZG9VaDRlblczS0F5TWhJM0YzN2VRdDhwS3UlMkZzV2clMkYxRTlpczRXaWt3Z0xMbWJqbEhtZFg4VlFWV1ZmQSUzRCUzRA; _UUID_CAS_=cc18f322-9a5c-4cf6-9dfd-1270e46f8582; _CASE_=732a6c416c2a3238242a69416c2a3238242a646a642a322a2a242a6b416c2a3238242a6467666f2a322a2a242a64697c2a322a2a242a784b672a322a2a242a7f416c2a32393a3a39383b3f3d242a7b416c2a3239393d3b383d3f3b242a7b5c71786d2a322a3a602a242a7f607b2a322a5373542a7f697a6d60677d7b6d57616c542a32393a3a39383b3f3d24542a7b6d7a7e616b6d577c71786d542a32542a3a60542a24542a57577c71786d6669656d542a32542a5f697a6d60677d7b6d7b542a752473542a7f697a6d60677d7b6d57616c542a323824542a7b6d7a7e616b6d577c71786d542a32542a393d65542a24542a57577c71786d6669656d542a32542a5f697a6d60677d7b6d7b542a75552a75; _gcl_au=1.1.1070690307.1661094594; _jxx=74cb82d0-f38f-11ec-b88d-977b36f46df7; _jx=74cb82d0-f38f-11ec-b88d-977b36f46df7; hfv_banner=true; bm_sz=FEDD193B43C05ABC0ECC7F218FD5E1C0~YAAQrSE1F/1cP+SDAQAA9oK15BF3DdkWy7nnyvTZieay5zJg128l/5uxqPSqkvFazOh4Wv3W/4AUQLS9ZTkA7gC6IWSGdmyUZZDpZneKXHpnw0z91FQk9Ydt+eYC27M4tsYrzfda+aWzsuJrefZsvvOvug/ZvrS4RI1pFjgoeAaotUY3gVVJBEa7KCQis4W/5OO94n03wgyxu7fB1vq8Gve2bXtPMuOP9kc5ShEm/stdSNt9WjiVt4Yvg9TMEDMK/8UqBRsvXbD8YPvbYdWMUw12n/bq+LfTV2EPb8hs1YAkS1U8+IE=~3293746~3355201; _SID_Tokopedia_=699V7myhqHJTekLwfsmffoi8jhxDptrX0TwX7hPKexK0RauqTC_em34ZEmpLo2P4yP7P2bCiEz2ll3qvPtNZtHAc3ocJtX5BLZG8pSe5mP3NYlRhpiclF-cTdKOejSvt; _gid=GA1.2.750563175.1665989199; _jxxs=1665989198-74cb82d0-f38f-11ec-b88d-977b36f46df7; _jxs=1665989198-74cb82d0-f38f-11ec-b88d-977b36f46df7; __asc=c2c83db8183e4b1d556eeadc2a6; NR_SID=NRl9cf5q6os1xrv; AMP_TOKEN=%24NOT_FOUND; ak_bmsc=154ED6F998E215265D990C8CABCF4618~000000000000000000000000000000~YAAQrSE1F3NuP+SDAQAALge35BEb0jR/EDHlGS9fxmk/fhk3+qBfnzY2Jv4HxyBnR6N0LsUayf3aiZXy3bUvITnTC1ZgZmWsLLcdS2aqgATfgZVTpkFvW9tCgDV0L3fpPHYR3sGnhjtTirg4mfbGw+C3xEDjSn14AS7SNnN0aCgzZzX7e229gmZGfuOALcII9lwh7LwBRSSd/BSKyn3Y34PH7qqwOS+F624Xm9fNwbbMGdnadv9PfPteUhSC9U0cqgtDxE7pAjc8vewzIf2xm/cr0+dCN+s7shGPXDk+ELtFGKYgFyz1Sp1aOIs606S4hJpX/sTzXvLceppM0tJ66/vCIzwAJD4id/VwC3N1teNAly0yhb28sTe5lNA5RnTYE27j6cEAGLbKObWIzNyULaf3e2bv0o5utLMRpqGbApAoKvbDgOBPjF+rBxoKSoByJnxlCM98M0G9XAzxpRDnRFLujKTzZ8CKnMk9FrZpdbttHVJT9YTHic7TMHYIOsUgra67HX3PGg==; _abck=DB9B8AC184B53D64511C0CA8E46737CB~0~YAAQrSE1F9ZuP+SDAQAAKxG35AgWg2PHBBvM9/T36YC42lsBvwpdbpPSZABAxneyJL5ZbzE4lx5xlz2XwH9a8MQ5IfXHqhgsra6cBqqSzU8xgcNOFIlp7RMpBNfjV2Cwla2iNAzzdbmskpkIB8HqujKdWibzNMJpXB/YqmiZwj/FLyVR8kUpJo+UG0evJyaNil6vVqoXXUPQmUFSGAoArQTI8WXXlKanMKbaIh8xLxRgv1rt5kKf5/R1m6275w1fQfh83by6VurvHnEd0YDOndLmPJdXI8Piow/tMatTi3FNObNjmHg5CNA63K5yxPtTnJsl3kG1Wexk7cH4FFpG74EMGWHukQZ6IFpeUymcj52FjxWYPwAH0lEKNq/qdOibLir0JybgJeLz8xa1eN2kXLlo06yKOnEkUWDl~-1~-1~-1; _dc_gtm_UA-126956641-6=1; _ga_70947XW48P=GS1.1.1665989198.16.1.1665989428.60.0.0; _ga=GA1.2.426299726.1636110422; _dc_gtm_UA-9801603-6=1; _abck=DB9B8AC184B53D64511C0CA8E46737CB~-1~YAAQrSE1F6clQOSDAQAA8Z3F5AhK11JVGEv0Mwg28CHe7ro9JDkPKhPuivMf6GtvJC4Bk/p7zI4a2xBftcSFG9nLsyh8w7LWvqI9LXyIg8zU4rbbRPpp5yk+oCyh7u0KTSOM4XRJaXk2MKwIG+Irdo5rGB8e0UJy+dr6OsWCBl/bnTIXj2xIvqwKEbLiGyyNX+keTPXnqVhARZ/m0OmEUnreuuXiazWGwjCJMPeMd2H405ipu3hEEJYVDEaxMp+zpT1y3FqjjfgUkSzoISVkh2rF73Cpz4yYNfC0HQeI0E1mcDJjDcxXQjErIOkN1O5bcwK/fWpXcC7r9nWWyDUB8RJanaDewcwGelUaKbA6lOmoJwIuCK7ON8DQzweB4opfl/xTUD7GVBnTyxhavU1zs3G+FDX+9UwPAaOw~0~-1~-1; bm_sz=933FB20E8A08C7F904B2BBEFAF59CF75~YAAQrSE1F8lFP+SDAQAAGuez5BFT+bdQDJxSRM+CqoyWKuJfBc5YLC9LptyhgD3iV0UTDDXYfIRkrJDvV3Uec6IMyRTsdgAjoHmRZ7fcDgjn1ynK05v+6t+cnwthQS1mSNrX6pjpQXQ3GJYjyW4SOG/TxwhZdXe13s/IYVoT8wsqF3jE/zmnc+FRmDrDQRpll4sWG/F/nsWCuBmtRrbB9nuHCuLffgln81YTFV1rWA8koN7HsTzOhv8+t3U1tkERLb1/B4OIaNAiP777rxQXW1gXyC7PafPY98603/oT9yhiNBb1q1U=~4534576~3163705',
73
+ 'origin': 'https://www.tokopedia.com',
74
+ 'queryhash': 'v1:90338d207352e8b71cf754979b915218;false',
75
+ 'referer': 'https://www.tokopedia.com/miniso-official/miniso-sandal-rumah-slipper-wanita-selop-comfortable-nyaman-flip-flop-light-green-39-40?extParam=src%3Dmultiloc%26whid%3D7377294',
76
+ 'sec-ch-ua': '"Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"',
77
+ 'sec-ch-ua-mobile': '?1',
78
+ 'sec-ch-ua-platform': '"Android"',
79
+ 'sec-fetch-dest': 'empty',
80
+ 'sec-fetch-mode': 'cors',
81
+ 'sec-fetch-site': 'same-site',
82
+ 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Mobile Safari/537.36',
83
+ 'x-device': 'mobile',
84
+ 'x-source': 'tokopedia-lite',
85
+ 'x-tkpd-akamai': 'pdpGetLayout',
86
+ 'x-tkpd-lite-service': 'atreus',
87
+ 'x-version': '859a718'
88
  }
89
+
90
+ response = requests.post(ENDPOINT, headers=headers, data=payload)
91
+
92
+ data = response.json()
93
+
94
+ return data
95
+
96
+ # payload = {
97
+ # "operationName": "PDPGetLayoutQuery",
98
+ # "variables": {
99
+ # "shopDomain": f"{shop_domain}",
100
+ # "productKey": f"{product_key}",
101
+ # "apiVersion": 1,
102
+ # },
103
+ # "query": """fragment ProductVariant on pdpDataProductVariant {
104
+ # errorCode
105
+ # parentID
106
+ # defaultChild
107
+ # children {
108
+ # productID
109
+ # }
110
+ # __typename
111
+ # }
112
+
113
+ # query PDPGetLayoutQuery($shopDomain: String, $productKey: String, $layoutID: String, $apiVersion: Float, $userLocation: pdpUserLocation, $extParam: String, $tokonow: pdpTokoNow, $deviceID: String) {
114
+ # pdpGetLayout(shopDomain: $shopDomain, productKey: $productKey, layoutID: $layoutID, apiVersion: $apiVersion, userLocation: $userLocation, extParam: $extParam, tokonow: $tokonow, deviceID: $deviceID) {
115
+ # requestID
116
+ # name
117
+ # pdpSession
118
+ # basicInfo {
119
+ # id: productID
120
+ # }
121
+ # components {
122
+ # name
123
+ # type
124
+ # position
125
+ # data {
126
+ # ...ProductVariant
127
+ # __typename
128
+ # }
129
+ # __typename
130
+ # }
131
+ # __typename
132
+ # }
133
+ # }
134
+ # """,
135
+ # }
136
+ # headers = {
137
+ # "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
138
+ # "Referer": "https://www.tokopedia.com",
139
+ # "X-TKPD-AKAMAI": "pdpGetLayout",
140
+ # }
141
+ # try:
142
+ # response = requests.post(ENDPOINT, json=payload, headers=headers, timeout=60)
143
+ # response.raise_for_status()
144
+ # logger.info(f"Request successful. Status code: {response.status_code}")
145
+ # return response
146
+ # except requests.exceptions.RequestException as e:
147
+ # logger.error(f"Request failed: {e}")
148
+ # return None
149
 
150
 
151
  # Function to request product reviews from Tokopedia