Spaces:
Sleeping
Sleeping
fix header
Browse files
app.py
CHANGED
@@ -51,39 +51,34 @@ cache = {}
|
|
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 = {
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
},
|
65 |
}
|
66 |
headers = {
|
67 |
-
'authority': 'gql.tokopedia.com',
|
68 |
'accept': '*/*',
|
69 |
-
'accept-language': 'en-US,en;q=0.9',
|
70 |
'content-type': 'application/json',
|
71 |
-
'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',
|
72 |
'origin': 'https://www.tokopedia.com',
|
73 |
-
'
|
74 |
-
'
|
75 |
-
'sec-ch-ua': '"Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"',
|
76 |
'sec-ch-ua-mobile': '?1',
|
77 |
'sec-ch-ua-platform': '"Android"',
|
78 |
'sec-fetch-dest': 'empty',
|
79 |
'sec-fetch-mode': 'cors',
|
80 |
'sec-fetch-site': 'same-site',
|
81 |
-
'user-agent': 'Mozilla/5.0 (
|
82 |
'x-device': 'mobile',
|
83 |
'x-source': 'tokopedia-lite',
|
84 |
'x-tkpd-akamai': 'pdpGetLayout',
|
85 |
-
'x-tkpd-lite-service': '
|
86 |
-
'x-version': '
|
87 |
}
|
88 |
|
89 |
response = requests.post(ENDPOINT, headers=headers, json=payload)
|
@@ -251,7 +246,7 @@ def scrape(product_id, max_reviews=LIMIT):
|
|
251 |
def get_product_id(URL):
|
252 |
parsed_url = urlparse(URL)
|
253 |
*_, shop, product_key = parsed_url.path.split("/")
|
254 |
-
response = request_product_id(shop, product_key)
|
255 |
if response:
|
256 |
product_id = response.json()["data"]["pdpGetLayout"]["basicInfo"]["id"]
|
257 |
logger.info(f"Product ID: {product_id}")
|
|
|
51 |
import json
|
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": shop_domain,
|
60 |
+
"productKey": product_key,
|
61 |
+
"layoutID": "",
|
62 |
+
"apiVersion": 1,
|
63 |
+
}
|
|
|
64 |
}
|
65 |
headers = {
|
|
|
66 |
'accept': '*/*',
|
|
|
67 |
'content-type': 'application/json',
|
|
|
68 |
'origin': 'https://www.tokopedia.com',
|
69 |
+
'referer': url,
|
70 |
+
'sec-ch-ua': '"Chromium";v="122", "Google Chrome";v="122", "Not;A=Brand";v="99"',
|
|
|
71 |
'sec-ch-ua-mobile': '?1',
|
72 |
'sec-ch-ua-platform': '"Android"',
|
73 |
'sec-fetch-dest': 'empty',
|
74 |
'sec-fetch-mode': 'cors',
|
75 |
'sec-fetch-site': 'same-site',
|
76 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
|
77 |
'x-device': 'mobile',
|
78 |
'x-source': 'tokopedia-lite',
|
79 |
'x-tkpd-akamai': 'pdpGetLayout',
|
80 |
+
'x-tkpd-lite-service': 'zeus',
|
81 |
+
'x-version': 'bcc81b6'
|
82 |
}
|
83 |
|
84 |
response = requests.post(ENDPOINT, headers=headers, json=payload)
|
|
|
246 |
def get_product_id(URL):
|
247 |
parsed_url = urlparse(URL)
|
248 |
*_, shop, product_key = parsed_url.path.split("/")
|
249 |
+
response = request_product_id(shop, product_key, URL)
|
250 |
if response:
|
251 |
product_id = response.json()["data"]["pdpGetLayout"]["basicInfo"]["id"]
|
252 |
logger.info(f"Product ID: {product_id}")
|