Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -52,91 +52,57 @@ import json
|
|
52 |
|
53 |
# Function to request product ID from Tokopedia
|
54 |
def request_product_id(shop_domain, product_key, url):
|
55 |
-
|
56 |
payload = {
|
57 |
"operationName": "PDPGetLayoutQuery",
|
58 |
"variables": {
|
59 |
-
"shopDomain": shop_domain,
|
60 |
-
"productKey": product_key,
|
61 |
-
"layoutID": "",
|
62 |
"apiVersion": 1,
|
63 |
},
|
64 |
-
"query": "fragment ProductVariant on pdpDataProductVariant
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
}
|
|
|
66 |
headers = {
|
67 |
-
"
|
68 |
-
"
|
69 |
-
"
|
70 |
-
"Referer": url,
|
71 |
-
"Sec-Ch-Ua": '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
|
72 |
-
"Sec-Ch-Ua-Mobile": "?0",
|
73 |
-
"Sec-Ch-Ua-Platform": '"Windows"',
|
74 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
75 |
-
"X-Device": "desktop",
|
76 |
-
"X-Source": "tokopedia-lite",
|
77 |
-
"X-Tkpd-Akamai": "pdpGetLayout",
|
78 |
-
"X-Tkpd-Lite-Service": "zeus",
|
79 |
-
"X-Version": "bcc81b6",
|
80 |
}
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
return response
|
86 |
-
|
87 |
-
# payload = {
|
88 |
-
# "operationName": "PDPGetLayoutQuery",
|
89 |
-
# "variables": {
|
90 |
-
# "shopDomain": f"{shop_domain}",
|
91 |
-
# "productKey": f"{product_key}",
|
92 |
-
# "apiVersion": 1,
|
93 |
-
# },
|
94 |
-
# "query": """fragment ProductVariant on pdpDataProductVariant {
|
95 |
-
# errorCode
|
96 |
-
# parentID
|
97 |
-
# defaultChild
|
98 |
-
# children {
|
99 |
-
# productID
|
100 |
-
# }
|
101 |
-
# __typename
|
102 |
-
# }
|
103 |
-
|
104 |
-
# query PDPGetLayoutQuery($shopDomain: String, $productKey: String, $layoutID: String, $apiVersion: Float, $userLocation: pdpUserLocation, $extParam: String, $tokonow: pdpTokoNow, $deviceID: String) {
|
105 |
-
# pdpGetLayout(shopDomain: $shopDomain, productKey: $productKey, layoutID: $layoutID, apiVersion: $apiVersion, userLocation: $userLocation, extParam: $extParam, tokonow: $tokonow, deviceID: $deviceID) {
|
106 |
-
# requestID
|
107 |
-
# name
|
108 |
-
# pdpSession
|
109 |
-
# basicInfo {
|
110 |
-
# id: productID
|
111 |
-
# }
|
112 |
-
# components {
|
113 |
-
# name
|
114 |
-
# type
|
115 |
-
# position
|
116 |
-
# data {
|
117 |
-
# ...ProductVariant
|
118 |
-
# __typename
|
119 |
-
# }
|
120 |
-
# __typename
|
121 |
-
# }
|
122 |
-
# __typename
|
123 |
-
# }
|
124 |
-
# }
|
125 |
-
# """,
|
126 |
-
# }
|
127 |
-
# headers = {
|
128 |
-
# "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
|
129 |
-
# "Referer": "https://www.tokopedia.com",
|
130 |
-
# "X-TKPD-AKAMAI": "pdpGetLayout",
|
131 |
-
# }
|
132 |
-
# try:
|
133 |
-
# response = requests.post(ENDPOINT, json=payload, headers=headers, timeout=60)
|
134 |
-
# response.raise_for_status()
|
135 |
-
# logger.info(f"Request successful. Status code: {response.status_code}")
|
136 |
-
# return response
|
137 |
-
# except requests.exceptions.RequestException as e:
|
138 |
-
# logger.error(f"Request failed: {e}")
|
139 |
-
# return None
|
140 |
|
141 |
|
142 |
# Function to request product reviews from Tokopedia
|
|
|
52 |
|
53 |
# Function to request product ID from Tokopedia
|
54 |
def request_product_id(shop_domain, product_key, url):
|
55 |
+
endpoint = "https://gql.tokopedia.com/graphql/PDPGetLayoutQuery"
|
56 |
payload = {
|
57 |
"operationName": "PDPGetLayoutQuery",
|
58 |
"variables": {
|
59 |
+
"shopDomain": f"{shop_domain}",
|
60 |
+
"productKey": f"{product_key}",
|
|
|
61 |
"apiVersion": 1,
|
62 |
},
|
63 |
+
"query": """fragment ProductVariant on pdpDataProductVariant {
|
64 |
+
errorCode
|
65 |
+
parentID
|
66 |
+
defaultChild
|
67 |
+
children {
|
68 |
+
productID
|
69 |
+
}
|
70 |
+
__typename
|
71 |
+
}
|
72 |
+
|
73 |
+
query PDPGetLayoutQuery($shopDomain: String, $productKey: String, $layoutID: String, $apiVersion: Float, $userLocation: pdpUserLocation, $extParam: String, $tokonow: pdpTokoNow, $deviceID: String) {
|
74 |
+
pdpGetLayout(shopDomain: $shopDomain, productKey: $productKey, layoutID: $layoutID, apiVersion: $apiVersion, userLocation: $userLocation, extParam: $extParam, tokonow: $tokonow, deviceID: $deviceID) {
|
75 |
+
requestID
|
76 |
+
name
|
77 |
+
pdpSession
|
78 |
+
basicInfo {
|
79 |
+
id: productID
|
80 |
+
}
|
81 |
+
components {
|
82 |
+
name
|
83 |
+
type
|
84 |
+
position
|
85 |
+
data {
|
86 |
+
...ProductVariant
|
87 |
+
__typename
|
88 |
+
}
|
89 |
+
__typename
|
90 |
+
}
|
91 |
+
__typename
|
92 |
+
}
|
93 |
+
}
|
94 |
+
""",
|
95 |
}
|
96 |
+
|
97 |
headers = {
|
98 |
+
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
|
99 |
+
"Referer": "https://www.tokopedia.com",
|
100 |
+
"X-TKPD-AKAMAI": "pdpGetLayout",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
}
|
102 |
|
103 |
+
return requests.request(
|
104 |
+
method="POST", url=endpoint, json=payload, headers=headers, timeout=30
|
105 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
|
108 |
# Function to request product reviews from Tokopedia
|